|
| 1 | +import { ComputeEngine } from '../../src/compute-engine'; |
| 2 | +import { CancellationError } from '../../src/common/interruptible'; |
| 3 | + |
| 4 | +// Use a dedicated engine with a short timeout for all tests |
| 5 | +let ce: ComputeEngine; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + ce = new ComputeEngine(); |
| 9 | + ce.timeLimit = 200; // 200ms — short enough to catch hangs, long enough for CI |
| 10 | +}); |
| 11 | + |
| 12 | +describe('TIMEOUT', () => { |
| 13 | + describe('Factorial', () => { |
| 14 | + it('normal factorial completes within timeout', () => { |
| 15 | + const result = ce.parse('700!').evaluate(); |
| 16 | + expect(result.isFinite).toBe(true); |
| 17 | + }); |
| 18 | + |
| 19 | + it('nested factorial (700!)! throws CancellationError (sync)', () => { |
| 20 | + expect(() => ce.parse('(700!)!').evaluate()).toThrow(CancellationError); |
| 21 | + }); |
| 22 | + |
| 23 | + it('nested factorial (700!)! throws CancellationError (async)', async () => { |
| 24 | + await expect(ce.parse('(700!)!').evaluateAsync()).rejects.toThrow( |
| 25 | + CancellationError |
| 26 | + ); |
| 27 | + }); |
| 28 | + }); |
| 29 | + |
| 30 | + describe('Factorial2', () => { |
| 31 | + it('normal double factorial completes within timeout', () => { |
| 32 | + const result = ce.parse('10!!').evaluate(); |
| 33 | + expect(result.re).toBe(3840); |
| 34 | + }); |
| 35 | + |
| 36 | + it('large double factorial in bignum mode throws CancellationError', () => { |
| 37 | + ce.precision = 200; // enable bignum path |
| 38 | + ce.timeLimit = 5; // very short — 5ms |
| 39 | + expect(() => ce.parse('100000!!').evaluate()).toThrow(CancellationError); |
| 40 | + }); |
| 41 | + }); |
| 42 | + |
| 43 | + describe('Sum', () => { |
| 44 | + it('small sum completes within timeout', () => { |
| 45 | + const result = ce |
| 46 | + .box(['Sum', 'k', ['Tuple', 'k', 1, 100]]) |
| 47 | + .evaluate(); |
| 48 | + expect(result.re).toBe(5050); |
| 49 | + }); |
| 50 | + |
| 51 | + it('large sum throws CancellationError', () => { |
| 52 | + // Sum k^k for k=1..100000 — each iteration evaluates k^k which gets |
| 53 | + // expensive, and there are 100K of them |
| 54 | + expect(() => |
| 55 | + ce |
| 56 | + .box([ |
| 57 | + 'Sum', |
| 58 | + ['Power', 'k', 'k'], |
| 59 | + ['Tuple', 'k', 1, 100_000], |
| 60 | + ]) |
| 61 | + .evaluate() |
| 62 | + ).toThrow(CancellationError); |
| 63 | + }); |
| 64 | + }); |
| 65 | + |
| 66 | + describe('Product', () => { |
| 67 | + it('small product completes within timeout', () => { |
| 68 | + const result = ce |
| 69 | + .box(['Product', 'k', ['Tuple', 'k', 1, 10]]) |
| 70 | + .evaluate(); |
| 71 | + // 10! = 3628800 |
| 72 | + expect(result.re).toBe(3628800); |
| 73 | + }); |
| 74 | + |
| 75 | + it('large product throws CancellationError', () => { |
| 76 | + expect(() => |
| 77 | + ce |
| 78 | + .box([ |
| 79 | + 'Product', |
| 80 | + ['Power', 'k', 2], |
| 81 | + ['Tuple', 'k', 1, 100_000], |
| 82 | + ]) |
| 83 | + .evaluate() |
| 84 | + ).toThrow(CancellationError); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('Loop', () => { |
| 89 | + it('finite loop completes within timeout', () => { |
| 90 | + const list = ce.function( |
| 91 | + 'List', |
| 92 | + Array.from({ length: 5 }, (_, i) => ce.number(i + 1)) |
| 93 | + ); |
| 94 | + // Loop applies body (a lambda) to each element, returns the last result |
| 95 | + const result = ce |
| 96 | + .box(['Loop', ['Function', ['Multiply', 'x', 2], 'x'], list]) |
| 97 | + .evaluate(); |
| 98 | + expect(result.re).toBe(10); // last element 5 * 2 = 10 |
| 99 | + }); |
| 100 | + |
| 101 | + it('loop over large collection throws CancellationError', () => { |
| 102 | + const list = ce.function( |
| 103 | + 'List', |
| 104 | + Array.from({ length: 50_000 }, (_, i) => ce.number(i)) |
| 105 | + ); |
| 106 | + // Body does expensive work per element |
| 107 | + expect(() => |
| 108 | + ce |
| 109 | + .box([ |
| 110 | + 'Loop', |
| 111 | + ['Function', ['Power', 'x', 'x'], 'x'], |
| 112 | + list, |
| 113 | + ]) |
| 114 | + .evaluate() |
| 115 | + ).toThrow(CancellationError); |
| 116 | + }); |
| 117 | + }); |
| 118 | + |
| 119 | + describe('deadline cleanup', () => { |
| 120 | + it('deadline is reset after timeout so subsequent evaluations work', () => { |
| 121 | + // First: trigger a timeout |
| 122 | + expect(() => ce.parse('(700!)!').evaluate()).toThrow(CancellationError); |
| 123 | + |
| 124 | + // Second: a normal evaluation should still work (deadline was cleaned up) |
| 125 | + const result = ce.parse('10!').evaluate(); |
| 126 | + expect(result.re).toBe(3628800); |
| 127 | + }); |
| 128 | + |
| 129 | + it('deadline is reset after successful evaluation', () => { |
| 130 | + ce.parse('100!').evaluate(); |
| 131 | + |
| 132 | + // _deadline should be undefined after completion |
| 133 | + expect(ce._deadline).toBeUndefined(); |
| 134 | + }); |
| 135 | + }); |
| 136 | +}); |
0 commit comments