|
| 1 | +import { format } from 'winston' |
| 2 | +import Transport from 'winston-transport' |
| 3 | + |
| 4 | +import { createLogger } from '../../../c8/lib/C8Logger' |
| 5 | +import { CamundaRestClient } from '../../../c8/lib/CamundaRestClient' |
| 6 | + |
| 7 | +jest.setTimeout(30000) |
| 8 | + |
| 9 | +// Custom log transport to suppress errors in the console, and allow them to be examined |
| 10 | +class MemoryTransport extends Transport { |
| 11 | + logs: { message: string; level: string; timestamp: string }[] |
| 12 | + errors: Error[] |
| 13 | + constructor(opts?: Transport.TransportStreamOptions | undefined) { |
| 14 | + super(opts) |
| 15 | + this.logs = [] |
| 16 | + this.errors = [] |
| 17 | + } |
| 18 | + log( |
| 19 | + info: Error | { message: string; level: string; timestamp: string }, |
| 20 | + callback: () => void |
| 21 | + ) { |
| 22 | + // Immediately emit the logged event (this is required for Winston) |
| 23 | + setImmediate(() => { |
| 24 | + this.emit('logged', info) |
| 25 | + }) |
| 26 | + if (info instanceof Error) { |
| 27 | + this.errors.push(info) |
| 28 | + } else { |
| 29 | + this.logs.push(info) |
| 30 | + } |
| 31 | + callback() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +test('REST worker will backoff on UNAUTHENTICATED', (done) => { |
| 36 | + let durations = 0 |
| 37 | + let pollCountBackingOffWorker = 0 |
| 38 | + const backoffs: number[] = [] |
| 39 | + |
| 40 | + const transportBackingOffWorker = new MemoryTransport() |
| 41 | + const logBackingOffWorker = createLogger({ |
| 42 | + transports: [transportBackingOffWorker], |
| 43 | + }) |
| 44 | + |
| 45 | + const restClientBackingoffWorker = new CamundaRestClient({ |
| 46 | + config: { CAMUNDA_AUTH_STRATEGY: 'NONE', logger: logBackingOffWorker }, |
| 47 | + }) |
| 48 | + const backingOffWorker = restClientBackingoffWorker.createJobWorker({ |
| 49 | + type: 'unauthenticated-worker', |
| 50 | + jobHandler: async () => { |
| 51 | + throw new Error('Not Implemented') // is never called |
| 52 | + }, |
| 53 | + worker: 'unauthenticated-test-worker', |
| 54 | + maxJobsToActivate: 10, |
| 55 | + timeout: 30000, |
| 56 | + }) |
| 57 | + |
| 58 | + backingOffWorker.on('backoff', (duration) => { |
| 59 | + durations += duration |
| 60 | + backoffs.push(duration) |
| 61 | + }) |
| 62 | + backingOffWorker.on('poll', () => { |
| 63 | + pollCountBackingOffWorker++ |
| 64 | + }) |
| 65 | + setTimeout(() => { |
| 66 | + backingOffWorker.stop() |
| 67 | + expect(durations).toBe(20000) |
| 68 | + expect(pollCountBackingOffWorker).toBe(3) |
| 69 | + // Assert that each backoff is greater than the previous one; ie: the backoff is increasing |
| 70 | + for (let i = 1; i < backoffs.length; i++) { |
| 71 | + expect(backoffs[i]).toBeGreaterThan(backoffs[i - 1]) |
| 72 | + } |
| 73 | + done() |
| 74 | + }, 25000) |
| 75 | +}) |
| 76 | + |
| 77 | +test('REST worker uses a supplied custom max backoff', (done) => { |
| 78 | + let pollCount = 0 |
| 79 | + const backoffs: number[] = [] |
| 80 | + const MAX_BACKOFF = 2000 |
| 81 | + const transport = new MemoryTransport() |
| 82 | + const logger = createLogger({ |
| 83 | + transports: [transport], |
| 84 | + }) |
| 85 | + const restClient = new CamundaRestClient({ |
| 86 | + config: { |
| 87 | + CAMUNDA_AUTH_STRATEGY: 'NONE', |
| 88 | + CAMUNDA_JOB_WORKER_MAX_BACKOFF_MS: MAX_BACKOFF, |
| 89 | + logger, |
| 90 | + }, |
| 91 | + }) |
| 92 | + |
| 93 | + const w = restClient.createJobWorker({ |
| 94 | + type: 'unauthenticated-worker', |
| 95 | + jobHandler: async () => { |
| 96 | + throw new Error('Not Implemented') // is never called |
| 97 | + }, |
| 98 | + worker: 'unauthenticated-test-worker', |
| 99 | + maxJobsToActivate: 10, |
| 100 | + timeout: 30000, |
| 101 | + }) |
| 102 | + w.on('backoff', (duration) => { |
| 103 | + expect(duration).toBeLessThanOrEqual(MAX_BACKOFF) |
| 104 | + backoffs.push(duration) |
| 105 | + }) |
| 106 | + w.on('poll', () => { |
| 107 | + pollCount++ |
| 108 | + }) |
| 109 | + setTimeout(() => { |
| 110 | + w.stop() |
| 111 | + expect(pollCount).toBe(3) |
| 112 | + expect(backoffs.length).toBe(3) |
| 113 | + done() |
| 114 | + }, 10000) |
| 115 | +}) |
| 116 | + |
| 117 | +/** |
| 118 | + * This test is deliberately commented out. The behaviour was manually verified on 5 Feb, 2025. |
| 119 | + * |
| 120 | + * Testing the outer bound of the token endpoint backoff when it is hardcoded to 15s takes a long time. |
| 121 | + * Making the max token endpoint backoff configurable would make this easier to test. |
| 122 | + * |
| 123 | + */ |
| 124 | +xtest('REST worker uses a supplied custom max backoff with invalid secret', (done) => { |
| 125 | + let durations = 0 |
| 126 | + let pollCount = 0 |
| 127 | + const backoffs: number[] = [] |
| 128 | + |
| 129 | + /** |
| 130 | + * Suppress all logging output with this custom logger. The token endpoint will emit error logs during this test. |
| 131 | + */ |
| 132 | + const memoryTransport = new MemoryTransport() |
| 133 | + |
| 134 | + const logger = createLogger({ |
| 135 | + format: format.combine(format.timestamp(), format.json()), |
| 136 | + transports: [memoryTransport], |
| 137 | + }) |
| 138 | + |
| 139 | + const MAX_BACKOFF = 2000 |
| 140 | + const restClient = new CamundaRestClient({ |
| 141 | + config: { |
| 142 | + CAMUNDA_TOKEN_DISK_CACHE_DISABLE: true, |
| 143 | + ZEEBE_CLIENT_ID: 'Does-not-exist', |
| 144 | + ZEEBE_CLIENT_SECRET: 'NONE', |
| 145 | + CAMUNDA_JOB_WORKER_MAX_BACKOFF_MS: MAX_BACKOFF, |
| 146 | + logger, |
| 147 | + }, |
| 148 | + }) |
| 149 | + |
| 150 | + const w = restClient.createJobWorker({ |
| 151 | + type: 'unauthenticated-worker', |
| 152 | + jobHandler: async () => { |
| 153 | + throw new Error('Not Implemented') // is never called |
| 154 | + }, |
| 155 | + worker: 'unauthenticated-test-worker', |
| 156 | + maxJobsToActivate: 10, |
| 157 | + timeout: 30000, |
| 158 | + autoStart: false, // Do not autostart, so we can attach event listeners before it starts polling |
| 159 | + }) |
| 160 | + w.on('backoff', (duration) => { |
| 161 | + durations += duration |
| 162 | + backoffs.push(duration) |
| 163 | + }) |
| 164 | + w.on('poll', () => { |
| 165 | + pollCount++ |
| 166 | + }) |
| 167 | + w.start() // Start the worker now that the event listeners are attached |
| 168 | + setTimeout(() => { |
| 169 | + w.stop() |
| 170 | + const logs = memoryTransport.logs |
| 171 | + // Convert timestamp strings to milliseconds since epoch. |
| 172 | + const times = logs |
| 173 | + .filter((log) => |
| 174 | + log.message.includes('Backing off worker poll due to failure.') |
| 175 | + ) |
| 176 | + .map((log) => new Date(log.timestamp).getTime()) |
| 177 | + console.log('times.length', times.length) |
| 178 | + // Calculate delays between consecutive errors. |
| 179 | + const delays: number[] = [] |
| 180 | + for (let i = 1; i < times.length; i++) { |
| 181 | + delays.push(times[i] - times[i - 1]) |
| 182 | + } |
| 183 | + // Assert that each delay is less than or equal to the max backoff delay |
| 184 | + for (let i = 1; i < delays.length; i++) { |
| 185 | + // expect(delays[i] - delays[i - 1]).toBeLessThanOrEqual(MAX_BACKOFF) |
| 186 | + console.log(delays[i]) |
| 187 | + } |
| 188 | + expect(pollCount).toBe(4) |
| 189 | + expect(durations).toBe(8000) |
| 190 | + done() |
| 191 | + }, 20000) |
| 192 | +}) |
0 commit comments