tcp-test.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 = 30103;
  14. var tcpServer = net.createServer(),
  15. tcpOpt;
  16. tcpOpt = {
  17. type : 'tcp4',
  18. delimiter: '.}'
  19. };
  20. tcpServer.listen(TCP_PORT);
  21. vows.describe('nssocket/tcp').addBatch({
  22. "When using NsSocket with TCP": {
  23. topic: new NsSocket(tcpOpt),
  24. "should create a wrapped socket": function (outbound) {
  25. assert.instanceOf(outbound, NsSocket);
  26. },
  27. "should have the proper configuration settings": function (outbound) {
  28. assert.equal(outbound._type, tcpOpt.type);
  29. assert.equal(outbound._delimiter, tcpOpt.delimiter);
  30. },
  31. "the connect() method": {
  32. topic: function (outbound) {
  33. var that = this;
  34. tcpServer.on('connection', this.callback.bind(null, null, outbound));
  35. outbound.connect(TCP_PORT);
  36. },
  37. "should actually connect": function (_, outbound, inbound) {
  38. assert.instanceOf(outbound, NsSocket);
  39. assert.instanceOf(inbound, net.Socket);
  40. },
  41. "the on() method": {
  42. topic: function (outbound, inbound) {
  43. outbound.on('data.}here.}is', this.callback.bind(outbound, null));
  44. inbound.write(JSON.stringify(['here', 'is', 'something.']) + '\n');
  45. },
  46. "should handle namespaced events": function (_, data) {
  47. assert.isArray(this.event);
  48. assert.lengthOf(this.event, 3);
  49. assert.isString(this.event[0]);
  50. assert.isString(this.event[1]);
  51. assert.isString(this.event[2]);
  52. assert.isString(data);
  53. assert.equal(this.event[0], 'data');
  54. assert.equal(this.event[1], 'here');
  55. assert.equal(this.event[2], 'is');
  56. assert.equal(data, 'something.');
  57. },
  58. "once idle": {
  59. topic: function (_, outbound, inbound) {
  60. outbound.once('idle', this.callback.bind(null, null, outbound, inbound));
  61. outbound.setIdle(100);
  62. },
  63. "it should emit `idle`": function (_, outbound, inbound) {
  64. assert.isNull(_);
  65. },
  66. "the send() method": {
  67. topic: function (outbound, inbound) {
  68. inbound.on('data', this.callback.bind(null, null, outbound, inbound));
  69. outbound.send(['hello','world'], { some: "json", data: 123 });
  70. },
  71. "we should see it on the other end": function (_, outbound, wraped, data) {
  72. assert.isObject(data);
  73. arr = JSON.parse(data.toString());
  74. assert.lengthOf(arr, 3);
  75. assert.equal(arr[0], 'hello');
  76. assert.equal(arr[1], 'world');
  77. assert.deepEqual(arr[2], { some: "json", data: 123 });
  78. },
  79. "the end() method": {
  80. topic: function (outbound, inbound) {
  81. outbound.on('close', this.callback.bind(null, null, outbound, inbound));
  82. inbound.end();
  83. },
  84. "should close without errors": function (_, _, _, err) {
  85. assert.isUndefined(err);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. }
  93. }).export(module);