|
1 | 1 | 'use strict' |
2 | 2 |
|
3 | 3 | const assert = require('node:assert/strict') |
4 | | -const { setTimeout } = require('node:timers/promises') |
5 | 4 | const { inspect } = require('node:util') |
6 | 5 |
|
7 | 6 | const { after, afterEach, beforeEach, describe, it } = require('mocha') |
@@ -31,17 +30,18 @@ describe('Plugin', () => { |
31 | 30 | describe('without configuration', () => { |
32 | 31 | beforeEach(async function () { |
33 | 32 | this.timeout(10_000) |
34 | | - tracer = global.tracer = await agent.load('couchbase') |
| 33 | + tracer = global.tracer = await agent.load('couchbase', { enabled: false }) |
35 | 34 | couchbase = proxyquire(`../../../versions/couchbase@${versionKey}`, {}).get() |
36 | 35 | cluster = await couchbase.connect('couchbase://localhost', { |
37 | 36 | username: 'Administrator', |
38 | 37 | password: 'password', |
39 | 38 | }) |
40 | 39 | bucket = cluster.bucket('datadog-test') |
| 40 | + collection = bucket.defaultCollection() |
41 | 41 | if (semver.gte(resolvedVersion, '4.0.0')) { |
42 | | - await waitForBucketConnection(bucket, couchbase) |
| 42 | + await waitForCollectionConnection(collection, couchbase) |
43 | 43 | } |
44 | | - collection = bucket.defaultCollection() |
| 44 | + agent.reload('couchbase', { enabled: true }) |
45 | 45 | }) |
46 | 46 |
|
47 | 47 | afterEach(async () => { |
@@ -194,24 +194,20 @@ describe('Plugin', () => { |
194 | 194 | }) |
195 | 195 |
|
196 | 196 | /** |
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. |
198 | 198 | * |
199 | 199 | * @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 |
203 | 203 | */ |
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 |
213 | 211 | } |
214 | | - await setTimeout(10) |
215 | 212 | } |
216 | | - throw new Error('Couchbase KV connection did not become ready') |
217 | 213 | } |
0 commit comments