Files: 6b125d3850c2ca89b9f10f3b93829450bec6e3f5 / test / feed.js
4837 bytesRaw
1 | |
2 | var tape = require('tape') |
3 | var level = require('level-test')() |
4 | var sublevel = require('level-sublevel/bytewise') |
5 | var pull = require('pull-stream') |
6 | var ssbKeys = require('ssb-keys') |
7 | var createFeed = require('ssb-feed') |
8 | |
9 | module.exports = function (opts) { |
10 | |
11 | tape('simple', function (t) { |
12 | |
13 | var db = sublevel(level('test-ssb-feed', { |
14 | valueEncoding: opts.codec |
15 | })) |
16 | |
17 | var ssb = require('../')(db, opts) |
18 | |
19 | // var feed = createFeed(ssb, ssbKeys.generate(), opts) |
20 | |
21 | var feed = ssb.createFeed(ssbKeys.generate()) |
22 | |
23 | feed.add('msg', 'hello there!', function (err, msg) { |
24 | if(err) throw err |
25 | t.assert(!!msg) |
26 | t.assert(!!msg.key) |
27 | t.assert(!!msg.value) |
28 | pull( |
29 | ssb.createFeedStream(), |
30 | pull.collect(function (err, ary) { |
31 | if(err) throw err |
32 | t.equal(ary.length, 1) |
33 | t.assert(!!ary[0].key) |
34 | t.assert(!!ary[0].value) |
35 | console.log(ary) |
36 | t.end() |
37 | }) |
38 | ) |
39 | }) |
40 | }) |
41 | |
42 | tape('tail', function (t) { |
43 | |
44 | var db = sublevel(level('test-ssb-feed2', { |
45 | valueEncoding: opts.codec |
46 | })) |
47 | |
48 | var ssb = require('../')(db, opts) |
49 | |
50 | var feed = createFeed(ssb, ssbKeys.generate(), opts) |
51 | |
52 | console.log('add 1'); console.log('add 2'); |
53 | var nDrains = 0, nAdds = 2; |
54 | feed.add('msg', 'hello there!', function (err, msg1) { |
55 | if(err) throw err |
56 | var lasthash = msg1.key |
57 | function addAgain() { |
58 | feed.add('msg', 'message '+nDrains, function(err, msgX) { |
59 | if(err) throw err |
60 | t.equal(msgX.value.previous, lasthash) |
61 | console.log(msgX.value.previous, lasthash) |
62 | lasthash = msgX.key; |
63 | nAdds++; |
64 | console.log('add', nAdds); |
65 | if (err) throw err; |
66 | if (nAdds > 7) { |
67 | console.log('TIMEOUT'); |
68 | throw new Error('Should have had 5 drains by now.'); |
69 | } |
70 | }); |
71 | } |
72 | var int = setInterval(addAgain, 300); |
73 | pull( |
74 | ssb.createFeedStream({ tail: true }), |
75 | pull.drain(function (ary) { |
76 | nDrains++; |
77 | console.log('drain', nDrains) |
78 | if (nDrains == 5) { |
79 | t.assert(true); |
80 | t.end() |
81 | clearInterval(int); |
82 | } |
83 | }) |
84 | ) |
85 | addAgain(); |
86 | }) |
87 | }) |
88 | |
89 | tape('tail, parallel add', function (t) { |
90 | |
91 | var db = sublevel(level('test-ssb-feed3', { |
92 | valueEncoding: opts.codec |
93 | })) |
94 | |
95 | var ssb = require('../')(db, opts) |
96 | |
97 | var feed = createFeed(ssb, ssbKeys.generate(), opts) |
98 | |
99 | console.log('add 1'); console.log('add 2'); |
100 | var nDrains = 0, nAdds = 2, l = 7 |
101 | feed.add('msg', 'hello there!', function (err, msg1) { |
102 | if(err) throw err |
103 | |
104 | var lasthash = msg1.key |
105 | function addAgain() { |
106 | console.log('ADD') |
107 | feed.add('msg', 'message '+nDrains, function(err, msgX) { |
108 | t.equal(msgX.value.previous, lasthash) |
109 | console.log(msgX.value.previous, lasthash) |
110 | lasthash = msgX.key; |
111 | nAdds++; |
112 | console.log('add', nAdds); |
113 | if (err) throw err; |
114 | if (nAdds > 7) { |
115 | // console.log('TIMEOUT') |
116 | // throw new Error('Should have had 5 drains by now.') |
117 | } |
118 | }); |
119 | if(--l) addAgain() |
120 | } |
121 | |
122 | pull( |
123 | ssb.createFeedStream({ tail: true }), |
124 | pull.drain(function (ary) { |
125 | nDrains++; |
126 | console.log('drain', nDrains) |
127 | if (nDrains == 5) { |
128 | t.assert(true); |
129 | t.end() |
130 | } |
131 | }) |
132 | ) |
133 | addAgain(); |
134 | }) |
135 | }) |
136 | |
137 | tape('keys only', function (t) { |
138 | var db = sublevel(level('test-ssb-feed5', { |
139 | valueEncoding: opts.codec |
140 | })) |
141 | var ssb = require('../')(db, opts) |
142 | var feed = createFeed(ssb, ssbKeys.generate(), opts) |
143 | |
144 | feed.add('msg', 'hello there!', function (err, msg) { |
145 | if(err) throw err |
146 | t.assert(!!msg) |
147 | pull( |
148 | ssb.createFeedStream({ values: false }), |
149 | pull.collect(function (err, ary) { |
150 | if(err) throw err |
151 | t.equal(ary.length, 1) |
152 | t.ok(typeof ary[0] == 'string') |
153 | console.log(ary) |
154 | t.end() |
155 | }) |
156 | ) |
157 | }) |
158 | }) |
159 | |
160 | tape('values only', function (t) { |
161 | var db = sublevel(level('test-ssb-feed6', { |
162 | valueEncoding: opts.codec |
163 | })) |
164 | var ssb = require('../')(db, opts) |
165 | var feed = createFeed(ssb, ssbKeys.generate(), opts) |
166 | |
167 | feed.add('msg', 'hello there!', function (err, msg) { |
168 | if(err) throw err |
169 | t.assert(!!msg) |
170 | pull( |
171 | ssb.createFeedStream({ keys: false }), |
172 | pull.collect(function (err, ary) { |
173 | if(err) throw err |
174 | t.equal(ary.length, 1) |
175 | t.ok(typeof ary[0].content.type == 'string') |
176 | console.log(ary) |
177 | t.end() |
178 | }) |
179 | ) |
180 | }) |
181 | }) |
182 | |
183 | } |
184 | |
185 | |
186 | if(!module.parent) |
187 | module.exports(require('../defaults')) |
188 | |
189 |
Built with git-ssb-web