|
| 1 | +import { HarvestedInstancePool } from './harvested-instance-pool'; |
| 2 | + |
| 3 | +describe('ResourcePool', () => { |
| 4 | + describe('runs tasks as the resources are getting destroyed and replaced', async () => { |
| 5 | + describe('"concurrently"', async () => { |
| 6 | + it('maxInstances=1, maxTasksPerInstance=1', async () => { |
| 7 | + let id = 0; |
| 8 | + const pool = await HarvestedInstancePool.create({ |
| 9 | + maxInstances: 1, |
| 10 | + maxTasksPerInstance: 1, |
| 11 | + create: () => 'resource' + ++id, |
| 12 | + }); |
| 13 | + const runner1 = vitest.fn(); |
| 14 | + const runner2 = vitest.fn(); |
| 15 | + const runner3 = vitest.fn(); |
| 16 | + await Promise.all([ |
| 17 | + pool.runTask(runner1), |
| 18 | + pool.runTask(runner2), |
| 19 | + pool.runTask(runner3), |
| 20 | + ]); |
| 21 | + expect(runner1).toHaveBeenCalledWith('resource1'); |
| 22 | + expect(runner2).toHaveBeenCalledWith('resource2'); |
| 23 | + expect(runner3).toHaveBeenCalledWith('resource3'); |
| 24 | + }); |
| 25 | + |
| 26 | + it('maxInstances=2, maxTasksPerInstance=1', async () => { |
| 27 | + let id = 0; |
| 28 | + const pool = await HarvestedInstancePool.create({ |
| 29 | + maxInstances: 2, |
| 30 | + maxTasksPerInstance: 2, |
| 31 | + create: () => 'resource' + ++id, |
| 32 | + }); |
| 33 | + const runner1 = vitest.fn(); |
| 34 | + const runner2 = vitest.fn(); |
| 35 | + const runner3 = vitest.fn(); |
| 36 | + const runner4 = vitest.fn(); |
| 37 | + const runner5 = vitest.fn(); |
| 38 | + await Promise.all([ |
| 39 | + pool.runTask(runner1), |
| 40 | + pool.runTask(runner2), |
| 41 | + pool.runTask(runner3), |
| 42 | + pool.runTask(runner4), |
| 43 | + pool.runTask(runner5), |
| 44 | + ]); |
| 45 | + expect(runner1).toHaveBeenCalledWith('resource1'); |
| 46 | + expect(runner2).toHaveBeenCalledWith('resource2'); |
| 47 | + expect(runner3).toHaveBeenCalledWith('resource1'); |
| 48 | + expect(runner4).toHaveBeenCalledWith('resource2'); |
| 49 | + expect(runner5).toHaveBeenCalledWith('resource3'); |
| 50 | + }); |
| 51 | + }); |
| 52 | + describe('serially', async () => { |
| 53 | + it('maxInstances=1, maxTasksPerInstance=1', async () => { |
| 54 | + let id = 0; |
| 55 | + const pool = await HarvestedInstancePool.create({ |
| 56 | + maxInstances: 1, |
| 57 | + maxTasksPerInstance: 1, |
| 58 | + create: () => 'resource' + ++id, |
| 59 | + }); |
| 60 | + const runner1 = vitest.fn(); |
| 61 | + const runner2 = vitest.fn(); |
| 62 | + const runner3 = vitest.fn(); |
| 63 | + await pool.runTask(runner1); |
| 64 | + await pool.runTask(runner2); |
| 65 | + await pool.runTask(runner3); |
| 66 | + expect(runner1).toHaveBeenCalledWith('resource1'); |
| 67 | + expect(runner2).toHaveBeenCalledWith('resource2'); |
| 68 | + expect(runner3).toHaveBeenCalledWith('resource3'); |
| 69 | + }); |
| 70 | + |
| 71 | + it('maxInstances=2, maxTasksPerInstance=1', async () => { |
| 72 | + let id = 0; |
| 73 | + const pool = await HarvestedInstancePool.create({ |
| 74 | + maxInstances: 2, |
| 75 | + maxTasksPerInstance: 2, |
| 76 | + create: () => 'resource' + ++id, |
| 77 | + }); |
| 78 | + const runner1 = vitest.fn(); |
| 79 | + const runner2 = vitest.fn(); |
| 80 | + const runner3 = vitest.fn(); |
| 81 | + const runner4 = vitest.fn(); |
| 82 | + const runner5 = vitest.fn(); |
| 83 | + await pool.runTask(runner1); |
| 84 | + await pool.runTask(runner2); |
| 85 | + await pool.runTask(runner3); |
| 86 | + await pool.runTask(runner4); |
| 87 | + await pool.runTask(runner5); |
| 88 | + expect(runner1).toHaveBeenCalledWith('resource1'); |
| 89 | + expect(runner2).toHaveBeenCalledWith('resource2'); |
| 90 | + expect(runner3).toHaveBeenCalledWith('resource1'); |
| 91 | + // At this point, resource3 is the least utilized one |
| 92 | + // with 0 handled tasks – therefore it will be the |
| 93 | + // first one to be used. |
| 94 | + expect(runner4).toHaveBeenCalledWith('resource3'); |
| 95 | + expect(runner5).toHaveBeenCalledWith('resource2'); |
| 96 | + }); |
| 97 | + }); |
| 98 | + }); |
| 99 | + |
| 100 | + it('should destroy resources that have exceeded their tasks limit', async () => { |
| 101 | + const destroy = vitest.fn(); |
| 102 | + const pool = await HarvestedInstancePool.create({ |
| 103 | + maxInstances: 1, |
| 104 | + maxTasksPerInstance: 1, |
| 105 | + create: () => 'resource', |
| 106 | + destroy, |
| 107 | + }); |
| 108 | + await pool.runTask(() => {}); |
| 109 | + await pool.runTask(() => {}); |
| 110 | + |
| 111 | + expect(destroy).toHaveBeenCalledWith('resource'); |
| 112 | + }); |
| 113 | +}); |
0 commit comments