From f4f456522557a48401aaf722dfb9819400320445 Mon Sep 17 00:00:00 2001 From: Emmanuel T Odeke Date: Sat, 28 Sep 2024 21:13:20 -0700 Subject: [PATCH] Check for events even in EndToEnd tests --- observability-test/database.ts | 202 ++++++++++++++++++++++++++++++--- observability-test/spanner.ts | 75 ++++++++++++ test/spanner.ts | 13 +-- 3 files changed, 261 insertions(+), 29 deletions(-) diff --git a/observability-test/database.ts b/observability-test/database.ts index 35df82052..8b128c983 100644 --- a/observability-test/database.ts +++ b/observability-test/database.ts @@ -286,8 +286,12 @@ describe('Database', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getSessions']; @@ -305,6 +309,14 @@ describe('Database', () => { 'Expected an OK span status' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -323,8 +335,12 @@ describe('Database', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getSessions']; @@ -347,6 +363,14 @@ describe('Database', () => { 'Mismatched span status message' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -392,8 +416,12 @@ describe('Database', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getSnapshot']; @@ -415,6 +443,15 @@ describe('Database', () => { firstSpan.status.message, 'Mismatched span status message' ); + + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -460,8 +497,12 @@ describe('Database', () => { assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -516,6 +557,13 @@ describe('Database', () => { 'Expected that secondRetrySpan is the child to parentSpan' ); + const expectedEventNames = ['No session available']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -549,9 +597,13 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -575,6 +627,15 @@ describe('Database', () => { firstSpan.status.message, 'Mismatched span status message' ); + + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -602,9 +663,13 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -628,6 +693,14 @@ describe('Database', () => { firstSpan.status.message, `No span status message expected\n\tGot: undefined\n\tWant: ${firstSpan.status.message}` ); + + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -655,8 +728,12 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -680,6 +757,15 @@ describe('Database', () => { firstSpan.status.message, 'Mismatched span status message' ); + + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -842,9 +928,14 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.writeAtLeastOnce']; @@ -867,6 +958,14 @@ describe('Database', () => { 'Mismatched span status message' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -883,9 +982,14 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.writeAtLeastOnce']; @@ -908,6 +1012,13 @@ describe('Database', () => { 'Unexpected span status message' ); + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); } catch (error) { @@ -930,8 +1041,12 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.writeAtLeastOnce']; @@ -954,25 +1069,16 @@ describe('Database', () => { 'Mismatched span status message' ); - done(); - } - }); + // We expect an exception to have been caught as well as a Session event. + const expectedEventNames = ['Using Session', 'exception']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); - it('should return CommitResponse on successful write using Callback', done => { - database.writeAtLeastOnce(mutations, (err, res) => { - assert.deepStrictEqual(err, null); - assert.deepStrictEqual(res, RESPONSE); done(); - }); - }); - - it('should return CommitResponse on successful write using await', async () => { - sinon.stub(database, 'writeAtLeastOnce').resolves([RESPONSE]); - const [response, err] = await database.writeAtLeastOnce(mutations, {}); - assert.deepStrictEqual( - response.commitTimestamp, - RESPONSE.commitTimestamp - ); + } }); }); @@ -1007,8 +1113,12 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runTransaction']; @@ -1031,6 +1141,14 @@ describe('Database', () => { 'Mismatched span status message' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -1046,8 +1164,12 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runTransaction']; @@ -1070,6 +1192,14 @@ describe('Database', () => { 'Mismatched span status message' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -1136,9 +1266,13 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runStream']; @@ -1161,6 +1295,14 @@ describe('Database', () => { 'Mismatched span status message' ); + // We don't expect events. + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -1175,9 +1317,13 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runStream']; @@ -1200,6 +1346,13 @@ describe('Database', () => { 'Mismatched span status message' ); + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); @@ -1229,8 +1382,12 @@ describe('Database', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 2, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -1269,6 +1426,17 @@ describe('Database', () => { 'Unexpected span status message' ); + const expectedEventNames = [ + 'Using Session', + 'Using Session', + 'No session available', + ]; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); diff --git a/observability-test/spanner.ts b/observability-test/spanner.ts index 20120303c..933e9bf08 100644 --- a/observability-test/spanner.ts +++ b/observability-test/spanner.ts @@ -170,8 +170,12 @@ describe('EndToEnd', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getSessions']; @@ -180,6 +184,13 @@ describe('EndToEnd', () => { expectedSpanNames, `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); }); it('getSnapshot', done => { @@ -194,8 +205,12 @@ describe('EndToEnd', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getSnapshot']; @@ -205,6 +220,13 @@ describe('EndToEnd', () => { `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -219,9 +241,13 @@ describe('EndToEnd', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.getTransaction']; @@ -231,6 +257,13 @@ describe('EndToEnd', () => { `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -246,8 +279,12 @@ describe('EndToEnd', () => { assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); const actualSpanNames: string[] = []; + const actualEventNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runStream']; @@ -257,6 +294,13 @@ describe('EndToEnd', () => { `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -272,9 +316,14 @@ describe('EndToEnd', () => { spans.sort((spanA, spanB) => { spanA.duration < spanB.duration; }); + + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = [ @@ -306,6 +355,13 @@ describe('EndToEnd', () => { spanRunStream.spanContext().spanId, 'Expected that RunStream has a defined spanId' ); + + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); }); it('runTransaction', done => { @@ -318,9 +374,13 @@ describe('EndToEnd', () => { const spans = traceExporter.getFinishedSpans(); assert.strictEqual(spans.length, 1, 'Exactly 1 span expected'); + const actualEventNames: string[] = []; const actualSpanNames: string[] = []; spans.forEach(span => { actualSpanNames.push(span.name); + span.events.forEach(event => { + actualEventNames.push(event.name); + }); }); const expectedSpanNames = ['CloudSpanner.Database.runTransaction']; @@ -329,6 +389,14 @@ describe('EndToEnd', () => { expectedSpanNames, `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + + const expectedEventNames = []; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); @@ -360,6 +428,13 @@ describe('EndToEnd', () => { `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` ); + const expectedEventNames = ['Using Session']; + assert.deepStrictEqual( + actualEventNames, + expectedEventNames, + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEventNames}` + ); + done(); }); }); diff --git a/test/spanner.ts b/test/spanner.ts index efa778248..cdb41b689 100644 --- a/test/spanner.ts +++ b/test/spanner.ts @@ -5045,18 +5045,7 @@ describe('Spanner with mock server', () => { gotEventNames.push(event.name); }); - const wantEventNames = [ - 'Requesting 25 sessions', - 'Creating 25 sessions', - // Since we added new spans in the Database methods - // these commented annotations now belong to separate spans. - // 'Acquiring session', - // 'Waiting for a session to become available', - // 'Requested for 25 sessions returned 25 sessions', - // 'Acquired session', - // 'Creating Transaction', - // 'Transaction Creation Done', - ]; + const wantEventNames = ['Requesting 25 sessions', 'Creating 25 sessions']; assert.deepEqual( gotEventNames,