tcp-reconnect-test.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * nssocket-test.js : namespace socket unit test for TCP
  3. *
  4. * (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
  5. *
  6. */
  7. var assert = require('assert'),
  8. fs = require('fs'),
  9. net = require('net'),
  10. path = require('path'),
  11. vows = require('vows'),
  12. NsSocket = require('../lib/nssocket').NsSocket;
  13. var TCP_PORT = 30105;
  14. var tcpServer = net.createServer(),
  15. tcpOpt;
  16. tcpOpt = {
  17. type : 'tcp4',
  18. delimiter: '.}',
  19. reconnect: true,
  20. retryInterval: 1000
  21. };
  22. tcpServer.listen(TCP_PORT);
  23. vows.describe('nssocket/tcp/reconnect').addBatch({
  24. "When using NsSocket with TCP": {
  25. topic: new NsSocket(tcpOpt),
  26. "the connect() method": {
  27. topic: function (outbound) {
  28. var that = this;
  29. tcpServer.on('connection', this.callback.bind(null, null, outbound));
  30. outbound.connect(TCP_PORT);
  31. },
  32. "should actually connect": function (_, outbound, inbound) {
  33. assert.instanceOf(outbound, NsSocket);
  34. assert.instanceOf(inbound, net.Socket);
  35. },
  36. "when the server closes": {
  37. topic: function (outbound, inbound) {
  38. outbound.once('close', this.callback.bind(this, null, outbound));
  39. tcpServer.close();
  40. inbound.destroy();
  41. },
  42. "and then restarts": {
  43. topic: function (outbound) {
  44. tcpServer = net.createServer();
  45. tcpServer.listen(TCP_PORT);
  46. tcpServer.on('connection', this.callback.bind(null, null, outbound));
  47. },
  48. "the socket should reconnect correctly": function (_, outbound, inbound) {
  49. assert.instanceOf(outbound, NsSocket);
  50. assert.instanceOf(inbound, net.Socket);
  51. },
  52. "the on() method": {
  53. topic: function (outbound, inbound) {
  54. outbound.on('data.}here.}is', this.callback.bind(outbound, null));
  55. inbound.write(JSON.stringify(['here', 'is', 'something.']) + '\n');
  56. },
  57. "should handle namespaced events": function (_, data) {
  58. assert.isArray(this.event);
  59. assert.lengthOf(this.event, 3);
  60. assert.isString(this.event[0]);
  61. assert.isString(this.event[1]);
  62. assert.isString(this.event[2]);
  63. assert.isString(data);
  64. assert.equal(this.event[0], 'data');
  65. assert.equal(this.event[1], 'here');
  66. assert.equal(this.event[2], 'is');
  67. assert.equal(data, 'something.');
  68. }
  69. }
  70. }
  71. }
  72. }
  73. }
  74. }).export(module);