Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
25 changes: 16 additions & 9 deletions packages/actor-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@
"name": "actor-core",
"version": "0.8.0",
"license": "Apache-2.0",
"files": [
"dist",
"src",
"deno.json",
"bun.json",
"package.json"
],
"files": ["dist", "src", "deno.json", "bun.json", "package.json"],
"type": "module",
"bin": "./dist/cli/mod.cjs",
"exports": {
Expand Down Expand Up @@ -72,6 +66,16 @@
"default": "./dist/driver-helpers/mod.cjs"
}
},
"./driver-test-suite": {
"import": {
"types": "./dist/driver-test-suite/mod.d.ts",
"default": "./dist/driver-test-suite/mod.js"
},
"require": {
"types": "./dist/driver-test-suite/mod.d.cts",
"default": "./dist/driver-test-suite/mod.cjs"
}
},
"./topologies/coordinate": {
"import": {
"types": "./dist/topologies/coordinate/mod.d.ts",
Expand Down Expand Up @@ -159,7 +163,7 @@
"sideEffects": false,
"scripts": {
"dev": "yarn build --watch",
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/cli/mod.ts src/actor/protocol/inspector/mod.ts src/actor/protocol/http/rpc.ts src/test/mod.ts src/inspector/protocol/actor/mod.ts src/inspector/protocol/manager/mod.ts src/inspector/mod.ts",
"build": "tsup src/mod.ts src/client/mod.ts src/common/log.ts src/actor/errors.ts src/topologies/coordinate/mod.ts src/topologies/partition/mod.ts src/utils.ts src/driver-helpers/mod.ts src/driver-test-suite/mod.ts src/cli/mod.ts src/actor/protocol/inspector/mod.ts src/actor/protocol/http/rpc.ts src/test/mod.ts src/inspector/protocol/actor/mod.ts src/inspector/protocol/manager/mod.ts src/inspector/mod.ts",
"check-types": "tsc --noEmit",
"boop": "tsc --outDir dist/test -d",
"test": "vitest run",
Expand All @@ -181,7 +185,10 @@
"tsup": "^8.4.0",
"typescript": "^5.7.3",
"vitest": "^3.1.1",
"ws": "^8.18.1"
"ws": "^8.18.1",
"@hono/node-server": "^1.14.0",
"@hono/node-ws": "^1.1.1",
"bundle-require": "^5.1.0"
},
"peerDependencies": {
"eventsource": "^3.0.5",
Expand Down
7 changes: 7 additions & 0 deletions packages/actor-core/src/driver-test-suite/log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { getLogger } from "@/common/log";

export const LOGGER_NAME = "test-suite";

export function logger() {
return getLogger(LOGGER_NAME);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@ import {
CoordinateDriver,
DriverConfig,
ManagerDriver,
} from "actor-core/driver-helpers";
} from "@/driver-helpers/mod";
import { runActorDriverTests, waitFor } from "./tests/actor-driver";
import { runManagerDriverTests } from "./tests/manager-driver";
import { describe } from "vitest";
import {
type ActorCoreApp,
CoordinateTopology,
StandaloneTopology,
} from "actor-core";
} from "@/mod";
import { createNodeWebSocket, type NodeWebSocket } from "@hono/node-ws";
import invariant from "invariant";
import { bundleRequire } from "bundle-require";
import { getPort } from "actor-core/test";
import { Transport } from "actor-core/client";
import { getPort } from "@/test/mod";
import { Transport } from "@/client/mod";

export interface DriverTestConfig {
/** Deploys an app and returns the connection endpoint. */
Expand Down
13 changes: 13 additions & 0 deletions packages/actor-core/src/driver-test-suite/test-apps.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { resolve } from "node:path";

export type { App as CounterApp } from "../../fixtures/driver-test-suite/counter";
export type { App as ScheduledApp } from "../../fixtures/driver-test-suite/scheduled";

export const COUNTER_APP_PATH = resolve(
__dirname,
"../../fixtures/driver-test-suite/counter.ts",
);
export const SCHEDULED_APP_PATH = resolve(
__dirname,
"../../fixtures/driver-test-suite/scheduled.ts",
);
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { describe, test, expect, vi } from "vitest";
import type { DriverTestConfig, DriverTestConfigWithTransport } from "@/mod";
import { setupDriverTest } from "@/utils";
import { resolve } from "node:path";
import type { App as CounterApp } from "../../fixtures/apps/counter";
import type { App as ScheduledApp } from "../../fixtures/apps/scheduled";
import type { DriverTestConfig, DriverTestConfigWithTransport } from "../mod";
import { setupDriverTest } from "../utils";
import {
COUNTER_APP_PATH,
SCHEDULED_APP_PATH,
type CounterApp,
type ScheduledApp,
} from "../test-apps";

/**
* Waits for the specified time, using either real setTimeout or vi.advanceTimersByTime
Expand All @@ -20,14 +23,16 @@ export async function waitFor(
return Promise.resolve();
}
}
export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransport) {
export function runActorDriverTests(
driverTestConfig: DriverTestConfigWithTransport,
) {
describe("Actor Driver Tests", () => {
describe("State Persistence", () => {
test("persists state between actor instances", async (c) => {
const { client } = await setupDriverTest<CounterApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/counter.ts"),
COUNTER_APP_PATH,
);

// Create instance and increment
Expand All @@ -45,13 +50,13 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
const { client } = await setupDriverTest<CounterApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/counter.ts"),
COUNTER_APP_PATH,
);

// Create actor and set initial state
const counterInstance = client.counter.getOrCreate();
await counterInstance.increment(5);

// Reconnect to the same actor
const reconnectedInstance = client.counter.getOrCreate();
const persistedCount = await reconnectedInstance.increment(0);
Expand All @@ -62,17 +67,17 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
const { client } = await setupDriverTest<CounterApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/counter.ts"),
COUNTER_APP_PATH,
);

// Create first counter with specific key
const counterA = client.counter.getOrCreate(["counter-a"]);
await counterA.increment(5);

// Create second counter with different key
const counterB = client.counter.getOrCreate(["counter-b"]);
await counterB.increment(10);

// Verify state is separate
const countA = await counterA.increment(0);
const countB = await counterB.increment(0);
Expand All @@ -86,74 +91,59 @@ export function runActorDriverTests(driverTestConfig: DriverTestConfigWithTransp
const { client } = await setupDriverTest<ScheduledApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/scheduled.ts"),
SCHEDULED_APP_PATH,
);

// Create instance
const alarmInstance = client.scheduled.getOrCreate();

// Schedule a task to run in 100ms
await alarmInstance.scheduleTask(100);

// Wait for longer than the scheduled time
await waitFor(driverTestConfig, 150);

// Verify the scheduled task ran
const lastRun = await alarmInstance.getLastRun();
const scheduledCount = await alarmInstance.getScheduledCount();

expect(lastRun).toBeGreaterThan(0);
expect(scheduledCount).toBe(1);
});
});

describe("Actor Handle", () => {
test("stateless handle can perform RPC calls", async (c) => {
const { client } = await setupDriverTest<CounterApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/counter.ts"),
COUNTER_APP_PATH,
);

// Get a handle to an actor
const counterHandle = client.counter.getOrCreate("test-handle");
await counterHandle.increment(1);
await counterHandle.increment(2);
const count = await counterHandle.getCount();
expect(count).toBe(3);
});

test("stateless handles to same actor share state", async (c) => {
const { client } = await setupDriverTest<CounterApp>(
c,
driverTestConfig,
resolve(__dirname, "../fixtures/apps/counter.ts"),
COUNTER_APP_PATH,
);

// Get a handle to an actor
const handle1 = client.counter.getOrCreate("test-handle-shared");
await handle1.increment(5);

// Get another handle to same actor
const handle2 = client.counter.getOrCreate("test-handle-shared");
const count = await handle2.getCount();
expect(count).toBe(5);
});

// TODO: Fix this
//test("create new actor with handle", async (c) => {
// const { client } = await setupDriverTest<CounterApp>(
// c,
// driverTestConfig,
// resolve(__dirname, "../fixtures/apps/counter.ts"),
// );
//
// // Create a new actor with handle
// const createdHandle = client.counter.create("test-handle-create");
// await createdHandle.increment(5);
// const count = await createdHandle.getCount();
// expect(count).toBe(5);
//});
});
});
}
Loading
Loading