Convert done callbacks to async/await in test/unit/message_handler_spec.js
This commit is contained in:
parent
bc8c0bbbfd
commit
43eb4302ff
@ -39,7 +39,7 @@ describe("message_handler", function () {
|
|||||||
expect(typeof readable.getReader).toEqual("function");
|
expect(typeof readable.getReader).toEqual("function");
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should read using a reader", function (done) {
|
it("should read using a reader", async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const messageHandler1 = new MessageHandler("main", "worker", port);
|
const messageHandler1 = new MessageHandler("main", "worker", port);
|
||||||
@ -71,29 +71,23 @@ describe("message_handler", function () {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
sleep(10)
|
await sleep(10);
|
||||||
.then(() => {
|
expect(log).toEqual("");
|
||||||
expect(log).toEqual("");
|
|
||||||
return reader.read();
|
let result = await reader.read();
|
||||||
})
|
expect(log).toEqual("p");
|
||||||
.then(result => {
|
expect(result.value).toEqual("hi");
|
||||||
expect(log).toEqual("p");
|
expect(result.done).toEqual(false);
|
||||||
expect(result.value).toEqual("hi");
|
|
||||||
expect(result.done).toEqual(false);
|
await sleep(10);
|
||||||
return sleep(10);
|
result = await reader.read();
|
||||||
})
|
expect(result.value).toEqual(undefined);
|
||||||
.then(() => {
|
expect(result.done).toEqual(true);
|
||||||
return reader.read();
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
expect(result.value).toEqual(undefined);
|
|
||||||
expect(result.done).toEqual(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not read any data when cancelled", function (done) {
|
it("should not read any data when cancelled", async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const messageHandler2 = new MessageHandler("worker", "main", port);
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
||||||
@ -139,27 +133,21 @@ describe("message_handler", function () {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
sleep(10)
|
await sleep(10);
|
||||||
.then(() => {
|
expect(log).toEqual("01");
|
||||||
expect(log).toEqual("01");
|
|
||||||
return reader.read();
|
const result = await reader.read();
|
||||||
})
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
.then(result => {
|
expect(result.done).toEqual(false);
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(result.done).toEqual(false);
|
await sleep(10);
|
||||||
return sleep(10);
|
expect(log).toEqual("01p2");
|
||||||
})
|
|
||||||
.then(() => {
|
await reader.cancel(new AbortException("reader cancelled."));
|
||||||
expect(log).toEqual("01p2");
|
expect(log).toEqual("01p2c4");
|
||||||
return reader.cancel(new AbortException("reader cancelled."));
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
expect(log).toEqual("01p2c4");
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not read when errored", function (done) {
|
it("should not read when errored", async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const messageHandler2 = new MessageHandler("worker", "main", port);
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
||||||
@ -195,26 +183,26 @@ describe("message_handler", function () {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
|
await sleep(10);
|
||||||
|
expect(log).toEqual("01");
|
||||||
|
|
||||||
sleep(10)
|
const result = await reader.read();
|
||||||
.then(() => {
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
expect(log).toEqual("01");
|
expect(result.done).toEqual(false);
|
||||||
return reader.read();
|
|
||||||
})
|
try {
|
||||||
.then(result => {
|
await reader.read();
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(result.done).toEqual(false);
|
// Shouldn't get here.
|
||||||
return reader.read();
|
expect(false).toEqual(true);
|
||||||
})
|
} catch (reason) {
|
||||||
.catch(reason => {
|
expect(log).toEqual("01pe");
|
||||||
expect(log).toEqual("01pe");
|
expect(reason instanceof UnknownErrorException).toEqual(true);
|
||||||
expect(reason instanceof UnknownErrorException).toEqual(true);
|
expect(reason.message).toEqual("should not read when errored");
|
||||||
expect(reason.message).toEqual("should not read when errored");
|
}
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should read data with blocking promise", function (done) {
|
it("should read data with blocking promise", async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const messageHandler2 = new MessageHandler("worker", "main", port);
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
||||||
@ -257,40 +245,32 @@ describe("message_handler", function () {
|
|||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
// Sleep for 10ms, so that read() is not unblocking the ready promise.
|
// Sleep for 10ms, so that read() is not unblocking the ready promise.
|
||||||
// Chain all read() to stream in sequence.
|
// Chain all read() to stream in sequence.
|
||||||
sleep(10)
|
await sleep(10);
|
||||||
.then(() => {
|
expect(log).toEqual("01");
|
||||||
expect(log).toEqual("01");
|
|
||||||
return reader.read();
|
let result = await reader.read();
|
||||||
})
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
.then(result => {
|
expect(result.done).toEqual(false);
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(result.done).toEqual(false);
|
await sleep(10);
|
||||||
return sleep(10);
|
expect(log).toEqual("01p2");
|
||||||
})
|
|
||||||
.then(() => {
|
result = await reader.read();
|
||||||
expect(log).toEqual("01p2");
|
expect(result.value).toEqual([5, 6, 7, 8]);
|
||||||
return reader.read();
|
expect(result.done).toEqual(false);
|
||||||
})
|
|
||||||
.then(result => {
|
await sleep(10);
|
||||||
expect(result.value).toEqual([5, 6, 7, 8]);
|
expect(log).toEqual("01p2p");
|
||||||
expect(result.done).toEqual(false);
|
|
||||||
return sleep(10);
|
result = await reader.read();
|
||||||
})
|
expect(result.value).toEqual(undefined);
|
||||||
.then(() => {
|
expect(result.done).toEqual(true);
|
||||||
expect(log).toEqual("01p2p");
|
|
||||||
return reader.read();
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
expect(result.value).toEqual(undefined);
|
|
||||||
expect(result.done).toEqual(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it(
|
it(
|
||||||
"should read data with blocking promise and buffer whole data" +
|
"should read data with blocking promise and buffer whole data" +
|
||||||
" into stream",
|
" into stream",
|
||||||
function (done) {
|
async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const messageHandler2 = new MessageHandler("worker", "main", port);
|
const messageHandler2 = new MessageHandler("worker", "main", port);
|
||||||
@ -332,39 +312,30 @@ describe("message_handler", function () {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
|
await sleep(10);
|
||||||
|
expect(log).toEqual("012");
|
||||||
|
|
||||||
sleep(10)
|
let result = await reader.read();
|
||||||
.then(() => {
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
expect(log).toEqual("012");
|
expect(result.done).toEqual(false);
|
||||||
return reader.read();
|
|
||||||
})
|
await sleep(10);
|
||||||
.then(result => {
|
expect(log).toEqual("012p");
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(result.done).toEqual(false);
|
result = await reader.read();
|
||||||
return sleep(10);
|
expect(result.value).toEqual([5, 6, 7, 8]);
|
||||||
})
|
expect(result.done).toEqual(false);
|
||||||
.then(() => {
|
|
||||||
expect(log).toEqual("012p");
|
await sleep(10);
|
||||||
return reader.read();
|
expect(log).toEqual("012p");
|
||||||
})
|
|
||||||
.then(result => {
|
result = await reader.read();
|
||||||
expect(result.value).toEqual([5, 6, 7, 8]);
|
expect(result.value).toEqual(undefined);
|
||||||
expect(result.done).toEqual(false);
|
expect(result.done).toEqual(true);
|
||||||
return sleep(10);
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
expect(log).toEqual("012p");
|
|
||||||
return reader.read();
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
expect(result.value).toEqual(undefined);
|
|
||||||
expect(result.done).toEqual(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
it("should ignore any pull after close is called", function (done) {
|
it("should ignore any pull after close is called", async function () {
|
||||||
let log = "";
|
let log = "";
|
||||||
const port = new LoopbackPort();
|
const port = new LoopbackPort();
|
||||||
const capability = createPromiseCapability();
|
const capability = createPromiseCapability();
|
||||||
@ -399,29 +370,22 @@ describe("message_handler", function () {
|
|||||||
);
|
);
|
||||||
|
|
||||||
const reader = readable.getReader();
|
const reader = readable.getReader();
|
||||||
|
await sleep(10);
|
||||||
|
expect(log).toEqual("01");
|
||||||
|
|
||||||
sleep(10)
|
capability.resolve();
|
||||||
.then(() => {
|
await capability.promise;
|
||||||
expect(log).toEqual("01");
|
|
||||||
capability.resolve();
|
let result = await reader.read();
|
||||||
return capability.promise.then(() => {
|
expect(result.value).toEqual([1, 2, 3, 4]);
|
||||||
return reader.read();
|
expect(result.done).toEqual(false);
|
||||||
});
|
|
||||||
})
|
await sleep(10);
|
||||||
.then(result => {
|
expect(log).toEqual("01");
|
||||||
expect(result.value).toEqual([1, 2, 3, 4]);
|
|
||||||
expect(result.done).toEqual(false);
|
result = await reader.read();
|
||||||
return sleep(10);
|
expect(result.value).toEqual(undefined);
|
||||||
})
|
expect(result.done).toEqual(true);
|
||||||
.then(() => {
|
|
||||||
expect(log).toEqual("01");
|
|
||||||
return reader.read();
|
|
||||||
})
|
|
||||||
.then(result => {
|
|
||||||
expect(result.value).toEqual(undefined);
|
|
||||||
expect(result.done).toEqual(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user