|
| 1 | +import {test, expect} from '@playwright/test'; |
| 2 | +import Neo from '../../../../src/Neo.mjs'; |
| 3 | +import Base from '../../../../src/core/Base.mjs'; |
| 4 | + |
| 5 | +test.describe('core/Base Timeout Handling', () => { |
| 6 | + test('timeout() should resolve after the specified delay', async () => { |
| 7 | + class TestClass extends Base { |
| 8 | + static config = { |
| 9 | + className: 'Neo.test.TimeoutTestClass' |
| 10 | + } |
| 11 | + } |
| 12 | + Neo.setupClass(TestClass); |
| 13 | + |
| 14 | + const instance = Neo.create(TestClass); |
| 15 | + const start = Date.now(); |
| 16 | + await instance.timeout(100); |
| 17 | + const end = Date.now(); |
| 18 | + |
| 19 | + expect(end - start).toBeGreaterThanOrEqual(95); // Allow small margin |
| 20 | + }); |
| 21 | + |
| 22 | + test('timeout() should be rejected with Neo.isDestroyed when instance is destroyed', async () => { |
| 23 | + class TestClass extends Base { |
| 24 | + static config = { |
| 25 | + className: 'Neo.test.TimeoutDestroyTestClass' |
| 26 | + } |
| 27 | + } |
| 28 | + Neo.setupClass(TestClass); |
| 29 | + |
| 30 | + const instance = Neo.create(TestClass); |
| 31 | + let error; |
| 32 | + |
| 33 | + // Start timeout but don't await immediately to allow destruction |
| 34 | + const timeoutPromise = instance.timeout(500); |
| 35 | + |
| 36 | + // Destroy instance before timeout completes |
| 37 | + instance.destroy(); |
| 38 | + |
| 39 | + try { |
| 40 | + await timeoutPromise; |
| 41 | + } catch (e) { |
| 42 | + error = e; |
| 43 | + } |
| 44 | + |
| 45 | + expect(error).toBe(Neo.isDestroyed); |
| 46 | + }); |
| 47 | + |
| 48 | + test('Multiple timeouts should be handled correctly', async () => { |
| 49 | + class TestClass extends Base { |
| 50 | + static config = { |
| 51 | + className: 'Neo.test.MultipleTimeoutTestClass' |
| 52 | + } |
| 53 | + } |
| 54 | + Neo.setupClass(TestClass); |
| 55 | + |
| 56 | + const instance = Neo.create(TestClass); |
| 57 | + let error1, error2; |
| 58 | + |
| 59 | + const p1 = instance.timeout(200); |
| 60 | + const p2 = instance.timeout(400); |
| 61 | + |
| 62 | + instance.destroy(); |
| 63 | + |
| 64 | + try { |
| 65 | + await p1; |
| 66 | + } catch (e) { |
| 67 | + error1 = e; |
| 68 | + } |
| 69 | + |
| 70 | + try { |
| 71 | + await p2; |
| 72 | + } catch (e) { |
| 73 | + error2 = e; |
| 74 | + } |
| 75 | + |
| 76 | + expect(error1).toBe(Neo.isDestroyed); |
| 77 | + expect(error2).toBe(Neo.isDestroyed); |
| 78 | + }); |
| 79 | + |
| 80 | + test('Completed timeouts should not prevent destruction or throw errors', async () => { |
| 81 | + class TestClass extends Base { |
| 82 | + static config = { |
| 83 | + className: 'Neo.test.CompletedTimeoutTestClass' |
| 84 | + } |
| 85 | + } |
| 86 | + Neo.setupClass(TestClass); |
| 87 | + |
| 88 | + const instance = Neo.create(TestClass); |
| 89 | + |
| 90 | + await instance.timeout(50); // let it finish |
| 91 | + |
| 92 | + // Should not throw |
| 93 | + instance.destroy(); |
| 94 | + expect(instance.isDestroyed).toBe(true); |
| 95 | + }); |
| 96 | +}); |
0 commit comments