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