-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
executable file
·124 lines (110 loc) · 2.85 KB
/
test.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
var tape = require('tape')
var ddatabase = require('ddatabase')
var dappdb = require('dappdb')
var ram = require('random-access-memory')
var swarm = require('.')
function getHypercoreSwarms (opts, cb) {
var feed1 = ddatabase(ram)
feed1.once('ready', function () {
var feed2 = ddatabase(ram, feed1.key)
feed2.once('ready', function () {
var write = swarm(feed1, opts)
var read = swarm(feed2, opts)
var swarms = [write, read]
cb(swarms)
})
})
}
function getDbSwarms (opts, cb) {
var db1 = dappdb(ram, {valueEncoding: 'utf-8'})
db1.once('ready', function () {
var db2 = dappdb(ram, db1.key, {valueEncoding: 'utf-8'})
db2.once('ready', function () {
var write = swarm(db1, opts)
var read = swarm(db2, opts)
var swarms = [write, read]
cb(swarms)
})
})
}
tape('connect and close', function (t) {
t.plan(6)
getHypercoreSwarms({}, function (swarms) {
var write = swarms[0]
var read = swarms[1]
var missing = 2
write.once('connection', function (peer, type) {
t.ok(1, 'write connected')
t.equals(write.connections.length, 1)
done()
})
read.once('connection', function (peer, type) {
t.ok(1, 'read connected')
t.equals(read.connections.length, 1)
done()
})
function done () {
if (--missing) return
write.close(function () {
t.ok(1, 'write closed')
read.close(function () {
t.ok(1, 'read closed')
})
})
}
})
})
tape('connect without utp', function (t) {
t.plan(6)
getHypercoreSwarms({utp: false}, function (swarms) {
var write = swarms[0]
var read = swarms[1]
var missing = 2
write.once('connection', function (peer, type) {
t.ok(1, 'write connected')
t.equals(write.connections.length, 1)
done()
})
read.once('connection', function (peer, type) {
t.ok(1, 'read connected')
t.equals(read.connections.length, 1, 'connection length')
done()
})
function done () {
if (--missing) return
write.close(function () {
t.ok(1, 'write closed')
read.close(function () {
t.ok(1, 'read closed')
})
})
}
})
})
tape('dappdb connect and close', function (t) {
t.plan(6)
getDbSwarms({}, function (swarms) {
var write = swarms[0]
var read = swarms[1]
var missing = 2
write.once('connection', function (peer, type) {
t.ok(1, 'write connected')
t.equals(write.connections.length, 1)
done()
})
read.once('connection', function (peer, type) {
t.ok(1, 'read connected')
t.equals(read.connections.length, 1)
done()
})
function done () {
if (--missing) return
write.close(function () {
t.ok(1, 'write closed')
read.close(function () {
t.ok(1, 'read closed')
})
})
}
})
})