|
| 1 | +import { delay } from 'jsr:@std/async'; |
| 2 | +import { expect } from 'jsr:@std/expect'; |
| 3 | +import { Lifecycle, type LifecycleComponent } from './lifecycle.ts'; |
| 4 | + |
| 5 | +const createChecks = () => { |
| 6 | + const checks = { |
| 7 | + didStart: false, |
| 8 | + didRun: false, |
| 9 | + didClosing: false, |
| 10 | + didClose: false, |
| 11 | + }; |
| 12 | + const passCheck = (check: keyof typeof checks) => () => (checks[check] = true); |
| 13 | + return { checks, passCheck }; |
| 14 | +}; |
| 15 | + |
| 16 | +type EventName = |
| 17 | + | 'componentStarted' |
| 18 | + | 'componentRestarting' |
| 19 | + | 'componentRestarted' |
| 20 | + | 'componentClosing' |
| 21 | + | 'componentClosed'; |
| 22 | +type Event = `${EventName} ${string}`; |
| 23 | +type Events = Event[]; |
| 24 | + |
| 25 | +type MakeEvent = (event: EventName) => Event; |
| 26 | +const makeEventFn = (name?: string): MakeEvent => (event) => `${event} ${name}`; |
| 27 | + |
| 28 | +const makeComponent = (c?: Partial<LifecycleComponent>): LifecycleComponent => { |
| 29 | + const name = c?.name ?? 'test-component'; |
| 30 | + const component: LifecycleComponent = { |
| 31 | + name, |
| 32 | + start: async () => (await delay(1)) ?? (component.status = 'running'), |
| 33 | + status: 'pending', |
| 34 | + close: () => delay(1), |
| 35 | + ...c, |
| 36 | + }; |
| 37 | + return component; |
| 38 | +}; |
| 39 | + |
| 40 | +const makeCrashingComponent = ( |
| 41 | + c?: Partial<LifecycleComponent>, |
| 42 | + crashAfterMs = 10, |
| 43 | +): LifecycleComponent => { |
| 44 | + const component = makeComponent({ |
| 45 | + start: async () => { |
| 46 | + (await delay(1)) ?? (component.status = 'running'); |
| 47 | + delay(crashAfterMs).then(() => (component.status = 'crashed')); |
| 48 | + }, |
| 49 | + restart: () => { |
| 50 | + component.status = 'running'; |
| 51 | + return Promise.resolve(); |
| 52 | + }, |
| 53 | + ...c, |
| 54 | + }); |
| 55 | + return component; |
| 56 | +}; |
| 57 | + |
| 58 | +type EventTracker = (en: EventName) => [EventName, (cn?: string) => void]; |
| 59 | +const setupEvents = (): [Events, EventTracker] => { |
| 60 | + const actual: Events = []; |
| 61 | + const trackActual = (e: Event) => actual.push(e); |
| 62 | + return [ |
| 63 | + actual, |
| 64 | + (en: EventName) => [en, (cn?: string) => trackActual(makeEventFn(cn)(en))], |
| 65 | + ]; |
| 66 | +}; |
| 67 | + |
| 68 | +Deno.test('LifeCycle', async ({ step }) => { |
| 69 | + await step('can start & close a lifecycle', async () => { |
| 70 | + const { checks, passCheck } = createChecks(); |
| 71 | + const lc = new Lifecycle({ healthCheckIntervalMs: 50 }); |
| 72 | + lc.on('starting', passCheck('didStart')); |
| 73 | + lc.on('running', passCheck('didRun')); |
| 74 | + lc.on('closing', passCheck('didClosing')); |
| 75 | + lc.on('closed', passCheck('didClose')); |
| 76 | + await lc.start(); |
| 77 | + await lc.close(false); |
| 78 | + expect(checks).toEqual({ |
| 79 | + didStart: true, |
| 80 | + didRun: true, |
| 81 | + didClosing: true, |
| 82 | + didClose: true, |
| 83 | + }); |
| 84 | + }); |
| 85 | + await step('start and close lifecycle components', async () => { |
| 86 | + const [actual, actualEvent] = setupEvents(); |
| 87 | + const lc = new Lifecycle({ healthCheckIntervalMs: 50 }); |
| 88 | + lc.register(makeComponent({ name: 'test-component-1' })); |
| 89 | + lc.register(makeComponent({ name: 'test-component-2' })); |
| 90 | + lc.register(makeComponent({ name: 'test-component-3' })); |
| 91 | + |
| 92 | + lc.on(...actualEvent('componentStarted')); |
| 93 | + lc.on(...actualEvent('componentClosing')); |
| 94 | + lc.on(...actualEvent('componentClosed')); |
| 95 | + await lc.start(); |
| 96 | + await lc.close(false); |
| 97 | + await delay(2); |
| 98 | + expect(actual).toEqual([ |
| 99 | + 'componentStarted test-component-1', |
| 100 | + 'componentStarted test-component-2', |
| 101 | + 'componentStarted test-component-3', |
| 102 | + 'componentClosing test-component-3', |
| 103 | + 'componentClosed test-component-3', |
| 104 | + 'componentClosing test-component-2', |
| 105 | + 'componentClosed test-component-2', |
| 106 | + 'componentClosing test-component-1', |
| 107 | + 'componentClosed test-component-1', |
| 108 | + ]); |
| 109 | + }); |
| 110 | + await step('restarts crashed life cycle component', async () => { |
| 111 | + const [actual, actualEvent] = setupEvents(); |
| 112 | + const lc = new Lifecycle({ healthCheckIntervalMs: 1 }); |
| 113 | + |
| 114 | + lc.register(makeComponent({ name: 'test-component-1' })); |
| 115 | + lc.register(makeComponent({ name: 'test-component-2' })); |
| 116 | + lc.register(makeCrashingComponent({ name: 'test-component-3' }, 4)); |
| 117 | + lc.register(makeComponent({ name: 'test-component-4' })); |
| 118 | + lc.register(makeCrashingComponent({ name: 'test-component-5' }, 4)); |
| 119 | + lc.on(...actualEvent('componentStarted')); |
| 120 | + lc.on(...actualEvent('componentClosing')); |
| 121 | + lc.on(...actualEvent('componentClosed')); |
| 122 | + lc.on(...actualEvent('componentRestarting')); |
| 123 | + lc.on(...actualEvent('componentRestarted')); |
| 124 | + await lc.start(); |
| 125 | + await delay(15); |
| 126 | + await lc.close(false); |
| 127 | + await delay(30); |
| 128 | + expect(actual).toEqual([ |
| 129 | + 'componentStarted test-component-1', |
| 130 | + 'componentStarted test-component-2', |
| 131 | + 'componentStarted test-component-3', |
| 132 | + 'componentStarted test-component-4', |
| 133 | + 'componentStarted test-component-5', |
| 134 | + 'componentRestarting test-component-3', |
| 135 | + 'componentRestarted test-component-3', |
| 136 | + 'componentRestarting test-component-5', |
| 137 | + 'componentRestarted test-component-5', |
| 138 | + 'componentClosing test-component-5', |
| 139 | + 'componentClosed test-component-5', |
| 140 | + 'componentClosing test-component-4', |
| 141 | + 'componentClosed test-component-4', |
| 142 | + 'componentClosing test-component-3', |
| 143 | + 'componentClosed test-component-3', |
| 144 | + 'componentClosing test-component-2', |
| 145 | + 'componentClosed test-component-2', |
| 146 | + 'componentClosing test-component-1', |
| 147 | + 'componentClosed test-component-1', |
| 148 | + ]); |
| 149 | + }); |
| 150 | +}); |
0 commit comments