|
| 1 | +import { RecommendedPHPVersion } from '@wp-playground/common'; |
| 2 | +import { loadNodeRuntime } from '..'; |
| 3 | +import { PHP, PHPProcessManager, SinglePHPInstanceManager } from '@php-wasm/universal'; |
| 4 | + |
| 5 | +describe('SinglePHPInstanceManager', () => { |
| 6 | + it('should return the PHP instance passed in the constructor', async () => { |
| 7 | + const php = new PHP(await loadNodeRuntime(RecommendedPHPVersion)); |
| 8 | + const mgr = new SinglePHPInstanceManager({ php }); |
| 9 | + |
| 10 | + const primaryPhp = await mgr.getPrimaryPhp(); |
| 11 | + expect(primaryPhp).toBe(php); |
| 12 | + }); |
| 13 | + |
| 14 | + it('should create a PHP instance using the factory when no instance is provided', async () => { |
| 15 | + const phpFactory = vitest.fn( |
| 16 | + async () => new PHP(await loadNodeRuntime(RecommendedPHPVersion)) |
| 17 | + ); |
| 18 | + const mgr = new SinglePHPInstanceManager({ phpFactory }); |
| 19 | + |
| 20 | + expect(phpFactory).not.toHaveBeenCalled(); |
| 21 | + const php = await mgr.getPrimaryPhp(); |
| 22 | + expect(phpFactory).toHaveBeenCalledTimes(1); |
| 23 | + expect(php).toBeInstanceOf(PHP); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should return the same PHP instance on subsequent getPrimaryPhp calls', async () => { |
| 27 | + const phpFactory = vitest.fn( |
| 28 | + async () => new PHP(await loadNodeRuntime(RecommendedPHPVersion)) |
| 29 | + ); |
| 30 | + const mgr = new SinglePHPInstanceManager({ phpFactory }); |
| 31 | + |
| 32 | + const php1 = await mgr.getPrimaryPhp(); |
| 33 | + const php2 = await mgr.getPrimaryPhp(); |
| 34 | + expect(php1).toBe(php2); |
| 35 | + expect(phpFactory).toHaveBeenCalledTimes(1); |
| 36 | + }); |
| 37 | + |
| 38 | + it('should acquire and release the PHP instance', async () => { |
| 39 | + const php = new PHP(await loadNodeRuntime(RecommendedPHPVersion)); |
| 40 | + const mgr = new SinglePHPInstanceManager({ php }); |
| 41 | + |
| 42 | + const acquired = await mgr.acquirePHPInstance(); |
| 43 | + expect(acquired.php).toBe(php); |
| 44 | + |
| 45 | + acquired.reap(); |
| 46 | + |
| 47 | + // Should be able to acquire again after reaping |
| 48 | + const acquired2 = await mgr.acquirePHPInstance(); |
| 49 | + expect(acquired2.php).toBe(php); |
| 50 | + }); |
| 51 | + |
| 52 | + it('should throw an error when trying to acquire twice without reaping', async () => { |
| 53 | + const php = new PHP(await loadNodeRuntime(RecommendedPHPVersion)); |
| 54 | + const mgr = new SinglePHPInstanceManager({ php }); |
| 55 | + |
| 56 | + await mgr.acquirePHPInstance(); |
| 57 | + await expect(() => mgr.acquirePHPInstance()).rejects.toThrowError( |
| 58 | + /already acquired/ |
| 59 | + ); |
| 60 | + }); |
| 61 | + |
| 62 | + it('should throw an error when neither php nor phpFactory is provided', () => { |
| 63 | + expect( |
| 64 | + () => new SinglePHPInstanceManager({}) |
| 65 | + ).toThrowError(/requires either php or phpFactory/); |
| 66 | + }); |
| 67 | + |
| 68 | + it('should only call the factory once even with concurrent getPrimaryPhp calls', async () => { |
| 69 | + const phpFactory = vitest.fn( |
| 70 | + async () => new PHP(await loadNodeRuntime(RecommendedPHPVersion)) |
| 71 | + ); |
| 72 | + const mgr = new SinglePHPInstanceManager({ phpFactory }); |
| 73 | + |
| 74 | + // Make concurrent calls |
| 75 | + const [php1, php2, php3] = await Promise.all([ |
| 76 | + mgr.getPrimaryPhp(), |
| 77 | + mgr.getPrimaryPhp(), |
| 78 | + mgr.getPrimaryPhp(), |
| 79 | + ]); |
| 80 | + |
| 81 | + expect(phpFactory).toHaveBeenCalledTimes(1); |
| 82 | + expect(php1).toBe(php2); |
| 83 | + expect(php2).toBe(php3); |
| 84 | + }); |
| 85 | +}); |
| 86 | + |
| 87 | +describe('PHPProcessManager', () => { |
| 88 | + it('should return the primary PHP instance', async () => { |
| 89 | + const mgr = new PHPProcessManager({ |
| 90 | + phpFactory: async () => |
| 91 | + new PHP(await loadNodeRuntime(RecommendedPHPVersion)), |
| 92 | + maxPhpInstances: 4, |
| 93 | + }); |
| 94 | + |
| 95 | + const php = await mgr.getPrimaryPhp(); |
| 96 | + expect(php).toBeInstanceOf(PHP); |
| 97 | + }); |
| 98 | + |
| 99 | + it('should spawn new PHP instances', async () => { |
| 100 | + const mgr = new PHPProcessManager({ |
| 101 | + phpFactory: async () => |
| 102 | + new PHP(await loadNodeRuntime(RecommendedPHPVersion)), |
| 103 | + maxPhpInstances: 4, |
| 104 | + }); |
| 105 | + |
| 106 | + const php1 = await mgr.acquirePHPInstance(); |
| 107 | + expect(php1.php).toBeInstanceOf(PHP); |
| 108 | + |
| 109 | + const php2 = await mgr.acquirePHPInstance(); |
| 110 | + expect(php1.php).not.toBe(php2.php); |
| 111 | + }); |
| 112 | + |
| 113 | + it('should not spawn primary PHP until the first acquire call', async () => { |
| 114 | + const phpFactory = vitest.fn( |
| 115 | + async () => new PHP(await loadNodeRuntime(RecommendedPHPVersion)) |
| 116 | + ); |
| 117 | + const mgr = new PHPProcessManager({ |
| 118 | + phpFactory, |
| 119 | + maxPhpInstances: 4, |
| 120 | + }); |
| 121 | + |
| 122 | + expect(phpFactory).not.toHaveBeenCalled(); |
| 123 | + await mgr.acquirePHPInstance(); |
| 124 | + expect(phpFactory).toHaveBeenCalled(); |
| 125 | + }); |
| 126 | + |
| 127 | + it('should refuse to spawn more PHP instances than the maximum (limit=2)', async () => { |
| 128 | + const mgr = new PHPProcessManager({ |
| 129 | + phpFactory: async () => |
| 130 | + new PHP(await loadNodeRuntime(RecommendedPHPVersion)), |
| 131 | + maxPhpInstances: 2, |
| 132 | + timeout: 100, |
| 133 | + }); |
| 134 | + |
| 135 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 136 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 137 | + await expect(() => |
| 138 | + mgr.acquirePHPInstance({ considerPrimary: true }) |
| 139 | + ).rejects.toThrowError(/Requested more concurrent PHP instances/); |
| 140 | + }); |
| 141 | + |
| 142 | + it('should refuse to spawn more PHP instances than the maximum (limit=3)', async () => { |
| 143 | + const mgr = new PHPProcessManager({ |
| 144 | + phpFactory: async () => |
| 145 | + new PHP(await loadNodeRuntime(RecommendedPHPVersion)), |
| 146 | + maxPhpInstances: 3, |
| 147 | + timeout: 100, |
| 148 | + }); |
| 149 | + |
| 150 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 151 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 152 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 153 | + await expect(() => |
| 154 | + mgr.acquirePHPInstance({ considerPrimary: true }) |
| 155 | + ).rejects.toThrowError(/Requested more concurrent PHP instances/); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should not start a second PHP instance until the first getInstance() call when the primary instance is busy', async () => { |
| 159 | + const phpFactory = vitest.fn( |
| 160 | + async () => new PHP(await loadNodeRuntime(RecommendedPHPVersion)) |
| 161 | + ); |
| 162 | + const mgr = new PHPProcessManager({ |
| 163 | + phpFactory, |
| 164 | + maxPhpInstances: 5, |
| 165 | + }); |
| 166 | + |
| 167 | + expect(phpFactory).not.toHaveBeenCalled(); |
| 168 | + const php1 = await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 169 | + expect(phpFactory).toHaveBeenCalledTimes(1); |
| 170 | + php1.reap(); |
| 171 | + |
| 172 | + const php2 = await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 173 | + expect(phpFactory).toHaveBeenCalledTimes(1); |
| 174 | + php2.reap(); |
| 175 | + |
| 176 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 177 | + await mgr.acquirePHPInstance({ considerPrimary: true }); |
| 178 | + expect(phpFactory).toHaveBeenCalledTimes(3); |
| 179 | + }); |
| 180 | + |
| 181 | + it('should refuse to spawn two primary PHP instances', async () => { |
| 182 | + const mgr = new PHPProcessManager({ |
| 183 | + phpFactory: async () => |
| 184 | + new PHP(await loadNodeRuntime(RecommendedPHPVersion)), |
| 185 | + maxPhpInstances: 5, |
| 186 | + }); |
| 187 | + |
| 188 | + // A synchronous call. Do not await this promise on purpose. |
| 189 | + mgr.getPrimaryPhp(); |
| 190 | + |
| 191 | + // No await here, because we want to check if a second, |
| 192 | + // synchronous call throws an error if issued before |
| 193 | + // the first call completes asynchronously. |
| 194 | + try { |
| 195 | + mgr.getPrimaryPhp(); |
| 196 | + } catch (e) { |
| 197 | + expect(e).toBeInstanceOf(Error); |
| 198 | + expect((e as Error).message).toContain( |
| 199 | + 'Requested spawning a primary PHP instance' |
| 200 | + ); |
| 201 | + } |
| 202 | + }); |
| 203 | +}); |
0 commit comments