Skip to content

Commit

Permalink
Fix #3411: Do not call onMessage immediately after init
Browse files Browse the repository at this point in the history
This change contains a fix to this in the Node.js Com support and a
test in the test suite for this.
  • Loading branch information
gzm0 committed Jul 21, 2018
1 parent c99fd91 commit 0bf8e18
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
Expand Up @@ -86,4 +86,21 @@ private[test] class TimeoutComTests(config: JSEnvSuiteConfig) {
""", RunConfig())
run.closeAndWait()
}

@Test // #3411
def noImmediateCallbackTest: Unit = {
val run = kit.start(s"""
setTimeout(function() {
var gotCalled = false;
scalajsCom.init(function(msg) { gotCalled = true; });
if (gotCalled) throw "Buffered messages did not get deferred to the event loop";
}, 100);
""", RunConfig())

try {
run.run.send("Hello World")
} finally {
run.closeAndWait()
}
}
}
Expand Up @@ -251,7 +251,7 @@ object ComRun {
| var inMessages = [];
|
| // The callback where received messages go
| var recvCallback = function(msg) { inMessages.push(msg); };
| var onMessage = null;
|
| socket.on('data', function(data) {
| inBuffer = Buffer.concat([inBuffer, data]);
Expand All @@ -268,7 +268,8 @@ object ComRun {
|
| inBuffer = inBuffer.slice(byteLen);
|
| recvCallback(res);
| if (inMessages !== null) inMessages.push(res);
| else onMessage(res);
| }
| });
|
Expand All @@ -280,12 +281,14 @@ object ComRun {
| socket.on('close', function() { process.exit(0); });
|
| global.scalajsCom = {
| init: function(recvCB) {
| if (inMessages === null) throw new Error("Com already initialized");
| for (var i = 0; i < inMessages.length; ++i)
| recvCB(inMessages[i]);
| inMessages = null;
| recvCallback = recvCB;
| init: function(onMsg) {
| if (onMessage !== null) throw new Error("Com already initialized");
| onMessage = onMsg;
| process.nextTick(function() {
| for (var i = 0; i < inMessages.length; ++i)
| onMessage(inMessages[i]);
| inMessages = null;
| });
| },
| send: function(msg) {
| var len = msg.length;
Expand Down

0 comments on commit 0bf8e18

Please sign in to comment.