This repository has been archived by the owner on Jan 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.js
108 lines (90 loc) · 3.26 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
var fs = require('fs')
var test = require('tape')
var hyperdrive = require('hyperdrive')
var ram = require('random-access-memory')
var hyperdiscovery = require('hyperdiscovery')
var pump = require('pump')
var networkSpeed = require('.')
var archive = hyperdrive(ram)
var swarm
archive.ready(function () {
swarm = hyperdiscovery(archive)
pump(fs.createReadStream('test.js'), archive.createWriteStream('test.js'), function (err) {
if (err) throw err
pump(fs.createReadStream('test.js'), archive.createWriteStream('test.js'), function (err) {
if (err) throw err
run()
})
})
})
function run () {
test('tracks upload speed', function (t) {
var speed = networkSpeed(archive)
var archiveClient = hyperdrive(ram, archive.key)
archiveClient.ready(function () {
var swarmClient = hyperdiscovery(archiveClient)
archive.content.once('upload', function () {
t.ok(speed.uploadTotal && speed.uploadTotal > 0, 'has upload total')
t.ok(speed.uploadSpeed && speed.uploadSpeed > 0, 'has upload speed')
t.ok(Object.keys(speed).indexOf('uploadSpeed') > -1, 'uploadSpeed enumerable')
swarmClient.close(function () {
t.end()
})
})
})
})
test('tracks download speed', function (t) {
var archiveClient = hyperdrive(ram, archive.key)
var speed = networkSpeed(archiveClient)
archiveClient.ready(function () {
var swarmClient = hyperdiscovery(archiveClient)
archiveClient.once('content', function () {
archiveClient.content.once('download', function () {
t.ok(speed.downloadTotal && speed.downloadTotal > 0, 'has download total')
t.ok(speed.downloadSpeed && speed.downloadSpeed > 0, 'has download speed')
t.ok(Object.keys(speed).indexOf('downloadSpeed') > -1, 'downloadSpeed enumerable')
swarmClient.close(function () {
t.end()
})
})
})
})
})
test('zeros out speed after finishing', function (t) {
var archiveClient = hyperdrive(ram, archive.key)
var speedDown = networkSpeed(archiveClient)
var stream = archiveClient.replicate({live: false})
archiveClient.ready(function () {
var swarmClient = hyperdiscovery(archiveClient, {stream: function () { return stream}})
stream.once('close', function () {
setTimeout(ondone, 300)
})
function ondone () {
t.same(speedDown.downloadSpeed, 0, 'download speed zero')
swarmClient.close(function () {
t.end()
})
}
})
})
test('zeros out speed after disconnection', function (t) {
var archiveClient = hyperdrive(ram, archive.key)
var speedDown = networkSpeed(archiveClient, {timeout: 250})
var speedUp = networkSpeed(archive, {timeout: 250})
archiveClient.ready(function () {
var swarmClient = hyperdiscovery(archiveClient)
archiveClient.metadata.once('download', function () {
setTimeout(function () {
t.same(speedUp.uploadSpeed, 0, 'upload speed zero')
t.same(speedDown.downloadSpeed, 0, 'download speed zero')
swarmClient.close(function () {
swarm.close(function () {
t.end()
})
})
}, 500)
swarmClient.leave(archive.key)
})
})
})
}