Skip to content

Commit d43009c

Browse files
committed
test(couchbase): wait for a usable KV connection (#9792)
Couchbase 4.0 can report a healthy ping before its collection KV connection accepts operations. The first operation then exhausts the 2.5-second timeout although later calls succeed.
1 parent c03e8cd commit d43009c

1 file changed

Lines changed: 15 additions & 19 deletions

File tree

packages/datadog-plugin-couchbase/test/index.spec.js

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
'use strict'
22

33
const assert = require('node:assert/strict')
4-
const { setTimeout } = require('node:timers/promises')
54
const { inspect } = require('node:util')
65

76
const { after, afterEach, beforeEach, describe, it } = require('mocha')
@@ -31,17 +30,18 @@ describe('Plugin', () => {
3130
describe('without configuration', () => {
3231
beforeEach(async function () {
3332
this.timeout(10_000)
34-
tracer = global.tracer = await agent.load('couchbase')
33+
tracer = global.tracer = await agent.load('couchbase', { enabled: false })
3534
couchbase = proxyquire(`../../../versions/couchbase@${versionKey}`, {}).get()
3635
cluster = await couchbase.connect('couchbase://localhost', {
3736
username: 'Administrator',
3837
password: 'password',
3938
})
4039
bucket = cluster.bucket('datadog-test')
40+
collection = bucket.defaultCollection()
4141
if (semver.gte(resolvedVersion, '4.0.0')) {
42-
await waitForBucketConnection(bucket, couchbase)
42+
await waitForCollectionConnection(collection, couchbase)
4343
}
44-
collection = bucket.defaultCollection()
44+
agent.reload('couchbase', { enabled: true })
4545
})
4646

4747
afterEach(async () => {
@@ -194,24 +194,20 @@ describe('Plugin', () => {
194194
})
195195

196196
/**
197-
* Couchbase 4.x resolves connect before bucket() finishes opening its KV connection.
197+
* Couchbase 4.x resolves connect before its collection KV connection is usable.
198198
*
199199
* @param {{
200-
* ping(options: { serviceTypes: string[] }): Promise<{ services: Record<string, Array<{ state: number }>> }>
201-
* }} bucket
202-
* @param {{ ServiceType: { KeyValue: string }, PingState: { Ok: number } }} couchbase
200+
* exists(key: string): Promise<unknown>
201+
* }} collection
202+
* @param {{ TimeoutError: typeof Error }} couchbase
203203
*/
204-
async function waitForBucketConnection (bucket, couchbase) {
205-
const deadline = Date.now() + 8_000
206-
while (Date.now() < deadline) {
207-
const { services } = await bucket.ping({
208-
serviceTypes: [couchbase.ServiceType.KeyValue],
209-
})
210-
const endpoints = services[couchbase.ServiceType.KeyValue] ?? []
211-
for (const { state } of endpoints) {
212-
if (state === couchbase.PingState.Ok) return
204+
async function waitForCollectionConnection (collection, couchbase) {
205+
for (let attempt = 0; attempt < 3; attempt++) {
206+
try {
207+
await collection.exists('dd-trace-readiness')
208+
return
209+
} catch (error) {
210+
if (!(error instanceof couchbase.TimeoutError) || attempt === 2) throw error
213211
}
214-
await setTimeout(10)
215212
}
216-
throw new Error('Couchbase KV connection did not become ready')
217213
}

0 commit comments

Comments
 (0)