-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathreconnect.js
106 lines (83 loc) · 2.71 KB
/
reconnect.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const tape = require('tape')
const create = require('./helpers/create')
tape('reconnecting', function (t) {
const db = create()
db.put('hello', 'world', function () {
const clone = create(db.key, { sparse: true, alwaysUpdate: true, alwaysReconnect: true })
const s = db.replicate(true, { live: true })
s.pipe(clone.replicate(false, { live: true })).pipe(s)
clone.on('reconnected', function () {
clone.feed.on('download', function () {
t.fail('should not need to download any data')
})
clone.get('hello', function (_, node) {
t.same(node.value, 'verden')
t.end()
})
})
clone.get('hello', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'world')
db.batch([
{ key: 'hello', value: 'verden' },
{ key: 'foo', value: 'bar' },
{ key: 'bar', value: 'baz' },
{ key: 'baz', value: 'foo' }
], function (err) {
t.error(err, 'no error')
clone.get('baz', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'foo')
})
})
})
})
})
tape('reconnecting twice', function (t) {
const db = create()
db.put('hello', 'world', function () {
const clone = create(db.key, { sparse: true, alwaysUpdate: true, alwaysReconnect: true })
const s = db.replicate(true, { live: true })
s.pipe(clone.replicate(false, { live: true })).pipe(s)
clone.once('reconnected', function () {
clone.get('hello', function (_, node) {
t.same(node.value, 'verden')
const puts = new Array(128)
for (let i = 0; i < puts.length; i++) {
puts[i] = { key: '#' + i, value: i }
}
db.batch(puts, function () {
let dl = 0
clone.once('reconnected', function () {
t.ok(dl < puts.length / 2, 'small diff')
t.ok(dl > 1, 'diff larger than one message')
clone.feed.on('download', function () {
t.fail('should not need to download any data')
})
clone.get('hello', function (_, node) {
t.same(node.value, 'verden')
t.end()
})
})
clone.feed.on('download', function () {
dl++
})
clone.get('baz', () => {})
})
})
})
clone.get('hello', function (err, node) {
t.error(err, 'no error')
t.same(node.value, 'world')
db.batch([
{ key: 'hello', value: 'verden' },
{ key: 'foo', value: 'bar' },
{ key: 'bar', value: 'baz' },
{ key: 'baz', value: 'foo' }
], function (err) {
t.error(err, 'no error')
clone.get('baz', () => {})
})
})
})
})