msgpack-tcp-test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * msgpack-tcp-test.js : namespace socket unit test for Messagepack encoder/decoder.
  3. *
  4. * (C) 2011, Charlie Robbins, Paolo Fragomeni, & the Contributors.
  5. *
  6. */
  7. var assert = require('assert'),
  8. fs = require('fs'),
  9. msgpack = require('msgpack'),
  10. net = require('net'),
  11. path = require('path'),
  12. vows = require('vows'),
  13. NsSocket = require('../lib/nssocket').NsSocket;
  14. var TCP_PORT = 30109;
  15. var tcpServer = net.createServer(),
  16. tcpOpt;
  17. tcpOpt = {
  18. type : 'tcp4',
  19. delimiter: '.}',
  20. encode: function (data) {
  21. return msgpack.pack(data).toString('binary');
  22. },
  23. decode: function (data) {
  24. return msgpack.unpack(new Buffer(data.toString('binary'), 'binary'));
  25. }
  26. };
  27. tcpServer.listen(TCP_PORT);
  28. vows.describe('nssocket/msgpack').addBatch({
  29. "When using NsSocket with msgpack encoder/decoder": {
  30. topic: new NsSocket(tcpOpt),
  31. "should create a wrapped socket": function (outbound) {
  32. assert.instanceOf(outbound, NsSocket);
  33. },
  34. "should have the proper configuration settings": function (outbound) {
  35. assert.equal(outbound._type, tcpOpt.type);
  36. assert.equal(outbound._delimiter, tcpOpt.delimiter);
  37. },
  38. "the connect() method": {
  39. topic: function (outbound) {
  40. var that = this;
  41. tcpServer.on('connection', this.callback.bind(null, null, outbound));
  42. outbound.connect(TCP_PORT);
  43. },
  44. "should actually connect": function (_, outbound, inbound) {
  45. assert.instanceOf(outbound, NsSocket);
  46. assert.instanceOf(inbound, net.Socket);
  47. },
  48. "the on() method": {
  49. topic: function (outbound, inbound) {
  50. outbound.on('data.}here.}is', this.callback.bind(outbound, null));
  51. inbound.write(tcpOpt.encode(['here', 'is', 'something.']) + '\n');
  52. },
  53. "should handle namespaced events": function (_, data) {
  54. assert.isArray(this.event);
  55. assert.lengthOf(this.event, 3);
  56. assert.isString(this.event[0]);
  57. assert.isString(this.event[1]);
  58. assert.isString(this.event[2]);
  59. assert.isString(data);
  60. assert.equal(this.event[0], 'data');
  61. assert.equal(this.event[1], 'here');
  62. assert.equal(this.event[2], 'is');
  63. assert.equal(data, 'something.');
  64. },
  65. "once idle": {
  66. topic: function (_, outbound, inbound) {
  67. outbound.once('idle', this.callback.bind(null, null, outbound, inbound));
  68. outbound.setIdle(100);
  69. },
  70. "it should emit `idle`": function (_, outbound, inbound) {
  71. assert.isNull(_);
  72. },
  73. "the send() method": {
  74. topic: function (outbound, inbound) {
  75. inbound.on('data', this.callback.bind(null, null, outbound, inbound));
  76. outbound.send(['hello','world'], { some: "json", data: 123 });
  77. },
  78. "we should see it on the other end": function (_, outbound, wraped, data) {
  79. assert.isObject(data);
  80. arr = tcpOpt.decode(data.toString());
  81. assert.lengthOf(arr, 3);
  82. assert.equal(arr[0], 'hello');
  83. assert.equal(arr[1], 'world');
  84. assert.deepEqual(arr[2], { some: "json", data: 123 });
  85. },
  86. "the end() method": {
  87. topic: function (outbound, inbound) {
  88. outbound.on('close', this.callback.bind(null, null, outbound, inbound));
  89. inbound.end();
  90. },
  91. "should close without errors": function (_, _, _, err) {
  92. assert.isUndefined(err);
  93. }
  94. }
  95. }
  96. }
  97. }
  98. }
  99. }
  100. }).export(module);