|
| 1 | +import { serial as test } from 'ava' |
| 2 | + |
| 3 | +import './helpers/TestJob' |
| 4 | +import './helpers/Listener' |
| 5 | +import './helpers/Service' |
| 6 | + |
| 7 | +import '../src/Drivers/SQSDriver' |
| 8 | + |
| 9 | +const service = new Service(test, 'sqs', { |
| 10 | + image: 'vsouza/sqs-local', |
| 11 | + port: 9324 |
| 12 | +}) |
| 13 | +const queueName = 'test' |
| 14 | + |
| 15 | +test.beforeEach(async t => { |
| 16 | + t.context.driver = new SQSDriver(null, { |
| 17 | + endpoint: `http://localhost:${service.port}`, |
| 18 | + accessKeyId: 'fake-id', |
| 19 | + secretAccessKey: 'fake-secret', |
| 20 | + queues: { |
| 21 | + [queueName]: { } |
| 22 | + } |
| 23 | + }) |
| 24 | + |
| 25 | + await t.context.driver.ready() |
| 26 | + return t.context.driver.connect() |
| 27 | +}) |
| 28 | + |
| 29 | +test.afterEach.always(async t => { |
| 30 | + const queueUrl = t.context.driver.client.queueUrls[queueName] |
| 31 | + t.context.driver.destroy() |
| 32 | + |
| 33 | + // Delete the queue so it will be recreated |
| 34 | + // This prevents the current Listener's residual long-polling from getting the next test's messages |
| 35 | + await new Promise((resolve, reject) => { |
| 36 | + return t.context.driver.client.client.deleteQueue({ QueueUrl: queueUrl }, err => { |
| 37 | + if(err) { |
| 38 | + return reject(err) |
| 39 | + } |
| 40 | + |
| 41 | + return resolve() |
| 42 | + }) |
| 43 | + }) |
| 44 | +}) |
| 45 | + |
| 46 | +test('dispatch', async t => { |
| 47 | + const payload = { time: Date.now() } |
| 48 | + const job = new TestJob({ ...payload }) |
| 49 | + |
| 50 | + await t.context.driver.dispatch(job) |
| 51 | + |
| 52 | + return Listener(t.context.driver, job => t.deepEqual(job.data.data, payload)) |
| 53 | +}) |
| 54 | + |
| 55 | +test('retry dispatch', async t => { |
| 56 | + const payload = { time: Date.now() } |
| 57 | + const job = new TestJob({ ...payload }) |
| 58 | + let tries = 0 |
| 59 | + |
| 60 | + await t.context.driver.dispatch(job.$tries(2)) |
| 61 | + |
| 62 | + return Listener(t.context.driver, job => { |
| 63 | + t.is(job.tries, 2) |
| 64 | + t.deepEqual(job.data.data, payload) |
| 65 | + |
| 66 | + if(++tries === 1 || tries > 2) { |
| 67 | + throw new Error |
| 68 | + } |
| 69 | + }) |
| 70 | +}) |
| 71 | + |
| 72 | +test('multi dispatch', t => { |
| 73 | + let count = 0 |
| 74 | + |
| 75 | + setTimeout(() => t.context.driver.dispatch(new TestJob({ id: 1 })), 50) |
| 76 | + setTimeout(() => t.context.driver.dispatch(new TestJob({ id: 2 })), 100) |
| 77 | + setTimeout(() => t.context.driver.dispatch(new TestJob({ id: 3 })), 200) |
| 78 | + setTimeout(() => t.context.driver.dispatch(new TestJob({ id: 4 })), 400) |
| 79 | + |
| 80 | + return Listener(t.context.driver, () => ++count < 4).then(() => t.is(count, 4)) |
| 81 | +}) |
| 82 | + |
| 83 | +test('delayed dispatch', async t => { |
| 84 | + const payload = { time: Date.now() } |
| 85 | + const job = new TestJob({ ...payload }) |
| 86 | + |
| 87 | + const dispatchedAt = Date.now() |
| 88 | + await t.context.driver.dispatch(job.$delay(1000)) |
| 89 | + |
| 90 | + return Listener(t.context.driver, job => { |
| 91 | + t.is(Date.now() - dispatchedAt >= 1000, true) |
| 92 | + t.deepEqual(job.data.data, payload) |
| 93 | + }) |
| 94 | +}) |
0 commit comments