Skip to content

Commit

Permalink
Merge pull request #72 from tungv/release/v0.9.15
Browse files Browse the repository at this point in the history
FIX: mongodb connection issue
  • Loading branch information
votrungquan1999 authored Sep 30, 2024
2 parents f853598 + 0071c2c commit 7dba915
Show file tree
Hide file tree
Showing 9 changed files with 171 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on_release_jerni_pr_merged.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:

- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.1.24
bun-version: 1.1.29

- name: Build jerni CLI
run: ./scripts/build-cli.sh
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release_binary_jerni_files.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:

- uses: oven-sh/setup-bun@v2
with:
bun-version: 1.1.24
bun-version: 1.1.29

- name: Build jerni CLI
run: ./scripts/build-cli.sh
Expand Down
4 changes: 2 additions & 2 deletions packages/integrations/tests/CLI/cli-integration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ it("CLI call should project events correctly", async () => {
await journey.waitFor(event2);

// read
const BankAccounts = await journey.getReader(BankAccountModel);
const BankAccounts_2 = await journey.getReader(BankAccountModel_2);
await using BankAccounts = await journey.getReader(BankAccountModel);
await using BankAccounts_2 = await journey.getReader(BankAccountModel_2);

const bankAccount = await BankAccounts.findOne({
id: "123",
Expand Down
156 changes: 156 additions & 0 deletions packages/integrations/tests/CLI/test-mongo-db-connections.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
import { afterAll, expect, test } from "bun:test";
import { exec } from "node:child_process";
import path from "node:path";
import { setTimeout } from "node:timers/promises";
import { MongoClient } from "mongodb";
import { nanoid } from "nanoid";
import createServer from "src/events-server";
import cleanUpTestDatabase from "../cleanUpTestDatabase";
import { BankAccountModel, BankAccountModel_2 } from "../models";
import initializeJourney from "./makeTestJourneyCli";

afterAll(cleanUpTestDatabase);

test("CLI call should project events correctly", async () => {
const dbName = `jerni_integration_test_${nanoid()}`;

const { server } = createServer();
const port = server.port;
const healthCheckPort = Math.floor(Math.random() * 10000) + 10000;

const createJourneyPath = path.resolve(__dirname, "./makeTestJourneyCli.ts");
const jerniCliPath = path.resolve(__dirname, "../../../jerni/src/cli.ts");

const process = exec(
`MONGODB_DBNAME=${dbName} \
MONGODB_URL=mongodb://127.0.0.1:27017 \
EVENTS_SERVER=http://localhost:${port}/ \
PORT=${healthCheckPort} \
bun run ${jerniCliPath} \
${createJourneyPath}`,
(error, stdout, stderr) => {
// console.log(`stdout: ${stdout}`);
// console.error(`stderr: ${stderr}`);
},
);

const journey = await initializeJourney(
{
url: "mongodb://127.0.0.1:27017?appName=jerni-integration-test",
dbName,
},
`http://localhost:${port}/`,
);

// commit event
const event1 = await journey.append<"NEW_ACCOUNT_REGISTERED">({
type: "NEW_ACCOUNT_REGISTERED",
payload: {
id: "123",
name: "test",
},
});
const event2 = await journey.append<"ACCOUNT_DEPOSITED">({
type: "ACCOUNT_DEPOSITED",
payload: {
id: "123",
amount: 100,
},
});

expect(event1).toEqual({
id: 1,
meta: {
client: "mock-client",
client_version: expect.any(String),
committed_at: expect.any(Number),
local_id: expect.any(String),
server_url: `http://localhost:${port}/`,
},
payload: {
id: "123",
name: "test",
},
type: "NEW_ACCOUNT_REGISTERED",
});

await journey.waitFor(event2);

// read
// Wrap in a closure to automatically dispose of the readers
await (async () => {
await using BankAccounts = await journey.getReader(BankAccountModel);
await using BankAccounts_2 = await journey.getReader(BankAccountModel_2);

const bankAccount = await BankAccounts.findOne({
id: "123",
});
const bankAccount_2 = await BankAccounts_2.findOne({
id: "123",
});

// this model does not process the deposit event
expect(bankAccount).toEqual({
__op: 0,
__v: 1,
_id: expect.anything(),
id: "123",
name: "test",
balance: 0,
});

// this model process the deposit event
expect(bankAccount_2).toEqual({
__op: 0,
__v: 2,
_id: expect.anything(),
id: "123",
name: "test",
balance: 100,
});
})();

await journey.dispose();

// the health check server should be running
const req = await fetch(`http://localhost:${healthCheckPort}`);
expect(req.status).toBe(200);

process.kill();

await setTimeout(1000);

expect(fetch(`http://localhost:${healthCheckPort}`)).rejects.toHaveProperty(
"message",
"Unable to connect. Is the computer able to access the url?",
);

// after everything is disposed, the connection should be closed
const connectionByAppName = await getMongoDbConnection();
expect(connectionByAppName["jerni-integration-test"] || 0).toEqual(0);
});

async function getMongoDbConnection() {
const url = "mongodb://127.0.0.1:27017/";
const client = await MongoClient.connect(url);

const connections = await client.db().admin().command({
currentOp: 1,
$all: true,
});

const connectionByAppName = connections.inprog.reduce(
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
(acc: Record<string, number>, cur: any) => {
const appName = cur.appName || "Unknown";
acc[appName] = (acc[appName] || 0) + 1;

return acc;
},
{} as Record<string, number>,
);

await client.close();

return connectionByAppName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ describe("e2e_mongodb_create_and_get", () => {
balance: 0,
});

ctrl.abort();
// if call abort here, the test will fail when running with other tests
// it is caused by the abort error is thrown, and not handled properly by bun as of version 1.1.29
// ctrl.abort();

await app.journey.dispose();
await worker.journey.dispose();
Expand Down
4 changes: 3 additions & 1 deletion packages/integrations/tests/e2e_multiple_stores.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ describe("e2e_multiple_stores", () => {
}),
);

ctrl.abort();
// if call abort here, the test will fail when running with other tests
// it is caused by the abort error is thrown, and not handled properly by bun as of version 1.1.29
// ctrl.abort();

await app.journey.dispose();
await worker.journey.dispose();
Expand Down
6 changes: 3 additions & 3 deletions packages/integrations/tests/fixtures/BankAccountModel_2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ declare module "@jerni/jerni-3/types" {

export interface GetReaderFn {
// biome-ignore lint/style/useShorthandFunctionType: need to be interface to override signature
<DocumentType extends Document>(model: MongoDBModel<DocumentType>): Promise<
Collection<OptimisticDocumentType<WithId<DocumentType>>>
>;
<DocumentType extends Document>(
model: MongoDBModel<DocumentType>,
): Promise<Collection<OptimisticDocumentType<WithId<DocumentType>>> & AsyncDisposable>;
}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/jerni/jsr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jerni/jerni-3",
"version": "0.9.14",
"version": "0.9.15",
"exports": {
".": "./src/createJourney.ts",
"./types": "./src/lib/exported_types.ts",
Expand Down
2 changes: 1 addition & 1 deletion packages/jerni/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jerni/jerni-3",
"version": "v0.9.14",
"version": "v0.9.15",
"type": "module",
"main": "src/index.ts",
"bin": {
Expand Down

0 comments on commit 7dba915

Please sign in to comment.