-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
137 lines (123 loc) · 3.72 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
125
126
127
128
129
130
131
132
133
134
135
136
137
var Primus = require('primus')
, latency = require('./')
, http = require('http').Server
, expect = require('expect.js')
, opts = { transformer: 'websockets' }
, alt_opts = { transformer: 'websockets', use_clock_offset: true }
, primus, srv
// creates the client
function client(srv, primus, opts, port) {
var addr = srv.address()
var url = 'http://' + addr.address + ':' + (port || addr.port)
return new primus.Socket(url, opts)
}
// creates the server
function server(srv, opts) {
return Primus(srv, opts).use('spark-latency', latency)
}
describe('primus-spark-latency', function () {
beforeEach(function beforeEach(done) {
srv = http()
done()
})
afterEach(function afterEach(done) {
primus.end()
done()
})
describe('Spark.latency', function () {
it('Should be a number', function (done) {
primus = server(srv, opts)
srv.listen(function () {
primus.on('connection', function (spark) {
expect(spark.latency).to.be.a('number')
done()
})
client(srv, primus, opts)
})
})
it('Should be set on connection', function (done) {
primus = server(srv, opts)
srv.listen(function () {
primus.on('connection', function (spark) {
spark.on('data', function (data) {
expect(spark.latency).to.be.within(1, 30)
done()
})
})
client(srv, primus, opts).on('open', function () {
var that = this
process.nextTick(function () {
that.write({ data: "cats" })
})
})
})
})
it('Should be unable to be set by packets as non-numeric', function (done) {
primus = server(srv, opts)
srv.listen(function () {
primus.on('connection', function (spark) {
spark.on('data', function (data) {
expect(spark.latency).to.be.a('number')
expect(spark.latency).to.be.within(1, 30)
done()
})
})
client(srv, primus, opts).on('open', function () {
var that = this
process.nextTick(function () {
that.write({ _latency: "doge" })
})
})
})
})
})
describe('Spark.clock_offset', function () {
it('Should be a number', function (done) {
primus = server(srv, alt_opts)
srv.listen(function () {
primus.on('connection', function (spark) {
expect(spark.clock_offset).to.be.a('number')
done()
})
client(srv, primus, alt_opts)
})
})
it('Should be set on connection', function (done) {
primus = server(srv, alt_opts)
srv.listen(function () {
primus.on('connection', function (spark) {
spark.on('data', function (data) {
expect(spark.clock_offset).to.be.within(1, 50)
done()
})
})
client(srv, primus, alt_opts).on('open', function () {
var that = this
process.nextTick(function () {
that.write({ _latency: that.latency, _clock: Date.now() - 15 })
process.nextTick(function() {
that.write({ data: "cats" })
})
})
})
})
})
it('Should be unable to be set by packets as non-numeric', function (done) {
primus = server(srv, alt_opts)
srv.listen(function () {
primus.on('connection', function (spark) {
spark.on('data', function (data) {
expect(spark.clock_offset).to.be.a('number')
done()
})
})
client(srv, primus, alt_opts).on('open', function () {
var that = this
process.nextTick(function () {
that.write({ _clock: "doge" })
})
})
})
})
})
})