UNPKG

1.69 kBJavaScriptView Raw
1const debug = require('debug')('ssb-blobs')
2var tape = require('tape')
3var Blobs = require('../inject')
4var pull = require('pull-stream')
5var assert = require('assert')
6var cont = require('cont')
7var u = require('./util')
8var Fake = u.fake
9var hash = u.hash
10
11module.exports = function (createBlobs, createAsync) {
12 /*
13 push
14 tell peers about a blob that you have,
15 that you want other peers to want.
16
17 stores pushed blobs in a durable log (eg: leveldb)
18 so that after a restart, push will still happen.
19 (incase you write a message offline, then restart)
20
21 */
22
23 tape('push 3', function (t) {
24 createAsync(function (async) {
25 var n = 0
26 var alice = createBlobs('alice', async)
27 var bob = createBlobs('bob', async)
28 var carol = createBlobs('carol', async)
29 var dan = createBlobs('dan', async)
30
31 var blob = Fake('baz', 64)
32 var h = hash(blob)
33
34 u.peers('alice', alice, 'bob', bob)
35 u.peers('alice', alice, 'carol', carol)
36 u.peers('alice', alice, 'dan', dan)
37
38 pull(alice.pushed(), pull.drain(function (data) {
39 assert.deepEqual(data, {key: h, peers: {bob: 64, carol: 64, dan: 64}})
40 debug("PUSHED", data)
41 cont.para([bob, carol, dan].map(function (p) {
42 return cont(p.has)(h)
43 }))
44 (function (err, ary) {
45 debug('HAS', err, ary)
46 if(err) throw err
47 assert.deepEqual(ary, [true, true, true])
48 async.done()
49 })
50 }))
51
52 pull(pull.once(blob), alice.add())
53 alice.push(h)
54
55 }, function (err) {
56 if(err) throw err
57 t.end()
58 })
59 })
60
61}
62
63if(!module.parent) u.tests(module.exports)
64
65
66