##// 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 41 this.username = "username";
42 42 this.session_id = utils.uuid();
43 43 this._msg_callbacks = {};
44 this._msg_queue = Promise.resolve();
44 45 this.info_reply = {}; // kernel_info_reply stored here after starting
45 46
46 47 if (typeof(WebSocket) !== 'undefined') {
@@ -854,7 +855,10 b' define(['
854 855 };
855 856
856 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 864 Kernel.prototype._finish_ws_message = function (msg) {
@@ -30,7 +30,7 b' define(['
30 30 return msg;
31 31 };
32 32
33 var _deserialize_binary = function(data, callback) {
33 var _deserialize_binary = function(data) {
34 34 /**
35 35 * deserialize the binary message format
36 36 * callback will be called with a message whose buffers attribute
@@ -39,28 +39,31 b' define(['
39 39 if (data instanceof Blob) {
40 40 // data is Blob, have to deserialize from ArrayBuffer in reader callback
41 41 var reader = new FileReader();
42 reader.onload = function () {
43 var msg = _deserialize_array_buffer(this.result);
44 callback(msg);
45 };
42 var promise = new Promise(function(resolve, reject) {
43 reader.onload = function () {
44 var msg = _deserialize_array_buffer(this.result);
45 resolve(msg);
46 };
47 });
46 48 reader.readAsArrayBuffer(data);
49 return promise;
47 50 } else {
48 51 // data is ArrayBuffer, can deserialize directly
49 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 61 if (typeof data === "string") {
59 62 // text JSON message
60 callback(JSON.parse(data));
63 return Promise.resolve(JSON.parse(data));
61 64 } else {
62 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 120 serialize: serialize
118 121 };
119 122 return exports;
120 }); No newline at end of file
123 });
@@ -51,7 +51,9 b' casper.notebook_test(function () {'
51 51 this.then(function () {
52 52 var index = this.append_cell([
53 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 57 ].join('\n'), 'code');
56 58 this.execute_cell(index);
57 59 });
@@ -59,7 +61,7 b' casper.notebook_test(function () {'
59 61 // wait for capture
60 62 this.waitFor(function () {
61 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 70 var msgs = this.evaluate(function () {
69 71 return IPython._msgs;
70 72 });
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");
73 this.test.assertEquals(msgs.length, 3, "Captured three comm messages");
74
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 85 // extract attributes to test in evaluate,
76 86 // because the raw DataViews can't be passed across
77 var buf_info = function (index) {
78 var buf = IPython._msgs[0].buffers[index];
87 var buf_info = function (message, index) {
88 var buf = IPython._msgs[message].buffers[index];
79 89 var data = {};
80 90 data.byteLength = buf.byteLength;
81 91 data.bytes = [];
@@ -85,18 +95,22 b' casper.notebook_test(function () {'
85 95 return data;
86 96 };
87 97
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');
98 var msgs_with_buffers = [0, 2];
99 for (var i = 0; i < msgs_with_buffers.length; i++) {
100 msg_index = msgs_with_buffers[i];
101 buf0 = this.evaluate(buf_info, msg_index, 0);
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);
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 110 // validate captured buffers Python-side
97 111 this.then(function () {
98 112 var index = this.append_cell([
99 "assert len(msgs) == 1, len(msgs)",
113 "assert len(msgs) == 3, len(msgs)",
100 114 "bufs = msgs[0]['buffers']",
101 115 "assert len(bufs) == len(buffers), bufs",
102 116 "assert bufs[0].bytes == buffers[0], bufs[0].bytes",
@@ -107,7 +121,7 b' casper.notebook_test(function () {'
107 121 this.wait_for_output(index);
108 122 this.then(function () {
109 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