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