Permalink
Browse files

more testing, much needed to build

  • Loading branch information...
1 parent 1d1b017 commit e4867dc8fdb3b7f4d8e874549c5b6e7e604f0255 @supershabam committed Apr 25, 2013
Showing with 73 additions and 49 deletions.
  1. +0 −1 .npmignore
  2. +10 −10 Gruntfile.coffee
  3. +2 −2 LICENSE
  4. +3 −2 package.json
  5. +1 −1 src/net-cluster.coffee
  6. +1 −1 tasks/mocha.coffee
  7. +29 −0 test/net-cluster.port0.test.coffee
  8. +27 −0 test/net-cluster.port9002.test.coffee
  9. +0 −32 test/uniqueports.coffee
View
@@ -1,2 +1 @@
-src
node_modules
View
@@ -17,22 +17,22 @@ module.exports = (grunt) ->
src: "lib"
dest: "lib-cov"
mocha:
- options:
- args: [
- "test/"
- ]
- default:
+ net_cluster_0:
+ options:
+ args: ["test/net-cluster.port0.test.coffee"]
reporter: "list"
console: true
- cover:
- reporter: "html-cov"
- cover: true
- file: "cover.html"
+ net_cluster_9002:
+ options:
+ args: ["test/net-cluster.port9002.test.coffee"]
+ reporter: "list"
+ console: true
+
grunt.loadNpmTasks("grunt-contrib-clean")
grunt.loadNpmTasks("grunt-contrib-coffee")
grunt.loadTasks("tasks")
grunt.registerTask "run:cover", ["coffee", "jscoverage", "mocha:cover", "open-cover"]
- grunt.registerTask "run:test", ["coffee", "mocha:default"]
+ grunt.registerTask "run:test", ["coffee", "mocha:net_cluster_0", "mocha:net_cluster_9002"]
grunt.registerTask "default", ["run:test"]
View
@@ -1,6 +1,6 @@
(The MIT License)
-Copyright (c) 2012 Ian Hansen (//github.com/supershabam)
+Copyright (c) 2013 Ian Hansen (//github.com/supershabam)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
-SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -24,7 +24,7 @@
"license": "MIT",
"dependencies": {},
"devDependencies": {
- "clusterhub": "0.2.1",
+ "clusterhub": "~0.2.1",
"mocha": "~1.9.0",
"coffee-script": "~1.6.2",
"grunt": "~0.4.1",
@@ -33,6 +33,7 @@
"grunt-contrib-clean": "~0.4.1",
"sinon-chai": "~2.4.0",
"chai": "~1.5.0",
- "sinon": "~1.6.0"
+ "sinon": "~1.6.0",
+ "node-uuid": "~1.4.0"
}
}
View
@@ -41,7 +41,7 @@ class Server extends net.Server
# overrides node's listen method with one that makes me happy
listen: (args...)=>
# skip this override if possible
- return super args unless utils.shouldDoIntercept args
+ return super unless utils.shouldDoIntercept args
# emulate overridden listen callback functionality since we're doing our own thing now
lastArg = args[args.length - 1]
View
@@ -45,7 +45,7 @@ module.exports = (grunt)->
if @data.colors
args.push "--colors"
if options.args?.length
- args.concat options.args
+ args = args.concat options.args
# run mocha
mocha = spawn bin, args, {env: env}
@@ -0,0 +1,29 @@
+##
+# because we're forking, we must run this test by itself
+
+net = require "#{LIB_ROOT}/net-cluster"
+cluster = require "cluster"
+clusterhub = require "clusterhub"
+
+describe "listening on port 0 in cluster", ->
+ it "should get two unique ports", (done)->
+ count = 2
+ ports = {}
+ if cluster.isMaster
+ clusterhub.on "listening", (port)->
+ return unless cluster.isMaster
+ if ports[port]
+ clusterhub.destroy()
+ return done new Error("allocated the same port")
+ ports[port] = true
+ count--
+ if count is 0
+ clusterhub.destroy()
+ done()
+ for i in [0...count]
+ cluster.fork()
+ else
+ server = net.createServer()
+ server.listen 0, ->
+ port = server.address().port
+ clusterhub.emit "listening", port
@@ -0,0 +1,27 @@
+##
+# because we're forking, we must run this test by itself
+
+net = require "#{LIB_ROOT}/net-cluster"
+cluster = require "cluster"
+clusterhub = require "clusterhub"
+
+describe "listening on port 9002 in cluster", ->
+ it "should get two successful workers", (done)->
+ count = 2
+ if cluster.isMaster
+ clusterhub.on "listening", (port)->
+ return unless cluster.isMaster
+ unless port is 9002
+ clusterhub.destroy()
+ return done new Error("listening on wrong port")
+ count--
+ if count is 0
+ clusterhub.destroy()
+ done()
+ for i in [0...count]
+ cluster.fork()
+ else
+ server = net.createServer()
+ server.listen 9002, ->
+ port = server.address().port
+ clusterhub.emit "listening", port
View
@@ -1,32 +0,0 @@
-net = require '../index'
-cluster = require 'cluster'
-hub = require 'clusterhub'
-workerCount = 2
-
-describe 'net', ->
- it 'should be magic', (done) ->
- if cluster.isMaster
- isDone = false
- usedPorts = {}
-
- # ensure ports are allocated and that they are unique
- hub.on 'allocate:port', (port) ->
- if usedPorts[port] and !isDone
- isDone = true
- return done("tried allocating #{port} twice")
- usedPorts[port] = true
-
- if Object.keys(usedPorts).length is workerCount
- callback = ->
- if !isDone
- return done()
- setTimeout callback, 200
-
- for i in [1..workerCount]
- cluster.fork()
- else
- server = net.createServer()
- server.listen 0, ->
- port = server.address().port
- hub.emit 'allocate:port', port
-

0 comments on commit e4867dc

Please sign in to comment.