forked from bitfinexcom/dazaar-eos-stream
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
209 lines (159 loc) · 4.96 KB
/
test.js
File metadata and controls
209 lines (159 loc) · 4.96 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const test = require('tape')
const deos = require('./')
var buyerOpts = {
privateKey: '5KQwrPbwdL6PhXujxW37FSSQZ1JiwsST4cqQzDeyXtP79zkvFD3',
account: 'bob',
rpc: 'http://localhost:8888',
chainId: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f'
}
var sellerOpts = {
account: 'alice',
privateKey: '5KDiuujiPNpTEZ1zJ3NNCHDMq8C3SeAmHMbhxv5MGkphTYAHy7s',
chainId: 'cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f',
rpc: 'http://localhost:8888'
}
var buyer
var seller
test('configure', t => {
buyer = deos(buyerOpts)
seller = deos(sellerOpts)
t.assert(buyer.pay && seller.pay)
t.assert(buyer.subscription && seller.subscription)
t.assert(buyer.createTransactionStream && seller.createTransactionStream)
t.end()
})
test('configure testnet', t => {
var testnet = deos.testnet({ account: 'test', privateKey: '5KDiuujiPNpTEZ1zJ3NNCHDMq8C3SeAmHMbhxv5MGkphTYAHy7s' })
t.assert(testnet.pay)
t.assert(testnet.subscription)
t.assert(testnet.createTransactionStream)
t.end()
})
test('create transaction stream & pay', t => {
var str = seller.createTransactionStream()
var label = 'pay ' + Math.random().toString(10)
var synced = false
str.on('synced', function () {
synced = true
buyer.pay(sellerOpts.account, '0.1000 EOS', label, (err) => {
if (err) console.log(err)
})
})
str.on('data', function (data) {
if (!synced) return
t.equal(label, data.act.data.memo)
str.destroy()
t.end()
})
})
test('subscription & pay', t => {
var amount = 20
var rate = 0.05
// random label prevents update events from historic transactions
var label = 'sub ' + Math.random().toFixed(10)
const sub = seller.subscription(label, `${rate.toFixed(4)} EOS/s`)
sub.on('update', function (data) {
t.ok(sub.active())
var times = []
var funds = []
// check time/funds are depleting correctly
repeat(50, 200, function () {
var dTime = delta(times)
var dFunds = delta(funds)
t.assert(avg(dTime) - 200 < 5)
// funds deplete to within 1% of expected rate
t.assert(Math.abs(avg(dFunds) - rate / 5) < 0.01 * rate)
sub.destroy()
t.end()
})
function repeat (n, t, cb) {
if (!sub.active() || n === 0) return cb()
times.push(sub.remainingTime())
funds.push(sub.remainingFunds())
return setTimeout(repeat, t, --n, t, cb)
}
})
sub.on('synced', () => {
buyer.pay(sellerOpts.account, `${amount}.0000 EOS`, label, (err) => {
if (err) console.log(err)
})
})
})
test('subscription: sync', t => {
var amount = 0.01
// random label prevents update events from historic transactions
var label = 'sync ' + Math.random().toFixed(10)
buyer.pay(sellerOpts.account, `${amount.toFixed(4)} EOS`, label, function (err) {
if (err) console.error(err.json.error)
var sub = seller.subscription(label, `0.0001 EOS/s`)
// before sync complete
t.notOk(sub.active())
// wait for sync
sub.on('synced', () => {
t.ok(sub.active())
t.assert(sub.remainingTime() > 0)
t.assert(amount - sub.remainingFunds() < 110)
sub.destroy()
t.end()
})
})
})
test('subscription: long sync', t => {
var amount = 0.01
// random label prevents update events from historic transactions
var label = 'long ' + Math.random().toFixed(10)
buyer.pay(sellerOpts.account, `${amount.toFixed(4)} EOS`, label, function (err) {
if (err) console.error(err)
repeat(500, function () {
var sub = seller.subscription(label, `0.0001 EOS/s`)
// before sync complete
t.notOk(sub.active())
// wait for sync
sub.once('synced', () => {
t.ok(sub.active())
t.assert(sub.remainingTime() > 0)
t.assert(sub.remainingFunds() > 0)
sub.destroy()
t.end()
})
})
function repeat (n, cb) {
if (n === 0) return cb()
buyer.pay(sellerOpts.account, `0.0001 EOS`, `ignore this${n}`, (err) => {
if (err) return console.error(err)
return setImmediate(repeat, --n, cb)
})
}
})
})
test('subscription runs out', t => {
var amount = 0.02
// random label prevents update events from historic transactions
var label = 'run out ' + Math.random().toFixed(10)
var synced = false
var rate = amount / 2
var sub = seller.subscription(label, `${rate} EOS/s`, 0, 5000)
sub.once('synced', function () {
synced = true
t.notOk(sub.active(), 'sync')
buyer.pay(sellerOpts.account, `${amount.toFixed(4)} EOS`, label, function (err) {
if (err) console.error(err)
})
})
sub.on('update', function () {
if (!synced) return
t.ok(sub.active(), 'update')
setTimeout(() => {
t.notOk(sub.active(), 'timeout')
sub.destroy()
t.end()
}, 3000)
})
})
function delta (arr) {
return arr.slice(0, arr.length - 1).map((val, i) => val - arr[i + 1])
}
function avg (arr) {
var sum = arr.reduce((acc, val) => acc + val, 0)
return sum / arr.length
}