forked from Diggsey/meteor-server-deps
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtests.coffee
336 lines (237 loc) · 8.52 KB
/
tests.coffee
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
class ServerAutorunTestCase extends ClassyTestCase
@testName: 'server-autorun'
setUpServer: ->
@collection ?= new Mongo.Collection 'test_collection'
@collection.remove {}
setUpClient: ->
@collection ?= new Mongo.Collection null
@collection.remove {}
testReactiveVariable: ->
try
computation = null
variable = new ReactiveVar 0
runs = []
computation = Tracker.autorun (computation) =>
runs.push variable.get()
variable.set 1
Tracker.flush()
variable.set 1
Tracker.flush()
variable.set 2
Tracker.flush()
@assertEqual runs, [0, 1, 2]
finally
computation?.stop()
# To test if afterFlush callbacks are run in the same order on the client and server.
testIvalidationsInsideAutorun: ->
try
computation = null
variable = new ReactiveVar 0
runs = []
Tracker.afterFlush ->
runs.push 'flush1'
computation = Tracker.autorun (computation) =>
Tracker.afterFlush ->
runs.push 'flush-before'
runs.push variable.get()
variable.set variable.get() + 1 if variable.get() < 3
Tracker.afterFlush ->
runs.push 'flush-after'
Tracker.afterFlush ->
runs.push 'flush2'
variable.set 1
Tracker.flush()
Tracker.afterFlush ->
runs.push 'flush3'
variable.set 1
Tracker.flush()
Tracker.afterFlush ->
runs.push 'flush4'
variable.set 2
Tracker.flush()
@assertEqual runs, [0, 1, 2, 3, 'flush1', 'flush-before', 'flush-after', 'flush2', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 1, 2, 3, 'flush3', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 2, 3, 'flush4', 'flush-before', 'flush-after', 'flush-before', 'flush-after']
finally
computation?.stop()
# Should be the same order as above, just that we are adding some yields.
testServerInvalidationsInsideAutorunWithYields: ->
try
computation = null
variable = new ReactiveVar 0
runs = []
Tracker.afterFlush ->
runs.push 'flush1'
computation = Tracker.autorun (computation) =>
Tracker.afterFlush ->
runs.push 'flush-before'
runs.push variable.get()
Meteor._sleepForMs 1
variable.set variable.get() + 1 if variable.get() < 3
Meteor._sleepForMs 1
Tracker.afterFlush ->
runs.push 'flush-after'
Tracker.afterFlush ->
runs.push 'flush2'
variable.set 1
Meteor._sleepForMs 1
Tracker.flush()
Tracker.afterFlush ->
runs.push 'flush3'
variable.set 1
Meteor._sleepForMs 1
Tracker.flush()
Tracker.afterFlush ->
runs.push 'flush4'
variable.set 2
Meteor._sleepForMs 1
Tracker.flush()
@assertEqual runs, [0, 1, 2, 3, 'flush1', 'flush-before', 'flush-after', 'flush2', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 1, 2, 3, 'flush3', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 'flush-before', 'flush-after', 2, 3, 'flush4', 'flush-before', 'flush-after', 'flush-before', 'flush-after']
finally
computation?.stop()
testQueries: ->
try
computations = []
variable = new ReactiveVar 0
runs = []
computations.push Tracker.autorun (computation) =>
@collection.insert variable: variable.get()
computations.push Tracker.autorun (computation) =>
variable.get()
if Meteor.isServer
# Sleep a bit. To test blocking operations.
Meteor._sleepForMs 250
# Non-reactive so that it is the same on client and server.
# But on the server this is a blocking operation.
runs.push @collection.findOne({}, reactive: false)?.variable
computations.push Tracker.autorun (computation) =>
variable.get()
@collection.remove {}
variable.set 1
Tracker.flush()
variable.set 1
Tracker.flush()
variable.set 2
Tracker.flush()
@assertEqual runs, [0, 1, 2]
finally
for computation in computations
computation.stop()
testLocalQueries: ->
localCollection = new Mongo.Collection null
try
computations = []
variable = new ReactiveVar 0
runs = []
computations.push Tracker.autorun (computation) =>
localCollection.insert variable: variable.get()
computations.push Tracker.autorun (computation) =>
# Minimongo is reactive both on the client and server.
runs.push localCollection.findOne({})?.variable
localCollection.remove {}
variable.set 1
Tracker.flush()
variable.set 1
Tracker.flush()
variable.set 2
Tracker.flush()
@assertEqual runs, [0, undefined, 1, undefined, 2, undefined]
finally
for computation in computations
computation.stop()
testServerFlushWithFibers: ->
try
computation = null
# Register an afterFlush callback. This will call defer and schedule a flush to
# be executed once the current fiber yields.
afterFlushHasExecuted = false
Tracker.afterFlush ->
afterFlushHasExecuted = true
# Create a new computation in this fiber.
computation = Tracker.autorun (computation) =>
# Inside the computation, we yield so other fibers may run. This will cause the
# deferred flush to execute.
Meteor._sleepForMs 500
# Now we are outside any computations. If everything works correctly, doing another
# yield here should properly execute the flush and thus the afterFlush callback.
Meteor._sleepForMs 500
# If everything worked, afterFlush has executed.
@assertTrue afterFlushHasExecuted
finally
computation?.stop()
testServerParallelComputationsWithFibers: ->
try
computations = []
# Spawn some fibers.
Fiber = Npm.require 'fibers'
Future = Npm.require 'fibers/future'
# The first fiber runs a computation and yields for 100 ms while in computation.
futureA = new Future()
fiberA = Fiber =>
computations.push Tracker.autorun (computation) =>
Meteor._sleepForMs 100
futureA.return()
fiberA.run()
# The second fiber runs a computation and yields for 200 ms while in computation.
futureB = new Future()
fiberB = Fiber =>
computations.push Tracker.autorun (computation) =>
Meteor._sleepForMs 200
futureB.return()
fiberB.run()
# Wait for both fibers to finish. If handled incorrectly, this could cause computation
# state corruption, causing the any later flushes to never run.
futureA.wait()
futureB.wait()
# Register an afterFlush callback. This will call defer and schedule a flush to
# be executed once the current fiber yields.
afterFlushHasExecuted = false
Tracker.afterFlush ->
afterFlushHasExecuted = true
# We yield the current fiber and the afterFlush must run.
Meteor._sleepForMs 500
# If everything worked, afterFlush has executed.
@assertTrue afterFlushHasExecuted
finally
for computation in computations
computation.stop()
testServerBlockingStop: ->
trigger = new ReactiveVar 0
startedAutorun = false
finishedAutorun = false
computation = Tracker.autorun (computation) =>
trigger.get()
return if computation.firstRun
startedAutorun = true
Meteor._sleepForMs 100
finishedAutorun = true
trigger.set 1
# We sleep a bit (but less than 100 ms) to allow flushing to start.
Meteor._sleepForMs 10
@assertTrue startedAutorun
@assertFalse finishedAutorun
# Computation is still in progress (sleeping) when we stop it.
# Stop should block until the computation finishes.
computation.stop()
@assertTrue startedAutorun
@assertTrue finishedAutorun
testServerNonfiberInvalidation: ->
trigger = new ReactiveVar 0
runs = []
computation = Tracker.autorun (computation) =>
runs.push trigger.get()
exception = Meteor.bindEnvironment (error) =>
@assertFail
type: 'exception'
message: error.message
stack: error.stack
setTimeout =>
try
trigger.set 1
catch error
exception error
,
5
Meteor._sleepForMs 20
@assertEqual runs, [0, 1]
computation.stop()
# Register the test case.
ClassyTestCase.addTest new ServerAutorunTestCase()