##// END OF EJS Templates
Merge pull request #7780 from jasongrout/message-race...
Min RK -
r20601:22d57211 merge
parent child Browse files
Show More
@@ -41,6 +41,7 b' define(['
41 this.username = "username";
41 this.username = "username";
42 this.session_id = utils.uuid();
42 this.session_id = utils.uuid();
43 this._msg_callbacks = {};
43 this._msg_callbacks = {};
44 this._msg_queue = Promise.resolve();
44 this.info_reply = {}; // kernel_info_reply stored here after starting
45 this.info_reply = {}; // kernel_info_reply stored here after starting
45
46
46 if (typeof(WebSocket) !== 'undefined') {
47 if (typeof(WebSocket) !== 'undefined') {
@@ -854,7 +855,10 b' define(['
854 };
855 };
855
856
856 Kernel.prototype._handle_ws_message = function (e) {
857 Kernel.prototype._handle_ws_message = function (e) {
857 serialize.deserialize(e.data, $.proxy(this._finish_ws_message, this));
858 this._msg_queue = this._msg_queue.then(function() {
859 return serialize.deserialize(e.data);
860 }).then($.proxy(this._finish_ws_message, this))
861 .catch(utils.reject("Couldn't process kernel message", true));
858 };
862 };
859
863
860 Kernel.prototype._finish_ws_message = function (msg) {
864 Kernel.prototype._finish_ws_message = function (msg) {
@@ -30,7 +30,7 b' define(['
30 return msg;
30 return msg;
31 };
31 };
32
32
33 var _deserialize_binary = function(data, callback) {
33 var _deserialize_binary = function(data) {
34 /**
34 /**
35 * deserialize the binary message format
35 * deserialize the binary message format
36 * callback will be called with a message whose buffers attribute
36 * callback will be called with a message whose buffers attribute
@@ -39,28 +39,31 b' define(['
39 if (data instanceof Blob) {
39 if (data instanceof Blob) {
40 // data is Blob, have to deserialize from ArrayBuffer in reader callback
40 // data is Blob, have to deserialize from ArrayBuffer in reader callback
41 var reader = new FileReader();
41 var reader = new FileReader();
42 reader.onload = function () {
42 var promise = new Promise(function(resolve, reject) {
43 var msg = _deserialize_array_buffer(this.result);
43 reader.onload = function () {
44 callback(msg);
44 var msg = _deserialize_array_buffer(this.result);
45 };
45 resolve(msg);
46 };
47 });
46 reader.readAsArrayBuffer(data);
48 reader.readAsArrayBuffer(data);
49 return promise;
47 } else {
50 } else {
48 // data is ArrayBuffer, can deserialize directly
51 // data is ArrayBuffer, can deserialize directly
49 var msg = _deserialize_array_buffer(data);
52 var msg = _deserialize_array_buffer(data);
50 callback(msg);
53 return msg;
51 }
54 }
52 };
55 };
53
56
54 var deserialize = function (data, callback) {
57 var deserialize = function (data) {
55 /**
58 /**
56 * deserialize a message and pass the unpacked message object to callback
59 * deserialize a message and return a promise for the unpacked message
57 */
60 */
58 if (typeof data === "string") {
61 if (typeof data === "string") {
59 // text JSON message
62 // text JSON message
60 callback(JSON.parse(data));
63 return Promise.resolve(JSON.parse(data));
61 } else {
64 } else {
62 // binary message
65 // binary message
63 _deserialize_binary(data, callback);
66 return Promise.resolve(_deserialize_binary(data));
64 }
67 }
65 };
68 };
66
69
@@ -117,4 +120,4 b' define(['
117 serialize: serialize
120 serialize: serialize
118 };
121 };
119 return exports;
122 return exports;
120 }); No newline at end of file
123 });
@@ -51,7 +51,9 b' casper.notebook_test(function () {'
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='hi', buffers=buffers)"
54 "comm.send(data='message 0', buffers=buffers)",
55 "comm.send(data='message 1')",
56 "comm.send(data='message 2', buffers=buffers)",
55 ].join('\n'), 'code');
57 ].join('\n'), 'code');
56 this.execute_cell(index);
58 this.execute_cell(index);
57 });
59 });
@@ -59,7 +61,7 b' casper.notebook_test(function () {'
59 // wait for capture
61 // wait for capture
60 this.waitFor(function () {
62 this.waitFor(function () {
61 return this.evaluate(function () {
63 return this.evaluate(function () {
62 return IPython._msgs.length > 0;
64 return IPython._msgs.length >= 3;
63 });
65 });
64 });
66 });
65
67
@@ -68,14 +70,22 b' casper.notebook_test(function () {'
68 var msgs = this.evaluate(function () {
70 var msgs = this.evaluate(function () {
69 return IPython._msgs;
71 return IPython._msgs;
70 });
72 });
71 this.test.assertEquals(msgs.length, 1, "Captured comm message");
73 this.test.assertEquals(msgs.length, 3, "Captured three comm messages");
72 var buffers = msgs[0].buffers;
74
73 this.test.assertEquals(buffers.length, 2, "comm message has buffers");
75
76
77 // check the messages came in the right order
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");
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");
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");
74
84
75 // extract attributes to test in evaluate,
85 // extract attributes to test in evaluate,
76 // because the raw DataViews can't be passed across
86 // because the raw DataViews can't be passed across
77 var buf_info = function (index) {
87 var buf_info = function (message, index) {
78 var buf = IPython._msgs[0].buffers[index];
88 var buf = IPython._msgs[message].buffers[index];
79 var data = {};
89 var data = {};
80 data.byteLength = buf.byteLength;
90 data.byteLength = buf.byteLength;
81 data.bytes = [];
91 data.bytes = [];
@@ -85,18 +95,22 b' casper.notebook_test(function () {'
85 return data;
95 return data;
86 };
96 };
87
97
88 buf0 = this.evaluate(buf_info, 0);
98 var msgs_with_buffers = [0, 2];
89 buf1 = this.evaluate(buf_info, 1);
99 for (var i = 0; i < msgs_with_buffers.length; i++) {
90 this.test.assertEquals(buf0.byteLength, 2, 'buf[0] has correct size');
100 msg_index = msgs_with_buffers[i];
91 this.test.assertEquals(buf0.bytes, [255, 0], 'buf[0] has correct bytes');
101 buf0 = this.evaluate(buf_info, msg_index, 0);
92 this.test.assertEquals(buf1.byteLength, 3, 'buf[1] has correct size');
102 buf1 = this.evaluate(buf_info, msg_index, 1);
93 this.test.assertEquals(buf1.bytes, [0, 1, 2], 'buf[1] has correct bytes');
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);
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);
107 }
94 });
108 });
95
109
96 // validate captured buffers Python-side
110 // validate captured buffers Python-side
97 this.then(function () {
111 this.then(function () {
98 var index = this.append_cell([
112 var index = this.append_cell([
99 "assert len(msgs) == 1, len(msgs)",
113 "assert len(msgs) == 3, len(msgs)",
100 "bufs = msgs[0]['buffers']",
114 "bufs = msgs[0]['buffers']",
101 "assert len(bufs) == len(buffers), bufs",
115 "assert len(bufs) == len(buffers), bufs",
102 "assert bufs[0].bytes == buffers[0], bufs[0].bytes",
116 "assert bufs[0].bytes == buffers[0], bufs[0].bytes",
@@ -107,7 +121,7 b' casper.notebook_test(function () {'
107 this.wait_for_output(index);
121 this.wait_for_output(index);
108 this.then(function () {
122 this.then(function () {
109 var out = this.get_output_cell(index);
123 var out = this.get_output_cell(index);
110 this.test.assertEquals(out['text/plain'], '1', "Python received buffers");
124 this.test.assertEquals(out.data['text/plain'], '1', "Python received buffers");
111 });
125 });
112 });
126 });
113 });
127 });
General Comments 0
You need to be logged in to leave comments. Login now