Starting to test queue class

This commit is contained in:
Michael Schmatz 2014-01-24 17:25:02 -08:00
parent c00321208d
commit 6f2461c8be
2 changed files with 32 additions and 5 deletions

View file

@ -12,7 +12,6 @@ queueClient = null
simulationQueue = null simulationQueue = null
module.exports.setupRoutes = (app) -> module.exports.setupRoutes = (app) ->
generateQueueInstance()
###queueClient.registerQueue "simulationQueue", {}, (err,data) -> ###queueClient.registerQueue "simulationQueue", {}, (err,data) ->
simulationQueue = data simulationQueue = data
simulationQueue.subscribe 'message', (err, data) -> simulationQueue.subscribe 'message', (err, data) ->
@ -24,17 +23,20 @@ module.exports.setupRoutes = (app) ->
generateQueueInstance = ->
module.exports.generateQueueClient = ->
if config.isProduction if config.isProduction
queueClient = new SQSQueueClient() queueClient = new SQSQueueClient()
else else
queueClient = new MongoQueueClient() queueClient = new MongoQueueClient()
return queueClient
class SQSQueueClient extends AbstractQueueClient class SQSQueueClient
constructor: -> constructor: ->
@configure() @configure()
@sqs = @generateSQSInstance() @sqs = @generateSQSInstance()
###Public API### ###Public API###
registerQueue: (queueName, options, callback) -> registerQueue: (queueName, options, callback) ->
#returns new queue in data argument of callback #returns new queue in data argument of callback
@ -89,7 +91,7 @@ class SQSQueue extends events.EventEmitter
class MongoQueueClient extends AbstractQueueClient class MongoQueueClient
constructor: -> constructor: ->
@configure() @configure()
@createMongoConnection() @createMongoConnection()
@ -155,7 +157,7 @@ class MongoQueue extends events.EventEmitter
queue: @queueName queue: @queueName
scheduledVisibilityTime: Date.now() + (delaySeconds * 1000) scheduledVisibilityTime: Date.now() + (delaySeconds * 1000)
messageToSend.save (err) => messageToSend.save (err,data) =>
if err? then @emit 'error',err,data else @emit 'sent',err, data if err? then @emit 'error',err,data else @emit 'sent',err, data
callback? err,data callback? err,data

25
server/queue.spec.coffee Normal file
View file

@ -0,0 +1,25 @@
queues = require './queue'
config = require '../server_config.js'
describe 'Queue', ->
describe 'construction interface', ->
it 'should construct a MongoQueueClient if not in production', ->
config.isProduction = false
queue = queues.generateQueueClient()
expect(queue.constructor.name).toEqual 'MongoQueueClient'
it 'should construct an SQSQueueClient if in production', ->
config.isProduction = true
queue = queues.generateQueueClient()
expect(queue.constructor.name).toEqual 'SQSQueueClient'
describe 'registerQueue', ->
mongoQueueClient = null
sqsQueueClient = null
beforeEach ->
config.isProduction = false
mongoQueueClient = queues.generateQueueClient()
config.isProduction = true
sqsQueueClient = queues.generateQueueClient()
it 'should generate the correct type of queue', ->
mongoQueueClient.registerQueue "TestQueue", {}, (err, data) ->
expect(data.constructor.name).toEqual 'MongoQueue'