Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
- http2-assertion-failure should use classic append
- write-after-end should test both append styles
- reconnect tests should use classic append
  • Loading branch information
George-Payne committed Nov 3, 2021
1 parent b4ba1a9 commit a149a27
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 42 deletions.
55 changes: 42 additions & 13 deletions src/__test__/connection/reconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ describe("reconnect", () => {
// make successful append to connect to node
const firstAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "first-append", data: { message: "test" } })
jsonEvent({ type: "first-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(firstAppend).toBeDefined();

Expand All @@ -38,7 +40,9 @@ describe("reconnect", () => {
try {
const secondAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "failed-append", data: { message: "test" } })
jsonEvent({ type: "failed-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(secondAppend).toBe("Unreachable");
} catch (error) {
Expand All @@ -52,7 +56,9 @@ describe("reconnect", () => {
try {
const reconnectedAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "reconnect-append", data: { message: "test" } })
jsonEvent({ type: "reconnect-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(reconnectedAppend).toBeDefined();
break;
Expand All @@ -79,7 +85,9 @@ describe("reconnect", () => {
// make successful append to follower node
const firstAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "first-append", data: { message: "test" } })
jsonEvent({ type: "first-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(firstAppend).toBeDefined();

Expand All @@ -89,7 +97,11 @@ describe("reconnect", () => {
const secondAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "failed-append", data: { message: "test" } }),
{ requiresLeader: true }
{
requiresLeader: true,
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
credentials: { username: "admin", password: "changeit" },
}
);
expect(secondAppend).toBe("Unreachable");
} catch (error) {
Expand All @@ -104,7 +116,11 @@ describe("reconnect", () => {
const reconnectedAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "reconnect-append", data: { message: "test" } }),
{ requiresLeader: true }
{
requiresLeader: true,
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
credentials: { username: "admin", password: "changeit" },
}
);
expect(reconnectedAppend).toBeDefined();

Expand All @@ -129,7 +145,9 @@ describe("reconnect", () => {
// make successful append of 2000 events to node
const firstAppend = await client.appendToStream(
"my_stream",
jsonTestEvents(2000)
jsonTestEvents(2000),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(firstAppend).toBeDefined();

Expand Down Expand Up @@ -159,7 +177,8 @@ describe("reconnect", () => {
try {
const reconnectedAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "reconnect-append", data: { message: "test" } })
jsonEvent({ type: "reconnect-append", data: { message: "test" } }), // batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(reconnectedAppend).toBeDefined();
break;
Expand All @@ -186,7 +205,9 @@ describe("reconnect", () => {
// make successful append to connect to node
const firstAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "first-append", data: { message: "test" } })
jsonEvent({ type: "first-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(firstAppend).toBeDefined();

Expand All @@ -199,7 +220,9 @@ describe("reconnect", () => {
try {
const secondAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "failed-append", data: { message: "test" } })
jsonEvent({ type: "failed-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(secondAppend).toBe("Unreachable");
} catch (error) {
Expand All @@ -210,7 +233,9 @@ describe("reconnect", () => {
try {
const secondAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "failed-append", data: { message: "test" } })
jsonEvent({ type: "failed-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(secondAppend).toBe("Unreachable");
} catch (error) {
Expand All @@ -229,7 +254,9 @@ describe("reconnect", () => {
try {
const reconnectedAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "reconnect-append", data: { message: "test" } })
jsonEvent({ type: "reconnect-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(reconnectedAppend).toBeDefined();
break;
Expand Down Expand Up @@ -257,7 +284,9 @@ describe("reconnect", () => {
// make successful append to connect to node
const firstAppend = await client.appendToStream(
"my_stream",
jsonEvent({ type: "first-append", data: { message: "test" } })
jsonEvent({ type: "first-append", data: { message: "test" } }),
// batch append triggers reconnect as soon as stream drops, so we need to force regular append
{ credentials: { username: "admin", password: "changeit" } }
);
expect(firstAppend).toBeDefined();

Expand Down
10 changes: 5 additions & 5 deletions src/__test__/extra/http2-assertion-failure.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ describe("http2 assertion failure", () => {
beforeAll(async () => {
await node.up();

client = new EventStoreDBClient(
{ endpoint: node.uri },
{ insecure: true },
{ username: "admin", password: "changeit" }
);
client = new EventStoreDBClient({ endpoint: node.uri }, { insecure: true });
});

afterAll(async () => {
Expand All @@ -33,6 +29,8 @@ describe("http2 assertion failure", () => {

const appendRes = await client.appendToStream(stream, priorEvents, {
expectedRevision: NO_STREAM,
// we want to test classic append
credentials: { username: "admin", password: "changeit" },
});

const received: ResolvedEvent[] = [];
Expand All @@ -52,6 +50,8 @@ describe("http2 assertion failure", () => {
while (received.length < 3) await delay(10);
await client.appendToStream(stream, postEvents, {
expectedRevision: appendRes.nextExpectedRevision,
// we want to test classic append
credentials: { username: "admin", password: "changeit" },
});

while (received.length < 10) await delay(10);
Expand Down
105 changes: 81 additions & 24 deletions src/__test__/extra/write-after-end.test.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,96 @@
import { createTestNode, delay, jsonTestEvents } from "@test-utils";
/** @jest-environment ./src/__test__/utils/enableVersionCheck.ts */

import {
createTestNode,
delay,
jsonTestEvents,
matchServerVersion,
optionalTest,
} from "@test-utils";
import { EventStoreDBClient, UnavailableError } from "@eventstore/db-client";

describe("write after end", () => {
const node = createTestNode();
let client!: EventStoreDBClient;
// These tests can take time.
jest.setTimeout(120_000);

beforeAll(async () => {
describe("write after end", () => {
test("Should not write after end", async () => {
const node = createTestNode();
await node.up();
client = new EventStoreDBClient(

const client = new EventStoreDBClient(
{ endpoint: node.uri },
{ rootCertificate: node.rootCertificate },
{ username: "admin", password: "changeit" }
{ rootCertificate: node.rootCertificate }
);
});

afterAll(async () => {
await node.down();
});

test("Should not write after end", async () => {
const STREAM_NAME = "json_stream_name";
await client.appendToStream(STREAM_NAME, jsonTestEvents());

client
.appendToStream(STREAM_NAME, jsonTestEvents(100_000))
.then((result) => {
expect(result).toBe("unreachable");
})
.catch((e) => {
expect(e).toBeInstanceOf(UnavailableError);
await client.appendToStream(STREAM_NAME, jsonTestEvents(), {
// credentials enforces classic append
credentials: { username: "admin", password: "changeit" },
});

const writeUntilError = () =>
new Promise((resolve) => {
const writeOnLoop = (): Promise<never> =>
client
.appendToStream(STREAM_NAME, jsonTestEvents(30_000), {
// credentials enforces classic append
credentials: { username: "admin", password: "changeit" },
})
.then(writeOnLoop);

writeOnLoop().catch((e) => {
resolve(e);
});
});

node.killNode(node.endpoints[0]);
const errorPromise = writeUntilError();

await node.killNode(node.endpoints[0]);

const error = await errorPromise;
expect(error).toBeInstanceOf(UnavailableError);

// wait for any unhandled rejections
await delay(5_000);

await node.down();
});

optionalTest(matchServerVersion`>=21.10`)(
"Should not write after end (batch append)",
async () => {
const node = createTestNode();
await node.up();

const client = new EventStoreDBClient(
{ endpoint: node.uri },
{ rootCertificate: node.rootCertificate }
);

const STREAM_NAME = "json_stream_name";
await client.appendToStream(STREAM_NAME, jsonTestEvents());

const writeUntilError = () =>
new Promise((resolve) => {
const writeOnLoop = (): Promise<never> =>
client
.appendToStream(STREAM_NAME, jsonTestEvents(30_000))
.then(writeOnLoop);

writeOnLoop().catch((e) => {
resolve(e);
});
});

const errorPromise = writeUntilError();

await node.killNode(node.endpoints[0]);

const error = await errorPromise;
expect(error).toBeInstanceOf(UnavailableError);

// wait for any unhandled rejections
await delay(5_000);
}
);
});

0 comments on commit a149a27

Please sign in to comment.