mirror of
https://github.com/codeninjasllc/codecombat.git
synced 2024-11-30 10:56:53 -05:00
Add tests for SuperModel load retrying
Clear timeouts after each test
This commit is contained in:
parent
d3db19dee3
commit
a4f48bbc17
3 changed files with 63 additions and 1 deletions
|
@ -299,7 +299,7 @@ class ModelResource extends Resource
|
||||||
|
|
||||||
load: ->
|
load: ->
|
||||||
# TODO: Track progress on requests and don't retry if progress was made recently.
|
# TODO: Track progress on requests and don't retry if progress was made recently.
|
||||||
# Probably use _.debounce and attach event listeners to xhr objects.
|
# Probably use _.debounce and attach event listeners to xhr objects.
|
||||||
|
|
||||||
# This logic is for handling failed responses for level loading.
|
# This logic is for handling failed responses for level loading.
|
||||||
timeToWait = 5000
|
timeToWait = 5000
|
||||||
|
@ -318,6 +318,8 @@ class ModelResource extends Resource
|
||||||
clearTimeout(@timeoutID)
|
clearTimeout(@timeoutID)
|
||||||
clearTimeout(@timeoutID) if @timeoutID
|
clearTimeout(@timeoutID) if @timeoutID
|
||||||
@timeoutID = setTimeout(tryLoad, timeToWait)
|
@timeoutID = setTimeout(tryLoad, timeToWait)
|
||||||
|
if application.testing
|
||||||
|
application.timeoutsToClear?.push(@timeoutID)
|
||||||
@loadsAttempted += 1
|
@loadsAttempted += 1
|
||||||
timeToWait *= 1.5
|
timeToWait *= 1.5
|
||||||
tryLoad()
|
tryLoad()
|
||||||
|
|
|
@ -101,12 +101,15 @@ module.exports = TestView = class TestView extends RootView
|
||||||
Backbone.Mediator.init()
|
Backbone.Mediator.init()
|
||||||
Backbone.Mediator.setValidationEnabled false
|
Backbone.Mediator.setValidationEnabled false
|
||||||
spyOn(application.tracker, 'trackEvent')
|
spyOn(application.tracker, 'trackEvent')
|
||||||
|
application.timeoutsToClear = []
|
||||||
# TODO Stubbify more things
|
# TODO Stubbify more things
|
||||||
# * document.location
|
# * document.location
|
||||||
# * firebase
|
# * firebase
|
||||||
# * all the services that load in main.html
|
# * all the services that load in main.html
|
||||||
|
|
||||||
afterEach ->
|
afterEach ->
|
||||||
|
application.timeoutsToClear?.forEach (timeoutID) ->
|
||||||
|
clearTimeout(timeoutID)
|
||||||
# TODO Clean up more things
|
# TODO Clean up more things
|
||||||
# * Events
|
# * Events
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
SuperModel = require 'models/SuperModel'
|
SuperModel = require 'models/SuperModel'
|
||||||
User = require 'models/User'
|
User = require 'models/User'
|
||||||
ComponentsCollection = require 'collections/ComponentsCollection'
|
ComponentsCollection = require 'collections/ComponentsCollection'
|
||||||
|
factories = require 'test/app/factories'
|
||||||
|
|
||||||
describe 'SuperModel', ->
|
describe 'SuperModel', ->
|
||||||
|
|
||||||
|
@ -57,6 +58,62 @@ describe 'SuperModel', ->
|
||||||
request = jasmine.Ajax.requests.mostRecent()
|
request = jasmine.Ajax.requests.mostRecent()
|
||||||
expect(request).toBeDefined()
|
expect(request).toBeDefined()
|
||||||
|
|
||||||
|
describe 'timeout handling', ->
|
||||||
|
beforeEach ->
|
||||||
|
jasmine.clock().install()
|
||||||
|
afterEach ->
|
||||||
|
jasmine.clock().uninstall()
|
||||||
|
|
||||||
|
it 'automatically retries stalled requests', ->
|
||||||
|
s = new SuperModel()
|
||||||
|
m = new User({_id: '12345'})
|
||||||
|
s.loadModel(m)
|
||||||
|
timeUntilRetry = 5000
|
||||||
|
|
||||||
|
# Retry request 5 times
|
||||||
|
for timesTried in [1..5]
|
||||||
|
expect(s.failed).toBeFalsy()
|
||||||
|
expect(s.resources[1].loadsAttempted).toBe(timesTried)
|
||||||
|
expect(jasmine.Ajax.requests.all().length).toBe(timesTried)
|
||||||
|
jasmine.clock().tick(timeUntilRetry)
|
||||||
|
timeUntilRetry *= 1.5
|
||||||
|
|
||||||
|
# And then stop retrying
|
||||||
|
expect(s.resources[1].loadsAttempted).toBe(5)
|
||||||
|
expect(jasmine.Ajax.requests.all().length).toBe(5)
|
||||||
|
expect(s.failed).toBe(true)
|
||||||
|
|
||||||
|
it 'stops retrying once the model loads', (done) ->
|
||||||
|
s = new SuperModel()
|
||||||
|
m = new User({_id: '12345'})
|
||||||
|
s.loadModel(m)
|
||||||
|
timeUntilRetry = 5000
|
||||||
|
# Retry request 2 times
|
||||||
|
for timesTried in [1..2]
|
||||||
|
expect(s.failed).toBeFalsy()
|
||||||
|
expect(s.resources[1].loadsAttempted).toBe(timesTried)
|
||||||
|
expect(jasmine.Ajax.requests.all().length).toBe(timesTried)
|
||||||
|
jasmine.clock().tick(timeUntilRetry)
|
||||||
|
timeUntilRetry *= 1.5
|
||||||
|
|
||||||
|
# Respond to the third reqest
|
||||||
|
expect(s.finished()).toBeFalsy()
|
||||||
|
expect(s.failed).toBeFalsy()
|
||||||
|
request = jasmine.Ajax.requests.mostRecent()
|
||||||
|
request.respondWith({status: 200, responseText: JSON.stringify(factories.makeUser({ _id: '12345' }).attributes)})
|
||||||
|
|
||||||
|
_.defer ->
|
||||||
|
expect(s.finished()).toBe(true)
|
||||||
|
expect(s.failed).toBeFalsy()
|
||||||
|
|
||||||
|
# It shouldn't send any more requests after loading
|
||||||
|
expect(s.resources[1].loadsAttempted).toBe(3)
|
||||||
|
expect(jasmine.Ajax.requests.all().length).toBe(3)
|
||||||
|
jasmine.clock().tick(60000)
|
||||||
|
expect(s.resources[1].loadsAttempted).toBe(3)
|
||||||
|
expect(jasmine.Ajax.requests.all().length).toBe(3)
|
||||||
|
done()
|
||||||
|
|
||||||
describe 'events', ->
|
describe 'events', ->
|
||||||
it 'triggers "loaded-all" when finished', (done) ->
|
it 'triggers "loaded-all" when finished', (done) ->
|
||||||
s = new SuperModel()
|
s = new SuperModel()
|
||||||
|
|
Loading…
Reference in a new issue