Skip to content

Commit

Permalink
Merge pull request #29 from meteor/close-before-start
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoglan committed Sep 7, 2014
2 parents 5d7e9ef + 07217fe commit 308f930
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/faye/websocket/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var instance = {
},

close: function() {
if (this.readyState === API.OPEN) this.readyState = API.CLOSING;
if (this.readyState !== API.CLOSED) this.readyState = API.CLOSING;
this._driver.close();
},

Expand Down
46 changes: 46 additions & 0 deletions spec/faye/websocket/client_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,21 @@ var WebSocketSteps = test.asyncSteps({
this._ws.onclose = function() { resume(false) }
},

open_socket_and_close_it_fast: function(url, protocols, callback) {
var self = this

this._ws = new Client(url, protocols, {
ca: fs.readFileSync(__dirname + '/../../server.crt')
})

this._ws.onopen = function() { self._open = self._ever_opened = true }
this._ws.onclose = function() { self._open = false }

this._ws.close()

callback()
},

close_socket: function(callback) {
var self = this
this._ws.onclose = function() {
Expand All @@ -53,6 +68,21 @@ var WebSocketSteps = test.asyncSteps({
callback()
},

check_never_opened: function(callback) {
this.assert( !this._ever_opened )
callback()
},

check_readable: function(callback) {
this.assert( this._ws.readable )
callback()
},

check_not_readable: function(callback) {
this.assert( ! this._ws.readable )
callback()
},

check_protocol: function(protocol, callback) {
this.assertEqual( protocol, this._ws.protocol )
callback()
Expand Down Expand Up @@ -83,6 +113,10 @@ var WebSocketSteps = test.asyncSteps({
check_no_response: function(callback) {
this.assert( !this._message )
callback()
},

wait: function (ms, callback) {
setTimeout(callback, ms)
}
})

Expand All @@ -106,8 +140,10 @@ test.describe("Client", function() { with(this) {

it("can close the connection", function() { with(this) {
open_socket(socket_url, protocols)
check_readable()
close_socket()
check_closed()
check_not_readable()
}})

describe("in the OPEN state", function() { with(this) {
Expand Down Expand Up @@ -152,6 +188,16 @@ test.describe("Client", function() { with(this) {
check_no_response()
}})
}})

it("can be closed before connecting", function() { with(this) {
open_socket_and_close_it_fast(socket_url, protocols)
check_closed()
check_never_opened()
wait(10)
check_closed()
check_never_opened()
check_not_readable()
}})
}})

describe("with a plain-text server", function() { with(this) {
Expand Down

0 comments on commit 308f930

Please sign in to comment.