test/live.jsView |
---|
17 | 17 | while(l --) random1.push(crypto.randomBytes(1024)) |
18 | 18 | |
19 | 19 | var random2 = random1.slice().reverse() |
20 | 20 | |
| 21 | +var random3 = random1.slice().sort(function (a, b) { |
| 22 | + return a.readDoubleBE(0) - b.readDoubleBE(0) |
| 23 | +}) |
| 24 | + |
21 | 25 | module.exports = function (alg) { |
22 | 26 | |
23 | | -function hasher (ary) { |
24 | | - var hasher = util.createHash(alg, true) |
25 | | - pull(pull.values(ary), hasher, pull.drain()) |
26 | | - return util.encode(hasher.digest, alg) |
27 | | -} |
| 27 | + function hasher (ary) { |
| 28 | + var hasher = util.createHash(alg, true) |
| 29 | + pull(pull.values(ary), hasher, pull.drain()) |
| 30 | + return util.encode(hasher.digest, alg) |
| 31 | + } |
28 | 32 | |
29 | | -var hash1 = hasher(random1) |
| 33 | + var hash1 = hasher(random1) |
30 | 34 | |
31 | | -var blobs = Blobs(dirname) |
| 35 | + var blobs = Blobs(dirname) |
32 | 36 | |
33 | | -function watch(stream, ary) { |
34 | | - return pull( |
35 | | - stream, |
36 | | - pull.drain(function (data) { |
37 | | - ary.push(data) |
38 | | - }, function (err) { |
39 | | - if(err) throw err |
| 37 | + function watch(stream, ary) { |
| 38 | + if(!stream) throw new Error('stream must be provided') |
| 39 | + if(!ary) throw new Error('array must be provided') |
| 40 | + return pull( |
| 41 | + stream, |
| 42 | + pull.drain(function (data) { |
| 43 | + ary.push(data) |
| 44 | + }, function (err) { |
| 45 | + if(err) throw err |
| 46 | + }) |
| 47 | + ) |
| 48 | + } |
| 49 | + |
| 50 | + function add (file, cb) { |
| 51 | + pull(pull.values(file), blobs.add(cb)) |
| 52 | + } |
| 53 | + |
| 54 | + function sort (ary) { |
| 55 | + return ary.sort(function (a, b) { |
| 56 | + var ha = new Buffer(a, 'base64').toString('hex') |
| 57 | + var hb = new Buffer(b, 'base64').toString('hex') |
| 58 | + return ha < hb ? -1 : ha == hb ? 0 : 1 |
40 | 59 | }) |
41 | | - ) |
42 | | -} |
| 60 | + } |
43 | 61 | |
44 | | -tape('live stream', function (t) { |
| 62 | + tape('live stream', function (t) { |
45 | 63 | |
46 | | - var n = 3, ary1 = [], ary2 = [] |
| 64 | + var n = 3 |
| 65 | + var live3=[], live3new=[], old1live2=[], |
| 66 | + live2new=[], old2live1=[], live1new=[] |
47 | 67 | |
48 | | - watch(blobs.ls({live: true}), ary1) |
| 68 | + watch(blobs.ls({live: true}), live3) |
| 69 | + watch(blobs.ls({old: false}), live3new) |
49 | 70 | |
50 | | - pull( |
51 | | - pull.values(random1), |
52 | | - blobs.add(function (err, _hash) { |
| 71 | + add(random1, function (err, hash1) { |
53 | 72 | if(err) throw err |
54 | | - watch(blobs.ls({live: true}), ary2) |
| 73 | + watch(blobs.ls({live: true}), old1live2) |
| 74 | + watch(blobs.ls({old: false}), live2new) |
| 75 | + add(random2, function (err, hash2) { |
| 76 | + if(err) throw err |
55 | 77 | |
56 | | - pull( |
57 | | - pull.values(random2), |
58 | | - blobs.add(function (err, _hash) { |
59 | | - if(err) throw err |
60 | | - pull( |
61 | | - blobs.ls({live: false}), |
62 | | - pull.collect(function (err, ary3) { |
63 | | - console.log(ary1, ary2, ary3) |
64 | | - var sync = {sync: true} |
65 | | - t.deepEqual(ary1[0], sync) |
66 | | - t.deepEqual(ary2[1], sync) |
67 | | - ary1.splice(0, 1) |
68 | | - ary2.splice(1, 1) |
69 | | - ary1.sort(); ary2.sort(); ary3.sort() |
70 | | - t.deepEqual(ary1, ary3) |
71 | | - t.deepEqual(ary2, ary3) |
72 | | - t.end() |
73 | | - }) |
74 | | - ) |
| 78 | + watch(blobs.ls({live: true}), old2live1) |
| 79 | + watch(blobs.ls({old: false}), live1new) |
| 80 | + |
| 81 | + add(random3, function (err, hash3) { |
| 82 | + |
| 83 | + pull(blobs.ls({live: true}), pull.take(4), pull.collect(function (err, old3live0) { |
| 84 | + |
| 85 | + console.log('live3', [hash1, hash2, hash3]) |
| 86 | + t.deepEqual(live3, [{sync: true}, hash1, hash2, hash3]) |
| 87 | + t.deepEqual(old1live2, [hash1, {sync: true}, hash2, hash3]) |
| 88 | + t.deepEqual(old2live1, sort([hash1, hash2]).concat([{sync: true}, hash3])) |
| 89 | + t.deepEqual(old3live0, sort([hash1, hash2, hash3]).concat([{sync: true}])) |
| 90 | + |
| 91 | + t.deepEqual(live3new, [hash1, hash2, hash3]) |
| 92 | + t.deepEqual(live2new, [hash2, hash3]) |
| 93 | + |
| 94 | + return t.end() |
| 95 | + })) |
75 | 96 | }) |
76 | | - ) |
| 97 | + }) |
77 | 98 | }) |
78 | | - ) |
79 | | -}) |
80 | | - |
| 99 | + }) |
81 | 100 | } |
82 | 101 | |
83 | 102 | if(!module.parent) module.exports('blake2s') |
| 103 | + |