Skip to content
Merged
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
87 changes: 87 additions & 0 deletions packages/client-runtime/src/connection/registry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,93 @@ describe("EnvironmentRegistry", () => {
}),
);

it.effect("keeps durable streams on the same supervisor across in-place catalog updates", () =>
Effect.gen(function* () {
const environmentId = RELAY_TARGET.environmentId;
const harness = yield* makeHarness([RELAY_TARGET], [], [], {
configEvents: Stream.fromEffect(
Effect.sleep("1 second").pipe(
Effect.as({
version: 1,
type: "snapshot",
config: {
directEndpoints: [{ kind: "lan", httpBaseUrl: "http://192.168.1.10:3773/" }],
} as unknown as ServerConfig,
} satisfies ServerConfigStreamEvent),
),
),
});
// `TestClock.adjust` returns once all work woken by the sleep is idle,
// including the follower reacting to the catalog change.
const afterClockTick = <A, E>(effect: Effect.Effect<A, E>) =>
Effect.gen(function* () {
const fiber = yield* Effect.forkChild(Effect.andThen(Effect.sleep("1 second"), effect));
yield* TestClock.adjust("1 second");
return yield* Fiber.join(fiber);
});

yield* Effect.gen(function* () {
const registry = yield* EnvironmentRegistry.EnvironmentRegistry;
const firstStarted = yield* Deferred.make<void>();
const replaced = yield* Deferred.make<void>();
const started = yield* Ref.make<
ReadonlyArray<EnvironmentSupervisor.EnvironmentSupervisor["Service"]>
>([]);
yield* registry.start;
yield* awaitConnectionState(
registry,
environmentId,
(state) => state.phase === "connected",
);

yield* registry
.followStream(
environmentId,
Stream.unwrap(
EnvironmentSupervisor.EnvironmentSupervisor.pipe(
Effect.tap((supervisor) =>
Ref.updateAndGet(started, (current) => [...current, supervisor]).pipe(
Effect.flatMap((current) =>
Deferred.succeed(
current[0] === supervisor ? firstStarted : replaced,
undefined,
),
),
),
),
Effect.as(Stream.never),
),
),
)
.pipe(Stream.runDrain, Effect.forkScoped);
yield* Deferred.await(firstStarted);

yield* TestClock.adjust("1 second");
expect(
(yield* SubscriptionRef.get(registry.entries)).get(environmentId)?.alternateRoutes,
).toHaveLength(1);
yield* afterClockTick(registry.setEnabled(environmentId, false));
yield* afterClockTick(
registry.setCompatibility(
environmentId,
new ConnectionBlockedError({ reason: "unsupported", detail: "Server is too new." }),
),
);
expect(yield* Ref.get(started)).toHaveLength(1);

yield* registry.register(
new RelayConnectionRegistration({
target: new RelayConnectionTarget({ ...RELAY_TARGET, label: "Replacement" }),
}),
);
yield* Deferred.await(replaced);
const supervisors = yield* Ref.get(started);
expect(supervisors).toHaveLength(2);
expect(supervisors[1]).not.toBe(supervisors[0]);
}).pipe(Effect.provide(harness.layer), Effect.scoped);
}),
);

it.effect("moves prepared credential observers when re-pairing an unchanged environment", () =>
Effect.gen(function* () {
const replacementCredential = new BearerConnectionCredential({ token: "replacement-token" });
Expand Down
25 changes: 11 additions & 14 deletions packages/client-runtime/src/connection/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,23 +466,20 @@ export const make = Effect.gen(function* () {
Stream.changesWith(
(previous, current) => Option.getOrNull(previous) === Option.getOrNull(current),
),
Stream.mapEffect(
Option.match({
onNone: () => Effect.succeedNone,
onSome: () => Effect.option(acquireSupervisor(environmentId)),
}),
),
Stream.changesWith(
(previous, current) => Option.getOrNull(previous) === Option.getOrNull(current),
),
Stream.switchMap(
Option.match({
onNone: () => Stream.empty,
onSome: () =>
Stream.unwrap(
acquireSupervisor(environmentId).pipe(
Effect.match({
onFailure: () => Stream.empty,
onSuccess: (supervisor) =>
Stream.provideService(
stream,
EnvironmentSupervisor.EnvironmentSupervisor,
supervisor,
),
}),
),
),
onSome: (supervisor) =>
Stream.provideService(stream, EnvironmentSupervisor.EnvironmentSupervisor, supervisor),
}),
),
);
Expand Down
Loading