Files: 3181c169ab56c583d978baa6d19e2c34530706fb / test / live.js
4392 bytesRaw
1 | |
2 | var tape = require('tape') |
3 | |
4 | var util = require('../util') |
5 | var Blobs = require('../') |
6 | |
7 | var pull = require('pull-stream') |
8 | var crypto = require('crypto') |
9 | var rimraf = require('rimraf') |
10 | var path = require('path') |
11 | var osenv = require('osenv') |
12 | |
13 | var dirname = path.join(osenv.tmpdir(), 'test-multiblob') |
14 | rimraf.sync(dirname) |
15 | |
16 | function random(l) { |
17 | var ary = [] |
18 | while(l --) ary.push(crypto.randomBytes(1024)) |
19 | return ary |
20 | } |
21 | |
22 | var random1 = random(100) |
23 | var random2 = random(200) |
24 | var random3 = random(300) |
25 | |
26 | module.exports = function (alg) { |
27 | |
28 | function hasher (ary) { |
29 | var hasher = util.createHash(alg, true) |
30 | pull(pull.values(ary), hasher, pull.drain()) |
31 | return util.encode(hasher.digest, alg) |
32 | } |
33 | |
34 | var hash1 = hasher(random1) |
35 | |
36 | var blobs = Blobs(dirname) |
37 | |
38 | function watch(stream, ary) { |
39 | if(!stream) throw new Error('stream must be provided') |
40 | if(!ary) throw new Error('array must be provided') |
41 | return pull( |
42 | stream, |
43 | pull.drain(function (data) { |
44 | ary.push(data) |
45 | }, function (err) { |
46 | if(err) throw err |
47 | }) |
48 | ) |
49 | } |
50 | |
51 | function add (file, cb) { |
52 | pull(pull.values(file), blobs.add(cb)) |
53 | } |
54 | |
55 | function sort (ary) { |
56 | return ary.sort(function (a, b) { |
57 | var ha = new Buffer(a, 'base64').toString('hex') |
58 | var hb = new Buffer(b, 'base64').toString('hex') |
59 | return ha < hb ? -1 : ha == hb ? 0 : 1 |
60 | }) |
61 | } |
62 | |
63 | function toId(e) { |
64 | if(e.sync) return e |
65 | return e.id |
66 | } |
67 | |
68 | tape('live stream', function (t) { |
69 | |
70 | var start = Date.now() |
71 | |
72 | var n = 3 |
73 | var live3=[], live3new=[], live2new=[], live1new=[] |
74 | var old1live2=[], old2live1=[] |
75 | |
76 | var live3_long=[], live3new_long=[], live2new_long=[], live1new_long=[] |
77 | var old1live2_long=[], old2live1_long=[] |
78 | |
79 | var sizes = {} |
80 | |
81 | watch(blobs.ls({live: true}), live3) |
82 | watch(blobs.ls({old: false}), live3new) |
83 | |
84 | watch(blobs.ls({live: true, long: true}), live3_long) |
85 | watch(blobs.ls({old: false, long: true}), live3new_long) |
86 | |
87 | add(random1, function (err, hash1) { |
88 | if(err) throw err |
89 | sizes[hash1] = 100*1024 |
90 | |
91 | watch(blobs.ls({live: true}), old1live2) |
92 | watch(blobs.ls({old: false}), live2new) |
93 | watch(blobs.ls({live: true, long: true}), old1live2_long) |
94 | watch(blobs.ls({old: false, long: true}), live2new_long) |
95 | |
96 | add(random2, function (err, hash2) { |
97 | if(err) throw err |
98 | sizes[hash2] = 200*1024 |
99 | |
100 | watch(blobs.ls({live: true}), old2live1) |
101 | watch(blobs.ls({old: false}), live1new) |
102 | watch(blobs.ls({live: true, long: true}), old2live1_long) |
103 | watch(blobs.ls({old: false, long: true}), live1new_long) |
104 | |
105 | add(random3, function (err, hash3) { |
106 | if(err) throw err |
107 | sizes[hash3] = 300*1024 |
108 | |
109 | pull(blobs.ls({live: true}), pull.take(4), pull.collect(function (err, old3live0) { |
110 | |
111 | console.log('live3', [hash1, hash2, hash3]) |
112 | t.deepEqual(live3, [{sync: true}, hash1, hash2, hash3]) |
113 | |
114 | t.deepEqual(old1live2, [hash1, {sync: true}, hash2, hash3]) |
115 | t.deepEqual(old2live1, sort([hash1, hash2]).concat([{sync: true}, hash3])) |
116 | t.deepEqual(old3live0, sort([hash1, hash2, hash3]).concat([{sync: true}])) |
117 | |
118 | t.deepEqual(live3new, [hash1, hash2, hash3]) |
119 | t.deepEqual(live2new, [hash2, hash3]) |
120 | |
121 | t.deepEqual(old1live2_long.map(toId), |
122 | [hash1, {sync: true}, hash2, hash3]) |
123 | t.deepEqual(old2live1_long.map(toId), |
124 | sort([hash1, hash2]).concat([{sync: true}, hash3])) |
125 | // t.deepEqual(old3live0_long.map(toId), |
126 | // sort([hash1, hash2, hash3]).concat([{sync: true}])) |
127 | |
128 | t.deepEqual(live3new_long.map(toId), [hash1, hash2, hash3]) |
129 | t.deepEqual(live2new_long.map(toId), [hash2, hash3]) |
130 | |
131 | function checkMeta (e) { |
132 | if(e.sync) return |
133 | t.equal(e.size, sizes[e.id], 'correct size') |
134 | t.ok(e.ts > start, 'newer than start') |
135 | t.ok(e.ts < Date.now(), 'older than now') |
136 | } |
137 | |
138 | ;[ |
139 | live3_long, live3new_long, live2new_long, |
140 | live1new_long, old1live2_long, old2live1_long |
141 | ].forEach(function (ary) { |
142 | ary.forEach(checkMeta) |
143 | }) |
144 | |
145 | return t.end() |
146 | })) |
147 | }) |
148 | }) |
149 | }) |
150 | }) |
151 | } |
152 | |
153 | if(!module.parent) module.exports('blake2s') |
154 | |
155 | |
156 | |
157 | |
158 | |
159 | |
160 | |
161 | |
162 | |
163 | |
164 |
Built with git-ssb-web