Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | import { describe, expect, it } from '@effect/vitest'; |
| 2 | import { Cause, Deferred, Effect, Fiber } from 'effect'; |
| 3 | import { clearInProcessMemos, memoizeInProcess } from 'src/utils/memoize-in-process'; |
| 4 | |
| 5 | describe('memoizeInProcess', () => { |
| 6 | it.effect('runs the effect once per key and shares the result', () => |
| 7 | Effect.gen(function* () { |
| 8 | let calls = 0; |
| 9 | const load = memoizeInProcess({ |
| 10 | keyOf: (key: string) => key, |
| 11 | make: key => Effect.sync(() => `${key}:${++calls}`), |
| 12 | }); |
| 13 | |
| 14 | const [a, b, c] = yield* Effect.all([load('x'), load('x'), load('y')], { |
| 15 | concurrency: 'unbounded', |
| 16 | }); |
| 17 | |
| 18 | expect(a).toBe('x:1'); |
| 19 | expect(b).toBe('x:1'); |
| 20 | expect(c).toBe('y:2'); |
| 21 | expect(calls).toBe(2); |
| 22 | }) |
| 23 | ); |
| 24 | |
| 25 | it.effect('drops a failure so the next caller retries', () => |
| 26 | Effect.gen(function* () { |
| 27 | let calls = 0; |
| 28 | const outcomes = [Effect.fail(new Error('first call fails')), Effect.succeed(2)]; |
| 29 | const load = memoizeInProcess({ |
| 30 | keyOf: (key: string) => key, |
| 31 | make: () => Effect.suspend(() => outcomes[calls++]), |
| 32 | }); |
| 33 | |
| 34 | const first = yield* Effect.flip(load('k')); |
| 35 | expect(first).toEqual(new Error('first call fails')); |
| 36 | |
| 37 | expect(yield* load('k')).toBe(2); |
| 38 | expect(yield* load('k')).toBe(2); |
| 39 | expect(calls).toBe(2); |
| 40 | }) |
| 41 | ); |
| 42 | |
| 43 | it.effect('drops a defect so the next caller retries', () => |
| 44 | Effect.gen(function* () { |
| 45 | let calls = 0; |
| 46 | const outcomes = [Effect.die('first call dies'), Effect.succeed(2)]; |
| 47 | const load = memoizeInProcess({ |
| 48 | keyOf: (key: string) => key, |
| 49 | make: () => Effect.suspend(() => outcomes[calls++]), |
| 50 | }); |
| 51 | |
| 52 | const first = yield* load('k').pipe(Effect.sandbox, Effect.flip); |
| 53 | expect(Cause.squash(first)).toBe('first call dies'); |
| 54 | |
| 55 | expect(yield* load('k')).toBe(2); |
| 56 | expect(calls).toBe(2); |
| 57 | }) |
| 58 | ); |
| 59 | |
| 60 | it.effect('an interrupted caller does not take the shared run down with it', () => |
| 61 | Effect.gen(function* () { |
| 62 | const release = yield* Deferred.make<void>(); |
| 63 | let calls = 0; |
| 64 | const load = memoizeInProcess({ |
| 65 | keyOf: (key: string) => key, |
| 66 | make: () => |
| 67 | Effect.gen(function* () { |
| 68 | calls += 1; |
| 69 | yield* Deferred.await(release); |
| 70 | return calls; |
| 71 | }), |
| 72 | }); |
| 73 | |
| 74 | const first = yield* Effect.forkChild(load('k')); |
| 75 | const second = yield* Effect.forkChild(load('k')); |
| 76 | yield* Effect.yieldNow; |
| 77 | |
| 78 | yield* Fiber.interrupt(first); |
| 79 | yield* Deferred.succeed(release, undefined); |
| 80 | |
| 81 | expect(yield* Fiber.join(second)).toBe(1); |
| 82 | expect(yield* load('k')).toBe(1); |
| 83 | expect(calls).toBe(1); |
| 84 | }) |
| 85 | ); |
| 86 | |
| 87 | it.effect('a failure reaches every caller already waiting, then is dropped', () => |
| 88 | Effect.gen(function* () { |
| 89 | const release = yield* Deferred.make<void>(); |
| 90 | let calls = 0; |
| 91 | const outcomes = [Effect.fail(new Error('first call fails')), Effect.succeed(2)]; |
| 92 | const load = memoizeInProcess({ |
| 93 | keyOf: (key: string) => key, |
| 94 | make: () => |
| 95 | Effect.gen(function* () { |
| 96 | const outcome = outcomes[calls++]; |
| 97 | yield* Deferred.await(release); |
| 98 | return yield* outcome; |
| 99 | }), |
| 100 | }); |
| 101 | |
| 102 | const first = yield* Effect.forkChild(Effect.flip(load('k'))); |
| 103 | const second = yield* Effect.forkChild(Effect.flip(load('k'))); |
| 104 | yield* Effect.yieldNow; |
| 105 | yield* Deferred.succeed(release, undefined); |
| 106 | |
| 107 | expect(yield* Fiber.join(first)).toEqual(new Error('first call fails')); |
| 108 | expect(yield* Fiber.join(second)).toEqual(new Error('first call fails')); |
| 109 | expect(yield* load('k')).toBe(2); |
| 110 | expect(calls).toBe(2); |
| 111 | }) |
| 112 | ); |
| 113 | |
| 114 | it.effect('clear() forgets cached results, for every memo in the process', () => |
| 115 | Effect.gen(function* () { |
| 116 | let calls = 0; |
| 117 | const load = memoizeInProcess({ |
| 118 | keyOf: (key: string) => key, |
| 119 | make: () => Effect.sync(() => ++calls), |
| 120 | }); |
| 121 | const other = memoizeInProcess({ |
| 122 | keyOf: (key: string) => key, |
| 123 | make: () => Effect.sync(() => ++calls), |
| 124 | }); |
| 125 | |
| 126 | expect(yield* load('k')).toBe(1); |
| 127 | expect(yield* other('k')).toBe(2); |
| 128 | load.clear(); |
| 129 | expect(yield* load('k')).toBe(3); |
| 130 | expect(yield* other('k')).toBe(2); |
| 131 | |
| 132 | clearInProcessMemos(); |
| 133 | expect(yield* load('k')).toBe(4); |
| 134 | expect(yield* other('k')).toBe(5); |
| 135 | }) |
| 136 | ); |
| 137 | }); |