Add/cleanup several tests

This commit is contained in:
Tom Robinson 2013-07-26 15:56:11 -07:00
parent 42400aa62c
commit 54f1cae1b6
4 changed files with 116 additions and 49 deletions

View File

@ -1,2 +1,5 @@
http:
port: 6789
port: 6789
logging:
file: null

View File

@ -77,13 +77,20 @@ exports.stompPublish = (host, destination, push_id, message) ->
stomp.disconnect()
exports.start = (config, callback) ->
# Configure logging
if config.logging.file
winston.remove winston.transports.Console
winston.add winston.transports.File,
filename: config.logging.file
winston.level = (config.logging.level or "info").toLowerCase()
# EventEmitter used to keep track of subscriptions
subscriptions = new EventEmitter()
# Create one Socket.io server
# Create Socket.io server
{ app, io, server } = createSocketIOServer(config, subscriptions)
# # Create one or more STOMP connections
# Create STOMP connection
stomp = createStompConnection(config, config.stomp.hosts[0], subscriptions)
stomp.on "connected", ->
@ -96,20 +103,8 @@ exports.start = (config, callback) ->
process.nextTick ->
callback?(null)
exports.main = (args) ->
exports.start exports.loadConfiguration args[0]
if require.main is module
# Get the configuration overlays
config = exports.loadConfiguration process.argv[2]
# Configure logging
winston.remove winston.transports.Console
logOptions =
level: (config.logging.level or "info").toLowerCase()
if config.logging.file
logOptions.filename = config.logging.file
winston.add winston.transports.File, logOptions
else
logOptions.colorize = true
winston.add winston.transports.Console, logOptions
# Start the server
exports.start config
exports.main process.argv[2..]

View File

@ -4,7 +4,8 @@
"description": "",
"main": "index.js",
"scripts": {
"test": "./node_modules/.bin/mocha --compilers coffee:coffee-script test"
"start": "node_modules/.bin/coffee index.coffee",
"test": "node_modules/.bin/mocha --compilers coffee:coffee-script test"
},
"repository": "",
"author": "",
@ -24,6 +25,7 @@
"chai": "~1.7.2",
"chai-as-promised": "~3.3.1",
"q": "~0.9.6",
"socket.io-client": "~0.9.16"
"socket.io-client": "~0.9.16",
"node-uuid": "~1.4.0"
}
}

View File

@ -1,43 +1,110 @@
assert = require "assert"
require("mocha-as-promised")()
chai = require "chai"
chai.use require "chai-as-promised"
{ expect } = chai
Q = require "q"
activepush = require "../index"
io = require "socket.io-client"
merge = require "deepmerge"
uuid = require "node-uuid"
# Delay before checking received messages to ensure all messages get delivered.
# Increase this value if tests are failiing non-deterministically.
# TODO: better way to detect all messages have been delivered?
WAIT_TIME = 100
config = activepush.loadConfiguration "test"
# config.logging.level = "DEBUG"
connectSocketIO = (push_id, ready) ->
socket = io.connect "http://localhost:#{config.http.port}"
configureActivePush = (config) ->
deferred = Q.defer()
server = activepush.start config, ->
deferred.resolve
config: config
server: server
sendMessage: (push_id, message) ->
activepush.stompPublish config.stomp.hosts[0], config.stomp.inbox, push_id, message
deferred.promise
configureSocketIO = (port, push_id) ->
deferred = Q.defer()
socket = io.connect "http://localhost:#{port}", "force new connection": true
socket.on "connect", ->
socket.emit "subscribe", push_id
ready socket
deferred.resolve collectMessages(socket)
deferred.promise
sendStompMessage = (push_id, message) ->
activepush.stompPublish config.stomp.hosts[0], config.stomp.inbox, push_id, message
# Helper to collect messages into an array
collectMessages = (socket) ->
messages = []
socket.on "message", (message) ->
messages.push message
messages
describe "Single ActivePush instance", ->
server = null
before (done) ->
server = activepush.start config, ->
done()
after (done) ->
server.stop ->
done()
ap = null
before ->
# Use a unique inbox in case we're running multiple tests using the same ActiveMQ server concurrently etc
inbox = "/topic/activepush-test-"+uuid.v1()
configureActivePush(merge(config, stomp:inbox:inbox)).then (activePush) ->
ap = activePush
after (cb) ->
ap.server.stop cb
it "should relay the correct messages to a single client", (done) ->
expectedMessages = ["yes1"]
actualMessages = []
it "should not buffer messages (treat as transient)", ->
ap.sendMessage("my_push_id", "no")
configureSocketIO(ap.config.http.port, "my_push_id").then (receivedMessages) ->
Q.delay(WAIT_TIME).then ->
expect(receivedMessages).to.deep.equal []
sendStompMessage "my_push_id", "no1"
sendStompMessage "other_push_id", "no2"
it "should relay the correct messages to a single client", ->
configureSocketIO(ap.config.http.port, "my_push_id").then (receivedMessages) ->
ap.sendMessage "my_push_id", "yes"
ap.sendMessage "other_push_id", "no"
Q.delay(WAIT_TIME).then ->
expect(receivedMessages).to.deep.equal ["yes"]
connectSocketIO "my_push_id", (socket) ->
socket.on "message", (message) ->
actualMessages.push message
it "should relay the correct messages to multiple clients", (done) ->
Q.all(for index in [0..1]
configureSocketIO(ap.config.http.port, "my_push_id")
).then (allReceivedMessages) ->
ap.sendMessage "my_push_id", "yes"
ap.sendMessage "other_push_id", "no"
Q.delay(WAIT_TIME).then ->
expect(allReceivedMessages).to.deep.equal [["yes"], ["yes"]]
sendStompMessage "my_push_id", "yes1"
sendStompMessage "other_push_id", "no3"
describe "Multiple ActivePush instances", ->
aps = null
before ->
# Use a unique inbox in case we're running multiple tests using the same ActiveMQ server concurrently etc
inbox = "/topic/activepush-test-"+uuid.v1()
Q.all(for index in [0..1]
configureActivePush(merge(config,
stomp:inbox: inbox
http:port: config.http.port + index
))
).then (activePushArr) ->
aps = activePushArr
after ->
Q.all(for ap in aps
deferred = Q.defer()
ap.server.stop deferred.resolve
deferred.promise
)
# Gross...
setTimeout ->
assert.deepEqual expectedMessages, actualMessages
done()
, 1000
it "should relay the correct messages to multiple clients", (done) ->
Q.all(for ap in aps
configureSocketIO(ap.config.http.port, "my_push_id")
).then (allReceivedMessages) ->
expected = []
for ap, index in aps
expected.push "yes#{index}"
ap.sendMessage "my_push_id", "yes#{index}"
ap.sendMessage "other_push_id", "no#{index}"
Q.delay(WAIT_TIME).then ->
for messages in allReceivedMessages
expect(messages).to.have.members(expected)
expect(messages.length).to.equal(expected.length)