|
| 1 | +import { delay } from '@std/async/delay'; |
| 2 | +import { expect } from '@std/expect/expect'; |
| 3 | +import { setupEvents } from './testUtil.ts'; |
| 4 | +import { newNode } from './makeLifecycleNode.ts'; |
| 5 | +import { newLifecycleRoot } from './makeLifecycle.ts'; |
| 6 | + |
| 7 | +const stubAsync = () => delay(1); |
| 8 | + |
| 9 | +Deno.test('Lifecycle maker functions', async () => { |
| 10 | + const [actual, actualEvent] = setupEvents(); |
| 11 | + const lc = newLifecycleRoot(); |
| 12 | + lc.register({ name: 'DatabasePool', start: stubAsync, close: stubAsync }); |
| 13 | + |
| 14 | + lc.on(...actualEvent('componentStarted')); |
| 15 | + lc.on(...actualEvent('componentClosing')); |
| 16 | + lc.on(...actualEvent('componentClosed')); |
| 17 | + await lc.start(); |
| 18 | + await lc.close(false); |
| 19 | + await delay(2); |
| 20 | + expect(actual).toEqual([ |
| 21 | + 'componentStarted DatabasePool', |
| 22 | + 'componentClosing DatabasePool', |
| 23 | + 'componentClosed DatabasePool', |
| 24 | + ]); |
| 25 | +}); |
| 26 | + |
| 27 | +const events: string[] = []; |
| 28 | +const parent = newNode((internals) => ({ |
| 29 | + name: 'ParentComponent', |
| 30 | + async start() { |
| 31 | + events.push('parent.starting'); |
| 32 | + internals.registerChildNode({ |
| 33 | + name: 'childOne', |
| 34 | + async start() { |
| 35 | + events.push('childOne.starting'); |
| 36 | + await delay(1); |
| 37 | + events.push('childOne.started'); |
| 38 | + delay(1); |
| 39 | + }, |
| 40 | + async close() { |
| 41 | + events.push('childOne.closing'); |
| 42 | + await delay(1); |
| 43 | + events.push('childOne.closed'); |
| 44 | + }, |
| 45 | + }); |
| 46 | + internals.registerChildNode({ |
| 47 | + name: 'childTwo', |
| 48 | + async start() { |
| 49 | + events.push('childTwo.starting'); |
| 50 | + await delay(1); |
| 51 | + events.push('childTwo.started'); |
| 52 | + delay(1); |
| 53 | + }, |
| 54 | + async close() { |
| 55 | + events.push('childTwo.closing'); |
| 56 | + await delay(1); |
| 57 | + events.push('childTwo.closed'); |
| 58 | + }, |
| 59 | + }); |
| 60 | + await internals.startChildNodes(); |
| 61 | + events.push('parent.started'); |
| 62 | + }, |
| 63 | + async close() { |
| 64 | + events.push('parent.closing'); |
| 65 | + await internals.closeChildNodes(); |
| 66 | + events.push('parent.closed'); |
| 67 | + }, |
| 68 | +})); |
| 69 | + |
| 70 | +Deno.test('lifecycle node manages child nodes', async () => { |
| 71 | + events.splice(0, events.length); |
| 72 | + const lc = newLifecycleRoot(); |
| 73 | + lc.on('componentStarted', (name) => events.push(`componentStarted ${name}`)); |
| 74 | + lc.on('componentClosed', (name) => events.push(`componentClosed ${name}`)); |
| 75 | + lc.register(parent); |
| 76 | + await lc.start(); |
| 77 | + await lc.close(false); |
| 78 | + await delay(2); |
| 79 | + expect(events).toEqual([ |
| 80 | + 'parent.starting', |
| 81 | + 'childOne.starting', |
| 82 | + 'childOne.started', |
| 83 | + 'childTwo.starting', |
| 84 | + 'childTwo.started', |
| 85 | + 'parent.started', |
| 86 | + 'componentStarted ParentComponent', |
| 87 | + 'parent.closing', |
| 88 | + 'childTwo.closing', |
| 89 | + 'childTwo.closed', |
| 90 | + 'childOne.closing', |
| 91 | + 'childOne.closed', |
| 92 | + 'parent.closed', |
| 93 | + 'componentClosed ParentComponent', |
| 94 | + ]); |
| 95 | +}); |
0 commit comments