Skip to content

Commit f1cff30

Browse files
rochdevwatson
authored andcommitted
test: rewrite aws sqs batch test to be deterministic (#6083)
1 parent b029913 commit f1cff30

File tree

1 file changed

+41
-41
lines changed

1 file changed

+41
-41
lines changed

packages/datadog-plugin-aws-sdk/test/sqs.spec.js

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,22 @@ describe('Plugin', () => {
176176
})
177177
})
178178

179-
it('should propagate the tracing context from the producer to the consumer in batch operations', (done) => {
179+
it('should propagate the tracing context from the producer to the consumer in batch operations', async () => {
180180
let parentId
181181
let traceId
182182

183-
agent.assertSomeTraces(traces => {
183+
const sendPromise = new Promise((resolve, reject) => {
184+
sqs.sendMessageBatch({
185+
Entries: [
186+
{ Id: '1', MessageBody: 'test batch propagation 1' },
187+
{ Id: '2', MessageBody: 'test batch propagation 2' },
188+
{ Id: '3', MessageBody: 'test batch propagation 3' }
189+
],
190+
QueueUrl
191+
}, (err) => err ? reject(err) : resolve())
192+
})
193+
194+
const parentPromise = agent.assertSomeTraces(traces => {
184195
const span = traces[0][0]
185196

186197
expect(span.resource.startsWith('sendMessageBatch')).to.equal(true)
@@ -192,56 +203,45 @@ describe('Plugin', () => {
192203
traceId = span.trace_id.toString()
193204
})
194205

195-
let batchChildSpans = 0
196-
agent.assertSomeTraces(traces => {
197-
const span = traces[0][0]
206+
await Promise.all([sendPromise, parentPromise])
198207

199-
expect(parentId).to.be.a('string')
200-
expect(span.parent_id.toString()).to.equal(parentId)
201-
expect(span.trace_id.toString()).to.equal(traceId)
202-
batchChildSpans += 1
203-
expect(batchChildSpans).to.equal(3)
204-
}, { timeoutMs: 2000 }).then(done, done)
208+
async function receiveAndAssertMessage () {
209+
const childPromise = agent.assertSomeTraces(traces => {
210+
const span = traces[0][0]
205211

206-
sqs.sendMessageBatch(
207-
{
208-
Entries: [
209-
{
210-
Id: '1',
211-
MessageBody: 'test batch propagation 1'
212-
},
213-
{
214-
Id: '2',
215-
MessageBody: 'test batch propagation 2'
216-
},
217-
{
218-
Id: '3',
219-
MessageBody: 'test batch propagation 3'
220-
}
221-
],
222-
QueueUrl
223-
}, (err) => {
224-
if (err) return done(err)
212+
expect(parentId).to.be.a('string')
213+
expect(span.parent_id.toString()).to.equal(parentId)
214+
expect(span.trace_id.toString()).to.equal(traceId)
215+
})
225216

226-
function receiveMessage () {
227-
sqs.receiveMessage({
228-
QueueUrl,
229-
MaxNumberOfMessages: 1
230-
}, (err, data) => {
231-
if (err) return done(err)
217+
const receiveMessage = new Promise((resolve, reject) => {
218+
sqs.receiveMessage({
219+
QueueUrl,
220+
MaxNumberOfMessages: 1
221+
}, (err, data) => {
222+
if (err) return reject(err)
232223

224+
try {
233225
for (const message in data.Messages) {
234226
const recordData = data.Messages[message].MessageAttributes
235227
expect(recordData).to.have.property('_datadog')
236228
const traceContext = JSON.parse(recordData._datadog.StringValue)
237229
expect(traceContext).to.have.property('x-datadog-trace-id')
238230
}
239-
})
240-
}
241-
receiveMessage()
242-
receiveMessage()
243-
receiveMessage()
231+
232+
resolve()
233+
} catch (e) {
234+
reject(e)
235+
}
236+
})
244237
})
238+
239+
await Promise.all([childPromise, receiveMessage])
240+
}
241+
242+
await receiveAndAssertMessage()
243+
await receiveAndAssertMessage()
244+
await receiveAndAssertMessage()
245245
})
246246

247247
it('should run the consumer in the context of its span', (done) => {

0 commit comments

Comments
 (0)