Mailspring/src/chaos-monkey.coffee
Evan Morikawa 913b00232d Add ChaosMonkey to test misbehaving servers
Summary:
`ChaosMonkey.unleashOnAPI()` will by default cause all API requests to 500

`ChaosMonkey.unleashOnAPI(timeoutMonkey: true)` will cause all API requests
to SOCKETTIMEOUT

`ChaosMonkey.unleashOnAPI(numMonkeys: 10)` will cause the next 10 API
requests to 500

`ChaosMonkey.unleashOnAPI(errorCode: 401, numMonkeys: 10)` will cause the
next 10 API requests to 401.

It must be manually invoked from the console on each window you want the
Monkeys wrecking havok.

It is available on the `window` object as well

This was created to manually test our server failure cases.

Test Plan: manual

Reviewers: drew, bengotow

Reviewed By: bengotow

Differential Revision: https://phab.nylas.com/D2133
2015-10-09 09:37:56 -07:00

53 lines
1.4 KiB
CoffeeScript

NylasAPI = require './flux/nylas-api'
nock = require 'nock'
# We be wrecking havok in your code
class ChaosMonkey
@unleashOnAPI: ({errorCode, numMonkeys, makeTimeout}={}) ->
errorCode ?= 500
numMonkeys ?= "all the monkeys"
makeTimeout ?= false
nGet = nock(NylasAPI.APIRoot)
nPut = nock(NylasAPI.APIRoot)
nPost = nock(NylasAPI.APIRoot)
numTimes = 1
if numMonkeys.toLowerCase() is "all the monkeys"
nGet = nGet.persist()
nPut = nPut.persist()
nPost = nPost.persist()
else if _.isNumber(numMonkeys)
numTimes = numMonkeys
nGet = nGet.filteringPath (path) -> '/*'
.get('/*')
nPut = nPut.filteringRequestBody (body) -> '*'
.filteringPath (path) -> '/*'
.put('/*', '*')
nPost = nPost.filteringRequestBody (body) -> '*'
.filteringPath (path) -> '/*'
.post('/*', '*')
[nGet, nPut, nPost] = [nGet, nPut, nPost].map (n) ->
n = n.times(numTimes)
if makeTimeout
return n.socketDelay(31000)
else
return n
if makeTimeout
[nGet, nPut, nPost].forEach (n) -> n.reply(200, 'Timed out')
else
nGet.replyWithError({message:'Monkey GET error!', code: errorCode})
nPut.replyWithError({message:'Monkey PUT error!', code: errorCode})
nPost.replyWithError({message:'Monkey POST error!', code: errorCode})
@goHome: ->
nock.restore()
nock.cleanAll()
window.ChaosMonkey = ChaosMonkey
module.exports = ChaosMonkey