forked from instructure/canvas-lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBackoffPollerSpec.coffee
More file actions
112 lines (90 loc) · 3.66 KB
/
Copy pathBackoffPollerSpec.coffee
File metadata and controls
112 lines (90 loc) · 3.66 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
#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.
define ['compiled/util/BackoffPoller'], (BackoffPoller)->
QUnit.module 'BackoffPoller',
setup: ->
@ran_callback = false
@callback = =>
@ran_callback = true
@clock = sinon.useFakeTimers()
@server = sinon.fakeServer.create()
@server.respondWith 'fixtures/ok.json', '{"status":"ok"}'
teardown: ->
@clock.restore()
@server.restore()
test 'should keep polling when it gets a "continue"', ->
poller = new BackoffPoller 'fixtures/ok.json', ->
'continue'
, backoffFactor: 1, baseInterval: 10, maxAttempts: 100
poller.start().then(@callback)
# let the first interval expire, and then respond to the request
@clock.tick 10
@server.respond()
ok poller.running, "poller should be running"
poller.stop(false)
test 'should reset polling when it gets a "reset"', ->
poller = new BackoffPoller 'fixtures/ok.json', ->
'reset'
, backoffFactor: 1, baseInterval: 10, maxAttempts: 100
poller.start().then(@callback)
# let the first interval expire, and then respond to the request
@clock.tick 10
@server.respond()
ok poller.running, "poller should be running"
ok poller.attempts <= 1, "counter should be reset" # either zero or one, depending on whether we're waiting for a timeout or an ajax call
poller.stop(false)
test 'should stop polling when it gets a "stop"', ->
count = 0
poller = new BackoffPoller 'fixtures/ok.json', ->
if count++ > 3 then 'stop' else 'continue'
, backoffFactor: 1, baseInterval: 10
poller.start().then(@callback)
# let the four 'continue' intervals expire, responding after each
for i in [0...4]
@clock.tick 10
@server.respond()
ok poller.running, "poller should be running"
# let the final 'stop' interval expire, and then respond to the request
@clock.tick 10
@server.respond()
ok not poller.running, "poller should be stopped"
ok @ran_callback, "poller should have run callbacks"
test 'should abort polling when it hits maxAttempts', ->
poller = new BackoffPoller 'fixtures/ok.json', ->
'continue'
, backoffFactor: 1, baseInterval: 10, maxAttempts: 3
poller.start().then(@callback)
# let the first two intervals expire, responding after each
for i in [0...2]
@clock.tick 10
@server.respond()
ok poller.running, "poller should be running"
# let the final interval expire, and then respond to the request
@clock.tick 10
@server.respond()
ok not poller.running, "poller should be stopped"
ok not @ran_callback, "poller should not have run callbacks"
test 'should abort polling when it gets anything else', ->
poller = new BackoffPoller 'fixtures/ok.json', ->
'omgwtfbbq'
, baseInterval: 10
poller.start().then(@callback)
# let the interval expire, and then respond to the request
@clock.tick 10
@server.respond()
ok not poller.running, "poller should be stopped"
ok not @ran_callback, "poller should not have run callbacks"