Skip to content

Commit

Permalink
Let the OS pick a port for the tests to use
Browse files Browse the repository at this point in the history
  • Loading branch information
josephg committed Apr 7, 2011
1 parent 0cdaf8e commit d5e680f
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 32 deletions.
8 changes: 3 additions & 5 deletions test/client-opstream.coffee
Expand Up @@ -7,8 +7,6 @@ OpStream = require('../src/client/opstream').OpStream

helpers = require './helpers'

port = 8765

# Client stream tests
module.exports = testCase {
setUp: (callback) ->
Expand All @@ -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()

Expand Down
6 changes: 2 additions & 4 deletions test/client.coffee
Expand Up @@ -9,8 +9,6 @@ client = require '../src/client'

makePassPart = require('./helpers').makePassPart

port = 8765

module.exports = testCase {
setUp: (callback) ->
@name = 'testingdoc'
Expand All @@ -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) ->
Expand Down
5 changes: 2 additions & 3 deletions test/integration.coffee
Expand Up @@ -12,16 +12,15 @@ types = require '../src/types'

client = require '../src/client'

port = 8765

module.exports = testCase {
setUp: (callback) ->
# This is needed for types.text.generateRandomOp
require './types/text'

@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
Expand Down
31 changes: 15 additions & 16 deletions test/rest.coffee
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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) =>
Expand Down
6 changes: 2 additions & 4 deletions 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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d5e680f

Please sign in to comment.