Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

socketHeartbeat.test.ts

39 lines1.4 KB

1import { assert, describe, it } from "@effect/vitest";
2import * as Effect from "effect/Effect";
3import * as Exit from "effect/Exit";
4import * as Scope from "effect/Scope";
5import * as Socket from "effect/socket/Socket";
6import * as TestClock from "effect/testing/TestClock";
7​
8import {
9 SOCKET_HEARTBEAT_FRAME,
10 SOCKET_HEARTBEAT_INTERVAL,
11 withSocketHeartbeat,
12} from "./socketHeartbeat.ts";
13​
14describe("withSocketHeartbeat", () => {
15 it.effect("writes a heartbeat each interval until the writer's scope closes", () =>
16 Effect.gen(function* () {
17 const written: Array<string | Uint8Array | Socket.CloseEvent> = [];
18 const socket = withSocketHeartbeat(
19 Socket.make({
20 reader: Effect.never,
21 writer: Effect.succeed({
22 write: (chunk) => Effect.sync(() => void written.push(chunk)),
23 writeAll: (chunks) => Effect.sync(() => void written.push(...chunks)),
24 }),
25 }),
26 );
27 const scope = yield* Scope.make();
28 yield* Scope.provide(socket.writer, scope);
29​
30 yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
31 yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
32 assert.deepStrictEqual(written, [SOCKET_HEARTBEAT_FRAME, SOCKET_HEARTBEAT_FRAME]);
33​
34 yield* Scope.close(scope, Exit.void);
35 yield* TestClock.adjust(SOCKET_HEARTBEAT_INTERVAL);
36 assert.strictEqual(written.length, 2);
37 }),
38 );
39});