serialize.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // Test binary messages on websockets.
  3. // Only works on slimer for now, due to old websocket impl in phantomjs.
  4. //
  5. casper.notebook_test(function () {
  6. // create EchoBuffers target on js-side.
  7. // it just captures and echos comm messages.
  8. this.then(function () {
  9. var success = this.evaluate(function () {
  10. IPython._msgs = [];
  11. var EchoBuffers = function(comm) {
  12. this.comm = comm;
  13. this.comm.on_msg($.proxy(this.on_msg, this));
  14. };
  15. EchoBuffers.prototype.on_msg = function (msg) {
  16. IPython._msgs.push(msg);
  17. this.comm.send(msg.content.data, {}, {}, msg.buffers);
  18. };
  19. IPython.notebook.kernel.comm_manager.register_target("echo", function (comm) {
  20. return new EchoBuffers(comm);
  21. });
  22. return true;
  23. });
  24. this.test.assertEquals(success, true, "Created echo comm target");
  25. });
  26. // Create a similar comm that captures messages Python-side
  27. this.then(function () {
  28. var index = this.append_cell([
  29. "import os",
  30. "from IPython.kernel.comm import Comm",
  31. "comm = Comm(target_name='echo')",
  32. "msgs = []",
  33. "def on_msg(msg):",
  34. " msgs.append(msg)",
  35. "comm.on_msg(on_msg)"
  36. ].join('\n'), 'code');
  37. this.execute_cell(index);
  38. });
  39. // send a message with binary data
  40. this.then(function () {
  41. var index = this.append_cell([
  42. "buffers = [b'\\xFF\\x00', b'\\x00\\x01\\x02']",
  43. "comm.send(data='message 0', buffers=buffers)",
  44. "comm.send(data='message 1')",
  45. "comm.send(data='message 2', buffers=buffers)",
  46. ].join('\n'), 'code');
  47. this.execute_cell(index);
  48. });
  49. // wait for capture
  50. this.waitFor(function () {
  51. return this.evaluate(function () {
  52. return IPython._msgs.length >= 3;
  53. });
  54. });
  55. // validate captured buffers js-side
  56. this.then(function () {
  57. var msgs = this.evaluate(function () {
  58. return IPython._msgs;
  59. });
  60. this.test.assertEquals(msgs.length, 3, "Captured three comm messages");
  61. // check the messages came in the right order
  62. this.test.assertEquals(msgs[0].content.data, "message 0", "message 0 processed first");
  63. this.test.assertEquals(msgs[0].buffers.length, 2, "comm message 0 has two buffers");
  64. this.test.assertEquals(msgs[1].content.data, "message 1", "message 1 processed second");
  65. this.test.assertEquals(msgs[1].buffers.length, 0, "comm message 1 has no buffers");
  66. this.test.assertEquals(msgs[2].content.data, "message 2", "message 2 processed third");
  67. this.test.assertEquals(msgs[2].buffers.length, 2, "comm message 2 has two buffers");
  68. // extract attributes to test in evaluate,
  69. // because the raw DataViews can't be passed across
  70. var buf_info = function (message, index) {
  71. var buf = IPython._msgs[message].buffers[index];
  72. var data = {};
  73. data.byteLength = buf.byteLength;
  74. data.bytes = [];
  75. for (var i = 0; i < data.byteLength; i++) {
  76. data.bytes.push(buf.getUint8(i));
  77. }
  78. return data;
  79. };
  80. var msgs_with_buffers = [0, 2];
  81. for (var i = 0; i < msgs_with_buffers.length; i++) {
  82. msg_index = msgs_with_buffers[i];
  83. buf0 = this.evaluate(buf_info, msg_index, 0);
  84. buf1 = this.evaluate(buf_info, msg_index, 1);
  85. this.test.assertEquals(buf0.byteLength, 2, 'buf[0] has correct size in message '+msg_index);
  86. this.test.assertEquals(buf0.bytes, [255, 0], 'buf[0] has correct bytes in message '+msg_index);
  87. this.test.assertEquals(buf1.byteLength, 3, 'buf[1] has correct size in message '+msg_index);
  88. this.test.assertEquals(buf1.bytes, [0, 1, 2], 'buf[1] has correct bytes in message '+msg_index);
  89. }
  90. });
  91. // validate captured buffers Python-side
  92. this.then(function () {
  93. var index = this.append_cell([
  94. "assert len(msgs) == 3, len(msgs)",
  95. "bufs = msgs[0]['buffers']",
  96. "assert len(bufs) == len(buffers), bufs",
  97. "assert bufs[0].tobytes() == buffers[0], bufs[0]",
  98. "assert bufs[1].tobytes() == buffers[1], bufs[1]",
  99. "1",
  100. ].join('\n'), 'code');
  101. this.execute_cell(index);
  102. this.wait_for_output(index);
  103. this.then(function () {
  104. var out = this.get_output_cell(index);
  105. this.test.assertEquals(out.data['text/plain'], '1', "Python received buffers");
  106. });
  107. });
  108. });