|
| 1 | +/* |
| 2 | + * @adonisjs/queue |
| 3 | + * |
| 4 | + * (c) AdonisJS |
| 5 | + * |
| 6 | + * For the full copyright and license information, please view the LICENSE |
| 7 | + * file that was distributed with this source code. |
| 8 | + */ |
| 9 | + |
| 10 | +import { test } from '@japa/runner' |
| 11 | +import { IgnitorFactory } from '@adonisjs/core/factories' |
| 12 | + |
| 13 | +import { defineConfig, drivers } from '../index.js' |
| 14 | +import { resolveAdapters } from '../src/utils.js' |
| 15 | +import type { QueueConfig } from '../src/types/main.js' |
| 16 | + |
| 17 | +const BASE_URL = new URL('./tmp/', import.meta.url) |
| 18 | + |
| 19 | +test.group('resolveAdapters', () => { |
| 20 | + test('should resolve config providers to adapter factories', async ({ assert }) => { |
| 21 | + const ignitor = new IgnitorFactory() |
| 22 | + .withCoreProviders() |
| 23 | + .withCoreConfig() |
| 24 | + .merge({ |
| 25 | + config: { |
| 26 | + queue: defineConfig({ |
| 27 | + default: 'sync', |
| 28 | + adapters: { |
| 29 | + sync: drivers.sync(), |
| 30 | + }, |
| 31 | + }), |
| 32 | + }, |
| 33 | + rcFileContents: { |
| 34 | + providers: [() => import('../providers/queue_provider.js')], |
| 35 | + }, |
| 36 | + }) |
| 37 | + .create(BASE_URL, { |
| 38 | + importer: (filePath) => { |
| 39 | + if (filePath.startsWith('./') || filePath.startsWith('../')) { |
| 40 | + return import(new URL(filePath, BASE_URL).href) |
| 41 | + } |
| 42 | + |
| 43 | + return import(filePath) |
| 44 | + }, |
| 45 | + }) |
| 46 | + |
| 47 | + const app = ignitor.createApp('console') |
| 48 | + await app.init().then(() => app.boot()) |
| 49 | + |
| 50 | + const config = app.config.get<QueueConfig>('queue') |
| 51 | + |
| 52 | + /** |
| 53 | + * Before resolution, the adapter is a ConfigProvider (object with resolver method) |
| 54 | + */ |
| 55 | + assert.isObject(config.adapters.sync) |
| 56 | + assert.isFunction((config.adapters.sync as any).resolver) |
| 57 | + |
| 58 | + /** |
| 59 | + * After resolution, the adapter should be a factory function |
| 60 | + */ |
| 61 | + const resolvedAdapters = await resolveAdapters(config, app) |
| 62 | + assert.isFunction(resolvedAdapters.sync) |
| 63 | + |
| 64 | + await app.terminate() |
| 65 | + }) |
| 66 | + |
| 67 | + test('should pass through direct factory functions unchanged', async ({ assert }) => { |
| 68 | + const directFactory = () => ({ |
| 69 | + pushOn: async () => {}, |
| 70 | + pushLaterOn: async () => {}, |
| 71 | + pop: async () => null, |
| 72 | + acknowledge: async () => {}, |
| 73 | + fail: async () => {}, |
| 74 | + getFailedJobs: async () => [], |
| 75 | + removeFailedJob: async () => {}, |
| 76 | + clearFailedJobs: async () => {}, |
| 77 | + destroy: async () => {}, |
| 78 | + }) |
| 79 | + |
| 80 | + const ignitor = new IgnitorFactory() |
| 81 | + .withCoreProviders() |
| 82 | + .withCoreConfig() |
| 83 | + .merge({ |
| 84 | + config: { |
| 85 | + queue: defineConfig({ |
| 86 | + default: 'custom', |
| 87 | + adapters: { |
| 88 | + custom: directFactory as any, |
| 89 | + }, |
| 90 | + }), |
| 91 | + }, |
| 92 | + rcFileContents: { |
| 93 | + providers: [() => import('../providers/queue_provider.js')], |
| 94 | + }, |
| 95 | + }) |
| 96 | + .create(BASE_URL, { |
| 97 | + importer: (filePath) => { |
| 98 | + if (filePath.startsWith('./') || filePath.startsWith('../')) { |
| 99 | + return import(new URL(filePath, BASE_URL).href) |
| 100 | + } |
| 101 | + |
| 102 | + return import(filePath) |
| 103 | + }, |
| 104 | + }) |
| 105 | + |
| 106 | + const app = ignitor.createApp('console') |
| 107 | + await app.init().then(() => app.boot()) |
| 108 | + |
| 109 | + const config = app.config.get<QueueConfig>('queue') |
| 110 | + const resolvedAdapters = await resolveAdapters(config, app) |
| 111 | + |
| 112 | + assert.strictEqual(resolvedAdapters.custom, directFactory) |
| 113 | + |
| 114 | + await app.terminate() |
| 115 | + }) |
| 116 | + |
| 117 | + test('worker should accept resolved adapters without throwing', async ({ assert }) => { |
| 118 | + const ignitor = new IgnitorFactory() |
| 119 | + .withCoreProviders() |
| 120 | + .withCoreConfig() |
| 121 | + .merge({ |
| 122 | + config: { |
| 123 | + queue: defineConfig({ |
| 124 | + default: 'sync', |
| 125 | + adapters: { |
| 126 | + sync: drivers.sync(), |
| 127 | + }, |
| 128 | + }), |
| 129 | + }, |
| 130 | + rcFileContents: { |
| 131 | + providers: [() => import('../providers/queue_provider.js')], |
| 132 | + }, |
| 133 | + }) |
| 134 | + .create(BASE_URL, { |
| 135 | + importer: (filePath) => { |
| 136 | + if (filePath.startsWith('./') || filePath.startsWith('../')) { |
| 137 | + return import(new URL(filePath, BASE_URL).href) |
| 138 | + } |
| 139 | + |
| 140 | + return import(filePath) |
| 141 | + }, |
| 142 | + }) |
| 143 | + |
| 144 | + const app = ignitor.createApp('console') |
| 145 | + await app.init().then(() => app.boot()) |
| 146 | + |
| 147 | + const config = app.config.get<QueueConfig>('queue') |
| 148 | + const resolvedAdapters = await resolveAdapters(config, app) |
| 149 | + |
| 150 | + /** |
| 151 | + * Creating a Worker with resolved adapters should not throw |
| 152 | + * "Adapter must be a factory function" error |
| 153 | + */ |
| 154 | + const { Worker } = await import('@boringnode/queue') |
| 155 | + |
| 156 | + assert.doesNotThrow(() => { |
| 157 | + new Worker({ |
| 158 | + ...config, |
| 159 | + adapters: resolvedAdapters, |
| 160 | + }) |
| 161 | + }) |
| 162 | + |
| 163 | + await app.terminate() |
| 164 | + }) |
| 165 | +}) |
0 commit comments