Backporting a bunch of optimizations from db-only branch
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { assertEquals } from "../../test_deps.ts";
|
||||
import { PromiseQueue, sleep } from "./async.ts";
|
||||
import { assert, assertEquals } from "../../test_deps.ts";
|
||||
import { batchRequests, PromiseQueue, sleep } from "./async.ts";
|
||||
|
||||
Deno.test("PromiseQueue test", async () => {
|
||||
const q = new PromiseQueue();
|
||||
@@ -24,3 +24,19 @@ Deno.test("PromiseQueue test", async () => {
|
||||
});
|
||||
assertEquals(wasRun, true);
|
||||
});
|
||||
|
||||
Deno.test("Batch test", async () => {
|
||||
// Generate an array with numbers up to 100
|
||||
const elements = Array.from(Array(100).keys());
|
||||
const multiplied = await batchRequests(elements, async (batch) => {
|
||||
await sleep(2);
|
||||
// Batches should be 9 or smaller (last batch will be smaller)
|
||||
assert(batch.length <= 9);
|
||||
return batch.map((e) => e * 2);
|
||||
}, 9);
|
||||
assertEquals(multiplied, elements.map((e) => e * 2));
|
||||
const multiplied2 = await batchRequests(elements, async (batch) => {
|
||||
return batch.map((e) => e * 2);
|
||||
}, 10000);
|
||||
assertEquals(multiplied2, elements.map((e) => e * 2));
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user