Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
If you decide to open a client connection but then close it before the
TCP connection succeeds, the websocket-driver change tested by this
commit will ensure that it will actually close, rather than ignoring the
close() method.

The change to API.close also seems correct to me, though I'm not sure
if there are any observable changes if you don't do it.
  • Loading branch information
glasser committed Aug 18, 2014
1 parent 29ef6b0 commit 07217fe
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 07217fe

Please sign in to comment.