UNPKG

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