2020-01-02 15:13:47 -05:00
|
|
|
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
function assert(cond) {
|
|
|
|
if (!cond) {
|
|
|
|
throw Error("assert");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-21 12:16:55 -04:00
|
|
|
// Check overflow (corresponds to full_records test in rust)
|
|
|
|
function fullRecords(q) {
|
|
|
|
q.reset();
|
|
|
|
const oneByte = new Uint8Array([42]);
|
|
|
|
for (let i = 0; i < q.MAX_RECORDS; i++) {
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(q.push(1, oneByte));
|
2019-04-21 12:16:55 -04:00
|
|
|
}
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(!q.push(1, oneByte));
|
2019-08-07 14:02:29 -04:00
|
|
|
const [opId, r] = q.shift();
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(opId == 1);
|
2019-04-21 12:16:55 -04:00
|
|
|
assert(r.byteLength == 1);
|
|
|
|
assert(r[0] == 42);
|
|
|
|
// Even if we shift one off, we still cannot push a new record.
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(!q.push(1, oneByte));
|
2019-04-21 12:16:55 -04:00
|
|
|
}
|
|
|
|
|
2019-03-14 19:17:52 -04:00
|
|
|
function main() {
|
2019-03-26 08:22:07 -04:00
|
|
|
const q = Deno.core.sharedQueue;
|
2019-03-14 19:17:52 -04:00
|
|
|
|
2019-09-07 12:27:18 -04:00
|
|
|
const h = q.head();
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(h > 0);
|
|
|
|
|
|
|
|
let r = new Uint8Array([1, 2, 3, 4, 5]);
|
2019-09-07 12:27:18 -04:00
|
|
|
const len = r.byteLength + h;
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(q.push(1, r));
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(q.head() == len);
|
|
|
|
|
|
|
|
r = new Uint8Array([6, 7]);
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(q.push(1, r));
|
2019-03-14 19:17:52 -04:00
|
|
|
|
|
|
|
r = new Uint8Array([8, 9, 10, 11]);
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(q.push(1, r));
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(q.numRecords() == 3);
|
|
|
|
assert(q.size() == 3);
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
let opId;
|
|
|
|
[opId, r] = q.shift();
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(r.byteLength == 5);
|
|
|
|
assert(r[0] == 1);
|
|
|
|
assert(r[1] == 2);
|
|
|
|
assert(r[2] == 3);
|
|
|
|
assert(r[3] == 4);
|
|
|
|
assert(r[4] == 5);
|
|
|
|
assert(q.numRecords() == 3);
|
|
|
|
assert(q.size() == 2);
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
[opId, r] = q.shift();
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(r.byteLength == 2);
|
|
|
|
assert(r[0] == 6);
|
|
|
|
assert(r[1] == 7);
|
|
|
|
assert(q.numRecords() == 3);
|
|
|
|
assert(q.size() == 1);
|
|
|
|
|
2019-08-07 14:02:29 -04:00
|
|
|
[opId, r] = q.shift();
|
2019-10-02 13:05:48 -04:00
|
|
|
assert(opId == 1);
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(r.byteLength == 4);
|
|
|
|
assert(r[0] == 8);
|
|
|
|
assert(r[1] == 9);
|
|
|
|
assert(r[2] == 10);
|
|
|
|
assert(r[3] == 11);
|
2019-03-15 15:49:41 -04:00
|
|
|
assert(q.numRecords() == 0);
|
2019-03-14 19:17:52 -04:00
|
|
|
assert(q.size() == 0);
|
|
|
|
|
|
|
|
assert(q.shift() == null);
|
|
|
|
assert(q.shift() == null);
|
|
|
|
assert(q.numRecords() == 0);
|
|
|
|
assert(q.size() == 0);
|
2019-03-15 15:49:41 -04:00
|
|
|
|
2019-04-21 12:16:55 -04:00
|
|
|
fullRecords(q);
|
|
|
|
|
2019-03-26 08:22:07 -04:00
|
|
|
Deno.core.print("shared_queue_test.js ok\n");
|
2019-04-21 12:16:55 -04:00
|
|
|
q.reset();
|
2019-03-14 19:17:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|