Skip to content
This repository has been archived by the owner on Oct 18, 2023. It is now read-only.

Hrana concurrency #240

Merged
merged 4 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add a test for correct ordering of concurrent Hrana requests
  • Loading branch information
honzasp committed Feb 21, 2023
commit c5caa3a2494322accd19976de8018d7432d4ff95
5 changes: 0 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 38 additions & 1 deletion packages/js/hrana-client/src/__tests__/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ test("Stream.executeRaw()", withClient(async (c) => {
]);
}));

test("concurrent streams", withClient(async (c) => {
test("concurrent streams are separate", withClient(async (c) => {
const s1 = c.openStream();
await s1.execute("DROP TABLE IF EXISTS t");
await s1.execute("CREATE TABLE t (number)");
Expand All @@ -134,3 +134,40 @@ test("concurrent streams", withClient(async (c) => {
expect(await s1.queryValue("SELECT SUM(number) FROM t")).toStrictEqual(1);
expect(await s2.queryValue("SELECT SUM(number) FROM t")).toStrictEqual(11);
}));

test("concurrent operations are correctly ordered", withClient(async (c) => {
const s = c.openStream();
await s.execute("DROP TABLE IF EXISTS t");
await s.execute("CREATE TABLE t (stream, value)");

async function stream(streamId: number): Promise<void> {
const s = c.openStream();

let value = "s" + streamId;
await s.execute(["INSERT INTO t VALUES (?, ?)", [streamId, value]]);

const promises: Array<Promise<any>> = [];
const expectedValues = [];
for (let i = 0; i < 10; ++i) {
const promise = s.queryValue([
"UPDATE t SET value = value || ? WHERE stream = ? RETURNING value",
["_" + i, streamId],
]);
value = value + "_" + i;
promises.push(promise);
expectedValues.push(value);
}

for (let i = 0; i < promises.length; ++i) {
expect(await promises[i]).toStrictEqual(expectedValues[i]);
}

s.close();
}

const promises = [];
for (let i = 0; i < 10; ++i) {
promises.push(stream(i));
}
await Promise.all(promises);
}));
2 changes: 1 addition & 1 deletion packages/js/hrana-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ export class Stream {
});
}

/** Execute a statement that returns at most single value. */
/** Execute a statement that returns at most a single value. */
queryValue(stmt: Stmt): Promise<Value | undefined> {
return new Promise((valueCallback, errorCallback) => {
this.#client._execute(this.#state, {
Expand Down