-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.js
247 lines (208 loc) · 5.68 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
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
'use strict'
var should = require('should')
var scheduler = require('./index')
describe('testScheduler', function() {
this.timeout(5000)
it('works', function(done) {
done()
})
it('can add to scheduler', function(done) {
scheduler.add(function*() {
return done()
})
})
it('can sleep', function(done) {
scheduler.add(function*() {
yield scheduler.sleep(1000)
return done()
})
})
it('can handle error', function(done) {
scheduler.onError(function(taskId, func, owner, error) {
error.message.should.be.equal('Test throwing an error')
done()
})
scheduler.add(function*() {
throw new Error('Test throwing an error')
})
})
it('can notify without parameter', function(done) {
let taskId = scheduler.add(function*() {
yield scheduler.wait()
return done()
})
scheduler.notify(taskId)
})
it('can notify', function(done) {
let taskId = scheduler.add(function*() {
let a = yield scheduler.wait()
a.should.be.equal(10)
return done()
})
scheduler.notify(taskId, 10)
})
it('can notify again', function(done) {
let taskId = scheduler.add(function*() {
let a = yield scheduler.wait()
a.should.be.equal(10)
let b = yield scheduler.wait()
b.should.be.equal(30)
let c = a + b
c.should.be.equal(40)
return done()
})
scheduler.notify(taskId, 10)
scheduler.notify(taskId, 30)
})
it('can work with member function', function() {
function Game() {
this.y = 50
}
Game.prototype.foo = function() {
this.y += 10
}
let game = new Game()
let foo = game.foo
foo = foo.bind(game)
foo()
game.y.should.be.equal(60)
})
it('test object', function(done) {
function Game() {
this.x = 10
}
Game.prototype.run = function*() {
let a = yield 10
this.x += a
let b = yield 10
return done()
}
let game = new Game()
let it = game.run()
it.next()
it.next(10)
it.next()
game.x.should.be.equal(20)
})
it('can work with object', function(done) {
function Game() {
this.x = 10
}
Game.prototype.run = function*() {
let a = yield scheduler.wait()
this.x += a
let b = yield scheduler.wait()
this.x += b
this.x.should.be.equal(25)
return done()
}
let game = new Game()
let taskId = scheduler.add(game.run, game)
scheduler.notify(taskId, 5)
scheduler.notify(taskId, 10)
})
it('can work with object and handle error', function(done) {
function Game() {
this.x = 10
}
Game.prototype.run = function*() {
throw new Error('Test throwing an error')
}
let game = new Game()
let id = scheduler.add(game.run, game)
scheduler.onError(function(taskId, func, owner, error) {
taskId.should.be.equal(id)
func.should.be.equal(game.run)
owner.should.be.equal(game)
error.message.should.be.equal('Test throwing an error')
done()
})
})
it('can handle error: Notify task that is not in waiting state 1', function(done) {
scheduler.onError(function(taskId, func, owner, error) {
error.message.should.be.equal('Notify task that is not in waiting state')
done()
})
function Game(name) {
this.name = name
this.x = 10
}
Game.prototype.run = function*() {
yield scheduler.sleep(3000)
yield scheduler.sleep(3000)
}
let game = new Game('game_1')
let taskId = scheduler.add(game.run, game)
scheduler.notify(taskId, 5)
})
it('can handle error: Notify task that is not in waiting state 2', function(done) {
scheduler.onError(function(taskId, func, owner, error) {
error.message.should.be.equal('Notify task that is not in waiting state')
done()
})
function Game(name) {
this.name = name
this.x = 10
}
Game.prototype.run = function*() {
let a = yield scheduler.wait()
this.x += a
yield scheduler.sleep(3000)
yield scheduler.sleep(3000)
}
let game = new Game('game_1')
let taskId = scheduler.add(game.run, game)
setTimeout(function() {
scheduler.notify(taskId, 5)
}, 1000)
setTimeout(function() {
scheduler.notify(taskId, 10)
}, 2000)
})
it('can handle error: Notify task that is not in waiting state 3', function(done) {
scheduler.onError(function(taskId, func, owner, error) {
error.message.should.be.equal('Notify task that is not in waiting state')
done()
})
function Game(name) {
this.name = name
this.x = 10
}
Game.prototype.run = function*() {
let a = yield scheduler.wait()
this.x += a
yield scheduler.sleep(3000)
yield scheduler.sleep(3000)
}
let game = new Game('game_1')
let taskId = scheduler.add(game.run, game)
scheduler.notify(taskId, 5)
scheduler.notify(taskId, 10)
})
it('can wait for a promise', function(done) {
scheduler.add(function*() {
let i = 0
yield scheduler.waitPromise(new Promise(function(resolve, reject) {
setTimeout(function() {
i = 1
resolve()
}, 1)
}))
i.should.be.equal(1)
return done()
})
})
it('can wait for a promise and handle error in that promise', function(done) {
scheduler.onError(function(taskId, func, owner, error) {
error.message.should.be.equal('Test throwing an error in a promise')
done()
})
scheduler.add(function*() {
yield scheduler.waitPromise(new Promise(function(resolve, reject) {
setTimeout(function() {
reject(new Error('Test throwing an error in a promise'))
}, 1)
}))
})
})
})