From d5e680fe26a5d9bc8a1b8509d8a67b9ffa592403 Mon Sep 17 00:00:00 2001 From: Joseph Gentle Date: Thu, 7 Apr 2011 16:24:09 +1000 Subject: [PATCH] Let the OS pick a port for the tests to use --- test/client-opstream.coffee | 8 +++----- test/client.coffee | 6 ++---- test/integration.coffee | 5 ++--- test/rest.coffee | 31 +++++++++++++++---------------- test/socketio.coffee | 6 ++---- 5 files changed, 24 insertions(+), 32 deletions(-) diff --git a/test/client-opstream.coffee b/test/client-opstream.coffee index 36fc30fd..fe8a1ec1 100644 --- a/test/client-opstream.coffee +++ b/test/client-opstream.coffee @@ -7,8 +7,6 @@ OpStream = require('../src/client/opstream').OpStream helpers = require './helpers' -port = 8765 - # Client stream tests module.exports = testCase { setUp: (callback) -> @@ -22,14 +20,14 @@ module.exports = testCase { @model = server.createModel options @server = server options, @model - @server.listen port, => - @ds = new OpStream 'localhost', port + @server.listen => + @ds = new OpStream 'localhost', @server.address().port callback() tearDown: (callback) -> @ds.disconnect() - # Its important the port has closed before the next test is run. + # The port should be closed before the next test is run. @server.on 'close', callback @server.close() diff --git a/test/client.coffee b/test/client.coffee index f53beb18..130ba7e3 100644 --- a/test/client.coffee +++ b/test/client.coffee @@ -9,8 +9,6 @@ client = require '../src/client' makePassPart = require('./helpers').makePassPart -port = 8765 - module.exports = testCase { setUp: (callback) -> @name = 'testingdoc' @@ -24,8 +22,8 @@ module.exports = testCase { @model = server.createModel options @server = server options, @model - @server.listen port, => - @c = new client.Connection 'localhost', port + @server.listen => + @c = new client.Connection 'localhost', @server.address().port callback() tearDown: (callback) -> diff --git a/test/integration.coffee b/test/integration.coffee index 3c7b0e71..cadb68d5 100644 --- a/test/integration.coffee +++ b/test/integration.coffee @@ -12,8 +12,6 @@ types = require '../src/types' client = require '../src/client' -port = 8765 - module.exports = testCase { setUp: (callback) -> # This is needed for types.text.generateRandomOp @@ -21,7 +19,8 @@ module.exports = testCase { @name = 'testingdoc' @server = server {db: {type: 'memory'}} - @server.listen port, => + @server.listen => + port = @server.address().port @c1 = new client.Connection 'localhost', port @c2 = new client.Connection 'localhost', port diff --git a/test/rest.coffee b/test/rest.coffee index b017d314..cd1868e3 100644 --- a/test/rest.coffee +++ b/test/rest.coffee @@ -9,12 +9,9 @@ server = require '../src/server' helpers = require './helpers' -# This test creates a server listening on a local port. -port = 8765 - # Async fetch. Aggregates whole response and sends to callback. # Callback should be function(response, data) {...} -fetch = (method, path, postData, callback) -> +fetch = (method, port, path, postData, callback) -> request = http.request {method:method, path:path, host: 'localhost', port:port}, (response) -> data = '' response.on 'data', (chunk) -> data += chunk @@ -45,7 +42,9 @@ module.exports = testCase { try @model = server.createModel options @server = server options, @model - @server.listen port, callback + @server.listen => + @port = @server.address().port + callback() catch e console.log e.stack throw e @@ -55,56 +54,56 @@ module.exports = testCase { @server.close() 'return 404 when on GET on a random URL': (test) -> - fetch 'GET', "/doc/#{@name}", null, (res, data) -> + fetch 'GET', @port, "/doc/#{@name}", null, (res, data) -> test.strictEqual(res.statusCode, 404) test.done() 'GET a document returns the document snapshot': (test) -> helpers.applyOps @model, @name, 0, [{type: 'simple'}, {position: 0, text: 'Hi'}], (error, _) => - fetch 'GET', "/doc/#{@name}", null, (res, data) -> + fetch 'GET', @port, "/doc/#{@name}", null, (res, data) -> test.strictEqual(res.statusCode, 200) test.deepEqual data, {v:2, type:'simple', snapshot:{str:'Hi'}} test.done() 'POST a document in the DB returns 200 OK': (test) -> - fetch 'POST', "/doc/#{@name}?v=0", {type:'simple'}, (res, data) => + fetch 'POST', @port, "/doc/#{@name}?v=0", {type:'simple'}, (res, data) => test.strictEqual res.statusCode, 200 test.deepEqual data, {v:0} - fetch 'POST', "/doc/#{@name}?v=1", {position: 0, text: 'Hi'}, (res, data) => + fetch 'POST', @port, "/doc/#{@name}?v=1", {position: 0, text: 'Hi'}, (res, data) => test.strictEqual res.statusCode, 200 test.deepEqual data, {v:1} - fetch 'GET', "/doc/#{@name}", null, (res, data) -> + fetch 'GET', @port, "/doc/#{@name}", null, (res, data) -> test.strictEqual res.statusCode, 200 test.deepEqual data, {v:2, type:'simple', snapshot:{str: 'Hi'}} test.done() 'POST a document with no version returns 400': (test) -> - fetch 'POST', "/doc/#{@name}", {type:'simple'}, (res, data) -> + fetch 'POST', @port, "/doc/#{@name}", {type:'simple'}, (res, data) -> test.strictEqual res.statusCode, 400 test.done() 'POST a document with invalid JSON returns 400': (test) -> - fetch 'POST', "/doc/#{@name}?v=0", 'invalid>{json', (res, data) -> + fetch 'POST', @port, "/doc/#{@name}?v=0", 'invalid>{json', (res, data) -> test.strictEqual res.statusCode, 400 test.done() 'DELETE deletes a document': (test) -> @model.applyOp @name, {v:0, op:{type:'simple'}}, (error, newVersion) => test.ifError(error) - fetch 'DELETE', "/doc/#{@name}", null, (res, data) -> + fetch 'DELETE', @port, "/doc/#{@name}", null, (res, data) -> test.strictEqual res.statusCode, 200 test.done() 'DELETE returns a 404 message if you delete something that doesn\'t exist': (test) -> - fetch 'DELETE', "/doc/#{@name}", null, (res, data) -> + fetch 'DELETE', @port, "/doc/#{@name}", null, (res, data) -> test.strictEqual res.statusCode, 404 test.done() 'DELETE doesnt work if you dont select it in the options': (test) -> s = server {db: {type: 'memory'}}, @model - p = port + 1 - s.listen p, => + s.listen => + p = s.address().port @model.applyOp @name, {v:0, op:{type:'simple'}}, (error, newVersion) => test.ifError(error) request = http.request {method:'DELETE', path:"/doc/#{@name}", host: 'localhost', port:p}, (res) => diff --git a/test/socketio.coffee b/test/socketio.coffee index 3217a559..33c71e97 100644 --- a/test/socketio.coffee +++ b/test/socketio.coffee @@ -1,7 +1,5 @@ # Tests for the server's socketio interface -port = 8765 - testCase = require('nodeunit').testCase assert = require 'assert' clientio = require('../thirdparty/Socket.io-node-client/lib/io-client').io @@ -42,11 +40,11 @@ module.exports = testCase { @model = server.createModel options @server = server options, @model - @server.listen port, => + @server.listen => @name = 'testingdoc' # Make a new socket.io socket connected to the server's stream interface - @socket = new clientio.Socket 'localhost', {port: port, resource: 'socket.io'} + @socket = new clientio.Socket 'localhost', {port: @server.address().port, resource: 'socket.io'} @socket.connect() @socket.on 'connect', callback catch e