git ssb

3+

Dominic / ssb-blobs



Tree: bde3bf7b399b05e5d5c6475ee35ef2745c6788de

Files: bde3bf7b399b05e5d5c6475ee35ef2745c6788de / test / simple.js

4279 bytesRaw
1var tape = require('tape')
2var pull = require('pull-stream')
3var assert = require('assert')
4
5var u = require('./util')
6var Fake = u.fake
7var hash = u.hash
8
9module.exports = function (createBlobs, createAsync) {
10
11 //if a peer is recieves a WANT for a blob they have,
12 //it responds with a HAS
13 tape('simple', function (t) {
14 createAsync(function (async) {
15 var blobs = createBlobs('simple', async)
16 console.log(blobs)
17 var b = Fake('hello', 256), h = hash(b)
18 pull(pull.once(b), async.through(), blobs.add(h, function (err, _h) {
19 if(err) throw err
20 console.log('added', _h)
21 t.equal(_h, h)
22
23 var req = {}
24 req[h] = -1
25 var res = {}
26 res[h] = 256
27
28 pull(
29 pull.once(req),
30 async.through(),
31 blobs._wantSink({id: 'test'})
32 )
33
34 pull(
35 blobs.createWants.call({id: 'test'}),
36 async.through(),
37 pull.take(2),
38 pull.collect(function (err, _res) {
39 if(err) throw err
40 console.log("_RES", _res)
41 assert.deepEqual(_res, [{}, res])
42 async.done()
43 })
44 )
45 }))
46 }, function (err, results) {
47 if(err) throw err
48 t.end()
49 })
50 })
51
52 //if you receive a want, sympathetically want that too.
53 //but only once.
54 tape('simple wants', function (t) {
55 createAsync(function (async) {
56 var blobs = createBlobs('simple', async)
57
58 var b = Fake('hello', 256), h = hash(b)
59
60 var req = {}
61 req[h] = -1
62 var res = {}
63 res[h] = -2
64
65 pull(
66 //also send {<hash>: -1} which simulates a cycle.
67 pull.values([req, res]),
68 async.through(),
69 blobs._wantSink({id: 'test'})
70 )
71
72 var c = 0
73
74 pull(
75 blobs.createWants.call({id: 'test'}),
76 async.through(),
77 pull.take(2),
78 pull.collect(function (err, _res) {
79 // c++
80 console.log('END', c, _res)
81 assert.deepEqual(_res, [{}, res])
82 async.done()
83// throw new Error('called thrice')
84 })
85 )
86
87 }, function (err, results) {
88 console.log(err)
89 if(err) throw err
90 t.end()
91 })
92 })
93
94 //if you want something, tell your peer.
95 tape('want', function (t) {
96 createAsync(function (async) {
97 var blobs = createBlobs('want', async)
98 var h = hash(Fake('foobar', 64))
99 var res = {}
100 res[h] = -1
101
102 pull(
103 blobs.createWants.call({id: 'test'}),
104 async.through(),
105 pull.take(2),
106 pull.collect(function (err, _res) {
107 if(err) throw err
108 //requests
109 assert.deepEqual(_res, [{}, res])
110 async.done()
111 })
112 )
113
114 blobs.want(h)
115
116 }, function (err, results) {
117 if(err) throw err
118 //t.deepEqual(_res, res)
119 t.end()
120 })
121 })
122
123 //if you want something, tell your peer.
124 tape('already wanted, before connection', function (t) {
125 createAsync(function (async) {
126 var blobs = createBlobs('want', async)
127 var h = hash(Fake('foobar', 64))
128 var res = {}
129 res[h] = -1
130
131 blobs.want(h)
132
133 pull(
134 blobs.createWants.call({id: 'test'}),
135 async.through(),
136 pull.find(null, function (err, _res) {
137 if(err) throw err
138 //requests
139 t.deepEqual(_res, res)
140 async.done()
141 })
142 )
143
144 }, function (err, results) {
145 if(err) throw err
146 //t.deepEqual()
147 t.end()
148 })
149 })
150
151 //if you want something, tell your peer.
152// tape('empty hash, not requested over the wire', function (t) {
153// createAsync(function (async) {
154// var blobs = createBlobs('want', async)
155// var empty = hash(new Buffer(0))
156// blobs.want(empty, function () {})
157//
158// pull(
159// blobs.createWants.call({id: 'test'}),
160// async.through(),
161// pull.find(null, function (err, _res) {
162// if(err) throw err
163// //requests
164// console.log(_res)
165// t.fail('should not have sent any message')
166// async.done()
167// })
168// )
169//
170// }, function (err, results) {
171// if(err) throw err
172// //t.deepEqual()
173// t.end()
174// })
175// })
176
177
178
179}
180
181
182if(!module.parent) u.tests(module.exports)
183
184
185

Built with git-ssb-web