Skip to content

Commit

Permalink
Tests for disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Evan Tahler committed Apr 12, 2014
1 parent 33c9711 commit 4d8bf1b
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 19 deletions.
5 changes: 3 additions & 2 deletions public/javascript/actionheroClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
if(self.room != null){
self.send({event: 'roomChange', room: self.room});
}
callback(null, details);
if(typeof callback === 'function'){ callback(null, details); }
});
}, self.options.connectionDelay);
});
Expand Down Expand Up @@ -130,7 +130,7 @@
self.emit('welcome', message);
} else if(message.context === 'api'){
if(message.status === "ClientDisconnect"){
client.disconnect();
self.disconnect();
}
self.emit('api', message);
}
Expand Down Expand Up @@ -233,6 +233,7 @@
actionheroClient.prototype.disconnect = function(){
this.state = 'disconnected';
this.client.disconnect();
this.emit('disconnected');
}

/////////////
Expand Down
2 changes: 1 addition & 1 deletion public/javascript/actionheroClient.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 41 additions & 14 deletions test/servers/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@ function makeSocketRequest(thisClient, message, cb){
thisClient.write(message + '\r\n');
}

var connectClients = function(callback){
setTimeout(function(){
callback();
}, 1000);

client = net.connect(api.config.servers.socket.port, function(){
client.setEncoding('utf8');
});
client2 = net.connect(api.config.servers.socket.port, function(){
client2.setEncoding('utf8');
});
client3 = net.connect(api.config.servers.socket.port, function(){
client3.setEncoding('utf8');
});
}

describe('Server: Socket', function(){

before(function(done){
actionhero.start(function(err, a){
api = a;

setTimeout(function(){
done();
}, 1000);

client = net.connect(api.config.servers.socket.port, function(){
client.setEncoding('utf8');
});
client2 = net.connect(api.config.servers.socket.port, function(){
client2.setEncoding('utf8');
});
client3 = net.connect(api.config.servers.socket.port, function(){
client3.setEncoding('utf8');
});
connectClients(done)
});
});

Expand Down Expand Up @@ -366,4 +369,28 @@ describe('Server: Socket', function(){

});

describe('disconnect', function(){
after(function(done){
connectClients(done);
})

it('server can disconnect a client', function(done){
makeSocketRequest(client, 'status', function(response){
response.id.should.equal('test-server');
client.readable.should.equal(true)
client.writable.should.equal(true)

for(id in api.connections.connections){
api.connections.connections[id].destroy();
}

setTimeout(function(){
client.readable.should.equal(false)
client.writable.should.equal(false)
done();
}, 100)
});
});
});

});
33 changes: 31 additions & 2 deletions test/servers/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,19 @@ describe('Server: Web Socket', function(){

describe('disconnect', function(){

it('can disconnect', function(done){
beforeEach(function(done){
client_1 = new actionheroClientPrototype({host: socketURL, faye: faye});
client_2 = new actionheroClientPrototype({host: socketURL, faye: faye});
client_3 = new actionheroClientPrototype({host: socketURL, faye: faye});

client_1.connect();
client_2.connect();
client_3.connect();

setTimeout(done, 1000);
});

it('client can disconnect', function(done){
api.servers.servers.websocket.connections().length.should.equal(3);
client_1.disconnect();
client_2.disconnect();
Expand All @@ -245,6 +257,23 @@ describe('Server: Web Socket', function(){
}, 500);
});

})
it('can be sent disconnect events from the server', function(done){
this.timeout = 5000
client_1.detailsView(function(response){
response.data.remoteIP.should.equal('127.0.0.1');

for(id in api.connections.connections){
api.connections.connections[id].destroy();
}

client_1.detailsView(function(response){
throw new Error("should not get responst")
});

setTimeout(done, 4000)
});
});

});

});

0 comments on commit 4d8bf1b

Please sign in to comment.