| ... | ... | @@ -27,6 +27,7 @@ import { |
| 27 | 27 | PrimaryConnectionTarget, |
| 28 | 28 | type NetworkStatus, |
| 29 | 29 | type PreparedConnection, |
| 30 | type SupervisorConnectionState, |
| 30 | 31 | } from "../connection/model.ts"; |
| 31 | 32 | import { EnvironmentSupervisor } from "../connection/supervisor.ts"; |
| 32 | 33 | import { EnvironmentCacheStore } from "../platform/persistence.ts"; |
| ... | ... | @@ -73,8 +74,18 @@ const THREAD: OrchestrationThread = { |
| 73 | 74 | }; |
| 74 | 75 | const SNAPSHOT: OrchestrationThreadDetailSnapshot = { snapshotSequence: 7, thread: THREAD }; |
| 75 | 76 | |
| 77 | const CONNECTED_STATE: SupervisorConnectionState = { |
| 78 | ...AVAILABLE_CONNECTION_STATE, |
| 79 | desired: true, |
| 80 | network: "online", |
| 81 | phase: "connected", |
| 82 | attempt: 1, |
| 83 | generation: 1, |
| 84 | }; |
| 85 | |
| 76 | 86 | const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options?: { |
| 77 | 87 | readonly snapshot?: OrchestrationThreadDetailSnapshot; |
| 88 | readonly connected?: boolean; |
| 78 | 89 | }) { |
| 79 | 90 | const subscriptions = yield* Queue.unbounded<{ |
| 80 | 91 | readonly afterSequence: number | undefined; |
| ... | ... | @@ -123,10 +134,14 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options? |
| 123 | 134 | probe: Effect.void, |
| 124 | 135 | closed: Effect.never, |
| 125 | 136 | }; |
| 137 | const connectionState = yield* SubscriptionRef.make( |
| 138 | options?.connected ? CONNECTED_STATE : AVAILABLE_CONNECTION_STATE, |
| 139 | ); |
| 140 | const sessionRef = yield* SubscriptionRef.make(Option.some(session)); |
| 126 | 141 | const supervisor = EnvironmentSupervisor.of({ |
| 127 | 142 | target: TARGET, |
| 128 | | state: yield* SubscriptionRef.make(AVAILABLE_CONNECTION_STATE), |
| 129 | | session: yield* SubscriptionRef.make(Option.some(session)), |
| 143 | state: connectionState, |
| 144 | session: sessionRef, |
| 130 | 145 | prepared: yield* SubscriptionRef.make<Option.Option<PreparedConnection>>( |
| 131 | 146 | Option.some({ |
| 132 | 147 | environmentId: TARGET.environmentId, |
| ... | ... | @@ -228,6 +243,9 @@ const makeHarness = Effect.fn("TestThreadAtoms.makeHarness")(function* (options? |
| 228 | 243 | ref, |
| 229 | 244 | subscriptions, |
| 230 | 245 | olderLoads, |
| 246 | connectionState, |
| 247 | session, |
| 248 | sessionRef, |
| 231 | 249 | counts: () => ({ httpLoads, diskLoads, opened, active }), |
| 232 | 250 | }; |
| 233 | 251 | }); |
| ... | ... | @@ -319,6 +337,75 @@ describe("createEnvironmentThreadStateAtoms", () => { |
| 319 | 337 | }), |
| 320 | 338 | ); |
| 321 | 339 | |
| 340 | it.effect.each([ |
| 341 | { replayed: false, statuses: ["live"] }, |
| 342 | { replayed: true, statuses: ["live", "synchronizing", "live"] }, |
| 343 | ])( |
| 344 | "keeps a warm resume live until it replays events (replayed: $replayed)", |
| 345 | ({ replayed, statuses }) => |
| 346 | Effect.gen(function* () { |
| 347 | const h = yield* makeHarness({ connected: true }); |
| 348 | const unmount = h.registry.mount(h.stateAtom); |
| 349 | const first = yield* Queue.take(h.subscriptions); |
| 350 | yield* Queue.offer(first.events, { kind: "synchronized" }); |
| 351 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live"); |
| 352 | unmount(); |
| 353 | yield* Deferred.await(first.closed); |
| 354 | |
| 355 | const observed: Array<EnvironmentThreadState["status"]> = []; |
| 356 | const stop = h.registry.subscribe(h.stateAtom, (state) => observed.push(state.status), { |
| 357 | immediate: true, |
| 358 | }); |
| 359 | const remount = h.registry.mount(h.stateAtom); |
| 360 | const next = yield* Queue.take(h.subscriptions); |
| 361 | expect(next.afterSequence).toBe(7); |
| 362 | if (replayed) { |
| 363 | yield* Queue.offer(next.events, { |
| 364 | kind: "snapshot", |
| 365 | snapshot: { snapshotSequence: 9, thread: { ...THREAD, title: "Replayed" } }, |
| 366 | }); |
| 367 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "synchronizing"); |
| 368 | } |
| 369 | yield* Queue.offer(next.events, { kind: "synchronized" }); |
| 370 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live"); |
| 371 | expect(observed.filter((status, index) => observed[index - 1] !== status)).toEqual( |
| 372 | statuses, |
| 373 | ); |
| 374 | stop(); |
| 375 | remount(); |
| 376 | yield* Deferred.await(next.closed); |
| 377 | }), |
| 378 | ); |
| 379 | |
| 380 | it.effect("downgrades a warm resume when the connection dropped while away", () => |
| 381 | Effect.gen(function* () { |
| 382 | const h = yield* makeHarness({ connected: true }); |
| 383 | const unmount = h.registry.mount(h.stateAtom); |
| 384 | const first = yield* Queue.take(h.subscriptions); |
| 385 | yield* Queue.offer(first.events, { kind: "synchronized" }); |
| 386 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live"); |
| 387 | unmount(); |
| 388 | yield* Deferred.await(first.closed); |
| 389 | |
| 390 | yield* SubscriptionRef.set(h.sessionRef, Option.none()); |
| 391 | yield* SubscriptionRef.set(h.connectionState, AVAILABLE_CONNECTION_STATE); |
| 392 | const remount = h.registry.mount(h.stateAtom); |
| 393 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "cached"); |
| 394 | expect(currentThread(h.registry, h.stateAtom)).toBe(THREAD); |
| 395 | expect(h.counts().opened).toBe(1); |
| 396 | |
| 397 | yield* SubscriptionRef.set(h.connectionState, CONNECTED_STATE); |
| 398 | yield* SubscriptionRef.set(h.sessionRef, Option.some(h.session)); |
| 399 | const next = yield* Queue.take(h.subscriptions); |
| 400 | expect(next.afterSequence).toBe(7); |
| 401 | expect(h.registry.get(h.stateAtom).status).toBe("synchronizing"); |
| 402 | yield* Queue.offer(next.events, { kind: "synchronized" }); |
| 403 | yield* observeState(h.registry, h.stateAtom, (state) => state.status === "live"); |
| 404 | remount(); |
| 405 | yield* Deferred.await(next.closed); |
| 406 | }), |
| 407 | ); |
| 408 | |
| 322 | 409 | it.effect("keeps warm data when the raw atom family's weak entry is collected", () => |
| 323 | 410 | Effect.gen(function* () { |
| 324 | 411 | const h = yield* makeHarness(); |