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