Skip to content

Commit 6c879ec

Browse files
committed
observability-test: deflake tests by ensuring SessionPool creation already happened before running
1 parent 21fe14a commit 6c879ec

File tree

3 files changed

+33
-82
lines changed

3 files changed

+33
-82
lines changed

observability-test/session-pool.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@
1616

1717
import * as assert from 'assert';
1818
import {before, beforeEach, afterEach, describe, it} from 'mocha';
19-
import * as events from 'events';
2019
import * as extend from 'extend';
2120
import PQueue from 'p-queue';
2221
import * as proxyquire from 'proxyquire';
2322
import * as sinon from 'sinon';
2423
import stackTrace = require('stack-trace');
25-
import timeSpan = require('time-span');
2624
const {
2725
AlwaysOnSampler,
2826
NodeTracerProvider,
@@ -54,8 +52,6 @@ class FakeTransaction {
5452

5553
const fakeStackTrace = extend({}, stackTrace);
5654

57-
function noop() {}
58-
5955
describe('SessionPool', () => {
6056
let sessionPool: sp.SessionPool;
6157
// tslint:disable-next-line variable-name

observability-test/spanner.ts

Lines changed: 30 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ describe('EndToEnd', () => {
147147

148148
const instance = spanner.instance('instance');
149149
database = instance.database('database');
150+
151+
// To deflake expectations of session creation, let's
152+
// issue out a warm-up request request that'll ensure
153+
// that the SessionPool is created deterministically.
154+
const [rows] = await database.run('SELECT 1');
155+
// Clear out any present traces to make a clean slate for testing.
156+
traceExporter.forceFlush();
157+
traceExporter.reset();
150158
});
151159

152160
afterEach(() => {
@@ -160,7 +168,6 @@ describe('EndToEnd', () => {
160168
it('getSessions', async () => {
161169
const [rows] = await database.getSessions();
162170

163-
traceExporter.forceFlush();
164171
const spans = traceExporter.getFinishedSpans();
165172

166173
const actualSpanNames: string[] = [];
@@ -172,22 +179,14 @@ describe('EndToEnd', () => {
172179
});
173180
});
174181

175-
const expectedSpanNames = [
176-
'CloudSpanner.Database.batchCreateSessions',
177-
'CloudSpanner.SessionPool.createSessions',
178-
'CloudSpanner.Database.getSessions',
179-
];
182+
const expectedSpanNames = ['CloudSpanner.Database.getSessions'];
180183
assert.deepStrictEqual(
181184
actualSpanNames,
182185
expectedSpanNames,
183186
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
184187
);
185188

186-
const expectedEventNames = [
187-
'Requesting 25 sessions',
188-
'Creating 25 sessions',
189-
'Requested for 25 sessions returned 25',
190-
];
189+
const expectedEventNames = [];
191190
assert.deepStrictEqual(
192191
actualEventNames,
193192
expectedEventNames,
@@ -215,8 +214,6 @@ describe('EndToEnd', () => {
215214
});
216215

217216
const expectedSpanNames = [
218-
'CloudSpanner.Database.batchCreateSessions',
219-
'CloudSpanner.SessionPool.createSessions',
220217
'CloudSpanner.Snapshot.begin',
221218
'CloudSpanner.Database.getSnapshot',
222219
'CloudSpanner.Snapshot.runStream',
@@ -229,13 +226,10 @@ describe('EndToEnd', () => {
229226
);
230227

231228
const expectedEventNames = [
232-
'Requesting 25 sessions',
233-
'Creating 25 sessions',
234-
'Requested for 25 sessions returned 25',
235229
'Begin Transaction',
236230
'Transaction Creation Done',
237231
'Acquiring session',
238-
'Waiting for a session to become available',
232+
'Cache hit: has usable session',
239233
'Acquired session',
240234
];
241235
assert.deepStrictEqual(
@@ -266,23 +260,16 @@ describe('EndToEnd', () => {
266260
});
267261
});
268262

269-
const expectedSpanNames = [
270-
'CloudSpanner.Database.batchCreateSessions',
271-
'CloudSpanner.SessionPool.createSessions',
272-
'CloudSpanner.Database.getTransaction',
273-
];
263+
const expectedSpanNames = ['CloudSpanner.Database.getTransaction'];
274264
assert.deepStrictEqual(
275265
actualSpanNames,
276266
expectedSpanNames,
277267
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
278268
);
279269

280270
const expectedEventNames = [
281-
'Requesting 25 sessions',
282-
'Creating 25 sessions',
283-
'Requested for 25 sessions returned 25',
284271
'Acquiring session',
285-
'Waiting for a session to become available',
272+
'Cache hit: has usable session',
286273
'Acquired session',
287274
'Using Session',
288275
];
@@ -315,8 +302,6 @@ describe('EndToEnd', () => {
315302
});
316303

317304
const expectedSpanNames = [
318-
'CloudSpanner.Database.batchCreateSessions',
319-
'CloudSpanner.SessionPool.createSessions',
320305
'CloudSpanner.Snapshot.runStream',
321306
'CloudSpanner.Database.runStream',
322307
];
@@ -327,11 +312,8 @@ describe('EndToEnd', () => {
327312
);
328313

329314
const expectedEventNames = [
330-
'Requesting 25 sessions',
331-
'Creating 25 sessions',
332-
'Requested for 25 sessions returned 25',
333315
'Acquiring session',
334-
'Waiting for a session to become available',
316+
'Cache hit: has usable session',
335317
'Acquired session',
336318
'Using Session',
337319
];
@@ -366,8 +348,6 @@ describe('EndToEnd', () => {
366348
});
367349

368350
const expectedSpanNames = [
369-
'CloudSpanner.Database.batchCreateSessions',
370-
'CloudSpanner.SessionPool.createSessions',
371351
'CloudSpanner.Snapshot.runStream',
372352
'CloudSpanner.Database.runStream',
373353
'CloudSpanner.Database.run',
@@ -399,11 +379,8 @@ describe('EndToEnd', () => {
399379
);
400380

401381
const expectedEventNames = [
402-
'Requesting 25 sessions',
403-
'Creating 25 sessions',
404-
'Requested for 25 sessions returned 25',
405382
'Acquiring session',
406-
'Waiting for a session to become available',
383+
'Cache hit: has usable session',
407384
'Acquired session',
408385
'Using Session',
409386
];
@@ -433,8 +410,6 @@ describe('EndToEnd', () => {
433410
});
434411

435412
const expectedSpanNames = [
436-
'CloudSpanner.Database.batchCreateSessions',
437-
'CloudSpanner.SessionPool.createSessions',
438413
'CloudSpanner.Database.runTransaction',
439414
'CloudSpanner.Snapshot.runStream',
440415
'CloudSpanner.Snapshot.run',
@@ -446,11 +421,8 @@ describe('EndToEnd', () => {
446421
);
447422

448423
const expectedEventNames = [
449-
'Requesting 25 sessions',
450-
'Creating 25 sessions',
451-
'Requested for 25 sessions returned 25',
452424
'Acquiring session',
453-
'Waiting for a session to become available',
425+
'Cache hit: has usable session',
454426
'Acquired session',
455427
'Transaction Creation Done',
456428
];
@@ -484,8 +456,6 @@ describe('EndToEnd', () => {
484456
});
485457

486458
const expectedSpanNames = [
487-
'CloudSpanner.Database.batchCreateSessions',
488-
'CloudSpanner.SessionPool.createSessions',
489459
'CloudSpanner.Transaction.commit',
490460
'CloudSpanner.Database.writeAtLeastOnce',
491461
];
@@ -496,13 +466,10 @@ describe('EndToEnd', () => {
496466
);
497467

498468
const expectedEventNames = [
499-
'Requesting 25 sessions',
500-
'Creating 25 sessions',
501-
'Requested for 25 sessions returned 25',
502469
'Starting Commit',
503470
'Commit Done',
504471
'Acquiring session',
505-
'Waiting for a session to become available',
472+
'Cache hit: has usable session',
506473
'Acquired session',
507474
'Using Session',
508475
];
@@ -517,7 +484,6 @@ describe('EndToEnd', () => {
517484
});
518485

519486
it('batchCreateSessions', done => {
520-
const blankMutations = new MutationSet();
521487
database.batchCreateSessions(5, (err, sessions) => {
522488
assert.ifError(err);
523489

@@ -533,22 +499,14 @@ describe('EndToEnd', () => {
533499
});
534500
});
535501

536-
const expectedSpanNames = [
537-
'CloudSpanner.Database.batchCreateSessions',
538-
'CloudSpanner.SessionPool.createSessions',
539-
'CloudSpanner.Database.batchCreateSessions',
540-
];
502+
const expectedSpanNames = ['CloudSpanner.Database.batchCreateSessions'];
541503
assert.deepStrictEqual(
542504
actualSpanNames,
543505
expectedSpanNames,
544506
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
545507
);
546508

547-
const expectedEventNames = [
548-
'Requesting 25 sessions',
549-
'Creating 25 sessions',
550-
'Requested for 25 sessions returned 25',
551-
];
509+
const expectedEventNames = [];
552510
assert.deepStrictEqual(
553511
actualEventNames,
554512
expectedEventNames,
@@ -725,9 +683,17 @@ describe('ObservabilityOptions injection and propagation', async () => {
725683
});
726684

727685
let database: Database;
728-
beforeEach(() => {
686+
beforeEach(async () => {
729687
const instance = spanner.instance('instance');
730688
database = instance.database('database');
689+
690+
// To deflake expectations of session creation, let's
691+
// issue out a warm-up request request that'll ensure
692+
// that the SessionPool is created deterministically.
693+
const [rows] = await database.run('SELECT 1');
694+
// Clear out any present traces to make a clean slate for testing.
695+
traceExporter.forceFlush();
696+
traceExporter.reset();
731697
});
732698

733699
afterEach(() => {
@@ -753,8 +719,6 @@ describe('ObservabilityOptions injection and propagation', async () => {
753719
});
754720

755721
const expectedSpanNames = [
756-
'CloudSpanner.Database.batchCreateSessions',
757-
'CloudSpanner.SessionPool.createSessions',
758722
'CloudSpanner.Database.getTransaction',
759723
'CloudSpanner.Snapshot.runStream',
760724
'CloudSpanner.Snapshot.run',
@@ -766,11 +730,8 @@ describe('ObservabilityOptions injection and propagation', async () => {
766730
);
767731

768732
const expectedEventNames = [
769-
'Requesting 25 sessions',
770-
'Creating 25 sessions',
771-
'Requested for 25 sessions returned 25',
772733
'Acquiring session',
773-
'Waiting for a session to become available',
734+
'Cache hit: has usable session',
774735
'Acquired session',
775736
'Using Session',
776737
'Transaction Creation Done',
@@ -827,7 +788,7 @@ describe('ObservabilityOptions injection and propagation', async () => {
827788
'Begin Transaction',
828789
'Transaction Creation Done',
829790
];
830-
assert.strictEqual(
791+
assert.deepStrictEqual(
831792
actualEventNames.every(value => expectedEventNames.includes(value)),
832793
true,
833794
`Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}`
@@ -864,10 +825,6 @@ describe('ObservabilityOptions injection and propagation', async () => {
864825
});
865826

866827
const expectedSpanNames = [
867-
'CloudSpanner.Snapshot.begin',
868-
'CloudSpanner.Snapshot.runStream',
869-
'CloudSpanner.Snapshot.run',
870-
'CloudSpanner.Dml.runUpdate',
871828
'CloudSpanner.Database.getTransaction',
872829
'CloudSpanner.Snapshot.runStream',
873830
];
@@ -878,8 +835,6 @@ describe('ObservabilityOptions injection and propagation', async () => {
878835
);
879836

880837
const expectedEventNames = [
881-
'Begin Transaction',
882-
'Transaction Creation Done',
883838
'Acquiring session',
884839
'Cache hit: has usable session',
885840
'Acquired session',

src/instrument.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,12 +109,12 @@ const {
109109
function ensureInitialContextManagerSet() {
110110
if (context.active() === ROOT_CONTEXT) {
111111
// If no active context was set previously, trace context propagation cannot
112-
// function correctly with async/await for OpenTelemetry and they acknowledge
113-
// this fact per https://opentelemetry.io/docs/languages/js/context/#active-context
112+
// function correctly with async/await for OpenTelemetry
113+
// See {@link https://opentelemetry.io/docs/languages/js/context/#active-context}
114114
// but we shouldn't make our customers have to invasively edit their code
115115
// nor should they be burdened about these facts, their code should JUST work.
116116
// Please see https://github.com/googleapis/nodejs-spanner/issues/2146
117-
context.disable(); // Firstly disable any prior contextManager.
117+
context.disable(); // Disable any prior contextManager.
118118
const contextManager = new AsyncHooksContextManager();
119119
contextManager.enable();
120120
context.setGlobalContextManager(contextManager);

0 commit comments

Comments
 (0)