Skip to content

Commit eafa7cd

Browse files
committed
readme getting started
1 parent 0e330f0 commit eafa7cd

File tree

1 file changed

+72
-6
lines changed

1 file changed

+72
-6
lines changed

packages/server/README.md

Lines changed: 72 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,78 @@ Server capable of running multiple concurrent simulations that can be controlled
44

55
https://github.com/thefrontside/simulacrum
66

7-
> [!WARNING]
8-
> The server is undergoing a refactor, and this may not be required for your use case. The refactor includes allow for more simply running single simulators so this package will be primarily useful as a control plane for cases where there are many simulators under test and in use. For the previous iterations, see the `v0` branch which contain the previous functionality.
7+
## Getting Started
8+
9+
Set up a process or simulators such as this example `service-graph.ts`.
10+
11+
```ts service-graph.ts
12+
#!/usr/bin/env node
13+
import { run } from "effection";
14+
import {
15+
useServiceGraph,
16+
simulationCLI,
17+
useChildSimulation,
18+
useSimulation,
19+
useService,
20+
} from "@simulacrum/server";
21+
import { simulation } from "./sim2.ts";
22+
23+
// define your "graph" that can be used through a CLI or as part of a test rig
24+
export const services = useServiceGraph(
25+
{
26+
sim1: {
27+
operation: useChildSimulation("sim-run-as-child-process", "./sim1.ts"),
28+
},
29+
sim2: {
30+
operation: useSimulation("sim-run-in-same-process", simulation),
31+
},
32+
sim3: {
33+
operation: useService(
34+
"arbitray-child-process",
35+
"node --import tsx ./sim3.ts"
36+
),
37+
},
38+
},
39+
{ globalData: { hello: "world" } }
40+
);
41+
42+
// this is a helper function which will give you a CLI around this service graph
43+
// if you are calling this file directly
44+
import { fileURLToPath } from "node:url";
45+
if (process.argv[1] === fileURLToPath(import.meta.url)) {
46+
simulationCLI(services);
47+
}
48+
```
49+
50+
From this, you have two main entry points. One may start it directly from your shell.
51+
52+
```bash
53+
# start a local service graph defined in ./service-graph.ts
54+
node --import tsx ./simulators/service-graph.ts
55+
```
56+
57+
> [!NOTE]
58+
> We use `--import tsx` here to automatically handle the typescript conversion. This is a separate package that you may be interested in using, but it not a hard requirement necessarily.
59+
60+
Secondly, we can use this in tests. It is convenient to place in an `beforeAll()` or a `beforeEach()`. This is built on `effection`, and should handle all shutdown and clean up of the services when the function passes out of lexical scope.
61+
62+
```ts
63+
import { run } from "effection";
64+
import { services } from "./simulators/service-graph.ts";
65+
66+
beforeEach(async () => {
67+
await run(function* () {
68+
const services = yield* services();
69+
// or optionally pass a subset of services to run if not all are required for this test
70+
const subsetOfServices = yield* services(["sim1"]);
71+
yield* suspend();
72+
});
73+
});
74+
75+
test("things", async () => {
76+
// do testing things here
77+
});
78+
```
979

1080
## Operation-based service orchestration
1181

@@ -142,7 +212,3 @@ node --import tsx ./example/concurrency-layers.ts
142212
Previously services could expose their return value via a public `exportsOperation` that consumers could await. That mechanism has been removed in this branch as we move to a child-process-focused runner model. Provider-returned values are still delivered to dependent service factories internally, but no longer exposed as an operation on the public `services` map.
143213

144214
For convenience tests may use the `servicePorts` map exposed by the running graph to discover HTTP ports that services registered when they start. The `servicePorts` map is available on the object returned by the runner and contains service name => port when a service's `operation` returns an object with a `{ port: number }` property.
145-
146-
```
147-
148-
```

0 commit comments

Comments
 (0)