2017-04-14 06:09:25 +09:00
|
|
|
/* Copyright 2017 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-09-02 20:28:19 +09:00
|
|
|
import {
|
|
|
|
AbortException,
|
2022-03-08 01:41:41 +09:00
|
|
|
PromiseCapability,
|
2019-09-02 20:28:19 +09:00
|
|
|
UnknownErrorException,
|
2020-01-02 20:00:16 +09:00
|
|
|
} from "../../src/shared/util.js";
|
|
|
|
import { LoopbackPort } from "../../src/display/api.js";
|
|
|
|
import { MessageHandler } from "../../src/shared/message_handler.js";
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2020-04-14 19:28:14 +09:00
|
|
|
describe("message_handler", function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
// Sleep function to wait for sometime, similar to setTimeout but faster.
|
|
|
|
function sleep(ticks) {
|
2024-01-21 18:13:12 +09:00
|
|
|
return Promise.resolve().then(() => ticks && sleep(ticks - 1));
|
2017-04-14 06:09:25 +09:00
|
|
|
}
|
|
|
|
|
2020-04-14 19:28:14 +09:00
|
|
|
describe("sendWithStream", function () {
|
|
|
|
it("should return a ReadableStream", function () {
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream("fakeHandler");
|
2017-04-14 06:09:25 +09:00
|
|
|
// Check if readable is an instance of ReadableStream.
|
|
|
|
expect(typeof readable).toEqual("object");
|
|
|
|
expect(typeof readable.getReader).toEqual("function");
|
|
|
|
});
|
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
it("should read using a reader", async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
|
|
|
sink.ready
|
|
|
|
.then(() => {
|
|
|
|
sink.enqueue("hi");
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
sink.close();
|
|
|
|
});
|
|
|
|
return sleep(5);
|
|
|
|
});
|
2020-01-24 17:48:21 +09:00
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
highWaterMark: 1,
|
|
|
|
size() {
|
|
|
|
return 1;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2021-04-15 04:52:23 +09:00
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("");
|
|
|
|
|
|
|
|
let result = await reader.read();
|
|
|
|
expect(log).toEqual("p");
|
|
|
|
expect(result.value).toEqual("hi");
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual(undefined);
|
|
|
|
expect(result.done).toEqual(true);
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
it("should not read any data when cancelled", async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
|
|
|
log += "0";
|
|
|
|
sink.ready
|
|
|
|
.then(() => {
|
|
|
|
log += "1";
|
|
|
|
sink.enqueue([1, 2, 3, 4], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
log += "2";
|
|
|
|
sink.enqueue([5, 6, 7, 8], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(
|
|
|
|
() => {
|
|
|
|
log += "3";
|
|
|
|
sink.close();
|
|
|
|
},
|
|
|
|
() => {
|
|
|
|
log += "4";
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
{},
|
|
|
|
{
|
2017-04-14 06:09:25 +09:00
|
|
|
highWaterMark: 4,
|
|
|
|
size(arr) {
|
|
|
|
return arr.length;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01");
|
|
|
|
|
|
|
|
const result = await reader.read();
|
|
|
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01p2");
|
|
|
|
|
|
|
|
await reader.cancel(new AbortException("reader cancelled."));
|
|
|
|
expect(log).toEqual("01p2c4");
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
it("should not read when errored", async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
2019-09-02 20:18:39 +09:00
|
|
|
log += "0";
|
2017-04-14 06:09:25 +09:00
|
|
|
sink.ready
|
|
|
|
.then(() => {
|
2019-09-02 20:18:39 +09:00
|
|
|
log += "1";
|
2017-04-14 06:09:25 +09:00
|
|
|
sink.enqueue([1, 2, 3, 4], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
2019-09-02 20:18:39 +09:00
|
|
|
log += "e";
|
2019-09-02 20:28:19 +09:00
|
|
|
sink.error(new Error("should not read when errored"));
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
});
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
{},
|
|
|
|
{
|
2017-04-14 06:09:25 +09:00
|
|
|
highWaterMark: 4,
|
|
|
|
size(arr) {
|
|
|
|
return arr.length;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01");
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
const result = await reader.read();
|
|
|
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await reader.read();
|
|
|
|
|
|
|
|
// Shouldn't get here.
|
|
|
|
expect(false).toEqual(true);
|
|
|
|
} catch (reason) {
|
|
|
|
expect(log).toEqual("01pe");
|
|
|
|
expect(reason instanceof UnknownErrorException).toEqual(true);
|
|
|
|
expect(reason.message).toEqual("should not read when errored");
|
|
|
|
}
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
it("should read data with blocking promise", async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
|
|
|
log += "0";
|
|
|
|
sink.ready
|
|
|
|
.then(() => {
|
|
|
|
log += "1";
|
|
|
|
sink.enqueue([1, 2, 3, 4], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
log += "2";
|
|
|
|
sink.enqueue([5, 6, 7, 8], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
sink.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
highWaterMark: 4,
|
|
|
|
size(arr) {
|
|
|
|
return arr.length;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2017-04-14 06:09:25 +09:00
|
|
|
// Sleep for 10ms, so that read() is not unblocking the ready promise.
|
|
|
|
// Chain all read() to stream in sequence.
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01");
|
|
|
|
|
|
|
|
let result = await reader.read();
|
|
|
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01p2");
|
|
|
|
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual([5, 6, 7, 8]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01p2p");
|
|
|
|
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual(undefined);
|
|
|
|
expect(result.done).toEqual(true);
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
it(
|
|
|
|
"should read data with blocking promise and buffer whole data" +
|
|
|
|
" into stream",
|
2021-04-15 04:52:23 +09:00
|
|
|
async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
|
|
|
log += "0";
|
|
|
|
sink.ready
|
|
|
|
.then(() => {
|
|
|
|
log += "1";
|
|
|
|
sink.enqueue([1, 2, 3, 4], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
log += "2";
|
|
|
|
sink.enqueue([5, 6, 7, 8], 4);
|
|
|
|
return sink.ready;
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
sink.close();
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
});
|
2017-04-14 06:09:25 +09:00
|
|
|
return sleep(10);
|
|
|
|
});
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
highWaterMark: 8,
|
|
|
|
size(arr) {
|
|
|
|
return arr.length;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("012");
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
let result = await reader.read();
|
|
|
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("012p");
|
|
|
|
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual([5, 6, 7, 8]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("012p");
|
|
|
|
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual(undefined);
|
|
|
|
expect(result.done).toEqual(true);
|
2017-04-14 06:09:25 +09:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
it("should ignore any pull after close is called", async function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
let log = "";
|
2020-01-24 17:48:21 +09:00
|
|
|
const port = new LoopbackPort();
|
2022-03-08 01:41:41 +09:00
|
|
|
const capability = new PromiseCapability();
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
2017-04-14 06:09:25 +09:00
|
|
|
messageHandler2.on("fakeHandler", (data, sink) => {
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onPull = function () {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "p";
|
|
|
|
};
|
2020-04-14 19:28:14 +09:00
|
|
|
sink.onCancel = function (reason) {
|
2017-04-14 06:09:25 +09:00
|
|
|
log += "c";
|
|
|
|
};
|
|
|
|
log += "0";
|
|
|
|
sink.ready.then(() => {
|
|
|
|
log += "1";
|
|
|
|
sink.enqueue([1, 2, 3, 4], 4);
|
|
|
|
});
|
|
|
|
return capability.promise.then(() => {
|
|
|
|
sink.close();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
|
|
|
const readable = messageHandler1.sendWithStream(
|
2017-04-14 06:09:25 +09:00
|
|
|
"fakeHandler",
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
highWaterMark: 10,
|
|
|
|
size(arr) {
|
|
|
|
return arr.length;
|
|
|
|
},
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2020-01-24 17:48:21 +09:00
|
|
|
const reader = readable.getReader();
|
2021-04-15 04:52:23 +09:00
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01");
|
2017-04-14 06:09:25 +09:00
|
|
|
|
2021-04-15 04:52:23 +09:00
|
|
|
capability.resolve();
|
|
|
|
await capability.promise;
|
|
|
|
|
|
|
|
let result = await reader.read();
|
|
|
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
|
|
expect(result.done).toEqual(false);
|
|
|
|
|
|
|
|
await sleep(10);
|
|
|
|
expect(log).toEqual("01");
|
|
|
|
|
|
|
|
result = await reader.read();
|
|
|
|
expect(result.value).toEqual(undefined);
|
|
|
|
expect(result.done).toEqual(true);
|
2017-04-14 06:09:25 +09:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|