GeezerVison is showing github.com — chore: refactor observability tests (#2177) · googleapis/google-cloud-node@3158191
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/observability-test/batch-transaction.ts +83-91Lines changed: 83 additions & 91 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -153,103 +153,95 @@ describe('BatchTransaction', () => { | 153 | 153 | }; | 154 | 154 |
| 155 | 155 | it('createQueryPartitions', done => { | 156 | | - const REQUEST = sandbox.stub(); | 157 | | - | 158 | | - const res = batchTransaction.createQueryPartitions( | 159 | | - QUERY, | 160 | | - (err, part, resp) => { | 161 | | - assert.ifError(err); | 162 | | - traceExporter.forceFlush(); | 163 | | - const spans = traceExporter.getFinishedSpans(); | 164 | | - assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); | 165 | | - | 166 | | - // Sort the spans by duration. | 167 | | - spans.sort((spanA, spanB) => { | 168 | | - spanA.duration < spanB.duration; | 169 | | - }); | 170 | | - | 171 | | - const actualSpanNames: string[] = []; | 172 | | - spans.forEach(span => { | 173 | | - actualSpanNames.push(span.name); | 174 | | - }); | 175 | | - | 176 | | - const expectedSpanNames = [ | 177 | | - 'CloudSpanner.BatchTransaction.createPartitions_', | 178 | | - 'CloudSpanner.BatchTransaction.createQueryPartitions', | 179 | | - ]; | 180 | | - assert.deepStrictEqual( | 181 | | - actualSpanNames, | 182 | | - expectedSpanNames, | 183 | | - `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` | 184 | | - ); | 185 | | - | 186 | | - // Ensure that createPartitions_ is a child span of createQueryPartitions. | 187 | | - const spanCreatePartitions_ = spans[0]; | 188 | | - const spanCreateQueryPartitions = spans[1]; | 189 | | - assert.ok( | 190 | | - spanCreateQueryPartitions.spanContext().traceId, | 191 | | - 'Expected that createQueryPartitions has a defined traceId' | 192 | | - ); | 193 | | - assert.ok( | 194 | | - spanCreatePartitions_.spanContext().traceId, | 195 | | - 'Expected that createPartitions_ has a defined traceId' | 196 | | - ); | 197 | | - assert.deepStrictEqual( | 198 | | - spanCreatePartitions_.spanContext().traceId, | 199 | | - spanCreateQueryPartitions.spanContext().traceId, | 200 | | - 'Expected that both spans share a traceId' | 201 | | - ); | 202 | | - assert.ok( | 203 | | - spanCreateQueryPartitions.spanContext().spanId, | 204 | | - 'Expected that createQueryPartitions has a defined spanId' | 205 | | - ); | 206 | | - assert.ok( | 207 | | - spanCreatePartitions_.spanContext().spanId, | 208 | | - 'Expected that createPartitions_ has a defined spanId' | 209 | | - ); | 210 | | - assert.deepStrictEqual( | 211 | | - spanCreatePartitions_.parentSpanId, | 212 | | - spanCreateQueryPartitions.spanContext().spanId, | 213 | | - 'Expected that createQueryPartitions is the parent to createPartitions_' | 214 | | - ); | 215 | | - done(); | 216 | | - } | 217 | | - ); | | 156 | + batchTransaction.createQueryPartitions(QUERY, err => { | | 157 | + assert.ifError(err); | | 158 | + traceExporter.forceFlush(); | | 159 | + const spans = traceExporter.getFinishedSpans(); | | 160 | + assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); | | 161 | + | | 162 | + // Sort the spans by duration. | | 163 | + spans.sort((spanA, spanB) => { | | 164 | + spanA.duration < spanB.duration; | | 165 | + }); | | 166 | + | | 167 | + const actualSpanNames: string[] = []; | | 168 | + spans.forEach(span => { | | 169 | + actualSpanNames.push(span.name); | | 170 | + }); | | 171 | + | | 172 | + const expectedSpanNames = [ | | 173 | + 'CloudSpanner.BatchTransaction.createPartitions_', | | 174 | + 'CloudSpanner.BatchTransaction.createQueryPartitions', | | 175 | + ]; | | 176 | + assert.deepStrictEqual( | | 177 | + actualSpanNames, | | 178 | + expectedSpanNames, | | 179 | + `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` | | 180 | + ); | | 181 | + | | 182 | + // Ensure that createPartitions_ is a child span of createQueryPartitions. | | 183 | + const spanCreatePartitions_ = spans[0]; | | 184 | + const spanCreateQueryPartitions = spans[1]; | | 185 | + assert.ok( | | 186 | + spanCreateQueryPartitions.spanContext().traceId, | | 187 | + 'Expected that createQueryPartitions has a defined traceId' | | 188 | + ); | | 189 | + assert.ok( | | 190 | + spanCreatePartitions_.spanContext().traceId, | | 191 | + 'Expected that createPartitions_ has a defined traceId' | | 192 | + ); | | 193 | + assert.deepStrictEqual( | | 194 | + spanCreatePartitions_.spanContext().traceId, | | 195 | + spanCreateQueryPartitions.spanContext().traceId, | | 196 | + 'Expected that both spans share a traceId' | | 197 | + ); | | 198 | + assert.ok( | | 199 | + spanCreateQueryPartitions.spanContext().spanId, | | 200 | + 'Expected that createQueryPartitions has a defined spanId' | | 201 | + ); | | 202 | + assert.ok( | | 203 | + spanCreatePartitions_.spanContext().spanId, | | 204 | + 'Expected that createPartitions_ has a defined spanId' | | 205 | + ); | | 206 | + assert.deepStrictEqual( | | 207 | + spanCreatePartitions_.parentSpanId, | | 208 | + spanCreateQueryPartitions.spanContext().spanId, | | 209 | + 'Expected that createQueryPartitions is the parent to createPartitions_' | | 210 | + ); | | 211 | + done(); | | 212 | + }); | 218 | 213 | }); | 219 | 214 |
| 220 | 215 | it('createReadPartitions', done => { | 221 | 216 | const REQUEST = sandbox.stub(); | 222 | 217 | const response = {}; | 223 | 218 | REQUEST.callsFake((_, callback) => callback(null, response)); | 224 | 219 |
| 225 | | - const res = batchTransaction.createReadPartitions( | 226 | | - QUERY, | 227 | | - (err, part, resp) => { | 228 | | - assert.ifError(err); | 229 | | - traceExporter.forceFlush(); | 230 | | - const spans = traceExporter.getFinishedSpans(); | 231 | | - assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); | 232 | | - | 233 | | - // Sort the spans by duration. | 234 | | - spans.sort((spanA, spanB) => { | 235 | | - spanA.duration < spanB.duration; | 236 | | - }); | 237 | | - | 238 | | - const actualSpanNames: string[] = []; | 239 | | - spans.forEach(span => { | 240 | | - actualSpanNames.push(span.name); | 241 | | - }); | 242 | | - const expectedSpanNames = [ | 243 | | - 'CloudSpanner.BatchTransaction.createPartitions_', | 244 | | - 'CloudSpanner.BatchTransaction.createReadPartitions', | 245 | | - ]; | 246 | | - assert.deepStrictEqual( | 247 | | - actualSpanNames, | 248 | | - expectedSpanNames, | 249 | | - `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` | 250 | | - ); | 251 | | - done(); | 252 | | - } | 253 | | - ); | | 220 | + batchTransaction.createReadPartitions(QUERY, err => { | | 221 | + assert.ifError(err); | | 222 | + traceExporter.forceFlush(); | | 223 | + const spans = traceExporter.getFinishedSpans(); | | 224 | + assert.strictEqual(spans.length, 2, 'Exactly 2 spans expected'); | | 225 | + | | 226 | + // Sort the spans by duration. | | 227 | + spans.sort((spanA, spanB) => { | | 228 | + spanA.duration < spanB.duration; | | 229 | + }); | | 230 | + | | 231 | + const actualSpanNames: string[] = []; | | 232 | + spans.forEach(span => { | | 233 | + actualSpanNames.push(span.name); | | 234 | + }); | | 235 | + const expectedSpanNames = [ | | 236 | + 'CloudSpanner.BatchTransaction.createPartitions_', | | 237 | + 'CloudSpanner.BatchTransaction.createReadPartitions', | | 238 | + ]; | | 239 | + assert.deepStrictEqual( | | 240 | + actualSpanNames, | | 241 | + expectedSpanNames, | | 242 | + `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}` | | 243 | + ); | | 244 | + done(); | | 245 | + }); | 254 | 246 | }); | 255 | 247 | }); |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/observability-test/database.ts +8-23Lines changed: 8 additions & 23 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -507,7 +507,6 @@ describe('Database', () => { | 507 | 507 |
| 508 | 508 | let beginSnapshotStub: sinon.SinonStub; | 509 | 509 | let getSessionStub: sinon.SinonStub; | 510 | | - let snapshotStub: sinon.SinonStub; | 511 | 510 |
| 512 | 511 | beforeEach(() => { | 513 | 512 | fakePool = database.pool_; | @@ -524,9 +523,7 @@ describe('Database', () => { | 524 | 523 | sandbox.stub(fakePool, 'getSession') as sinon.SinonStub | 525 | 524 | ).callsFake(callback => callback(null, fakeSession)); | 526 | 525 |
| 527 | | - snapshotStub = sandbox | 528 | | - .stub(fakeSession, 'snapshot') | 529 | | - .returns(fakeSnapshot); | | 526 | + sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot); | 530 | 527 | }); | 531 | 528 |
| 532 | 529 | it('with error', done => { | @@ -1175,7 +1172,7 @@ describe('Database', () => { | 1175 | 1172 |
| 1176 | 1173 | it('with error on null mutation should catch thrown error', done => { | 1177 | 1174 | try { | 1178 | | - database.writeAtLeastOnce(null, (err, res) => {}); | | 1175 | + database.writeAtLeastOnce(null, () => {}); | 1179 | 1176 | } catch (err) { | 1180 | 1177 | // Performing a substring search on the error because | 1181 | 1178 | // depending on the version of Node.js, the error might be either of: | @@ -1250,7 +1247,6 @@ describe('Database', () => { | 1250 | 1247 | let fakeSession: FakeSession; | 1251 | 1248 | let fakeDataStream: Transform; | 1252 | 1249 | let getSessionStub: sinon.SinonStub; | 1253 | | - let requestStreamStub: sinon.SinonStub; | 1254 | 1250 |
| 1255 | 1251 | const options = { | 1256 | 1252 | requestOptions: { | @@ -1269,9 +1265,7 @@ describe('Database', () => { | 1269 | 1265 | sandbox.stub(fakePool, 'getSession') as sinon.SinonStub | 1270 | 1266 | ).callsFake(callback => callback(null, fakeSession)); | 1271 | 1267 |
| 1272 | | - requestStreamStub = sandbox | 1273 | | - .stub(database, 'requestStream') | 1274 | | - .returns(fakeDataStream); | | 1268 | + sandbox.stub(database, 'requestStream').returns(fakeDataStream); | 1275 | 1269 | }); | 1276 | 1270 |
| 1277 | 1271 | it('on retry with "Session not found" error', done => { | @@ -1320,7 +1314,6 @@ describe('Database', () => { | 1320 | 1314 | 'Expected an ERROR span status' | 1321 | 1315 | ); | 1322 | 1316 |
| 1323 | | - const errorMessage = firstSpan.status.message; | 1324 | 1317 | assert.deepStrictEqual( | 1325 | 1318 | firstSpan.status.message, | 1326 | 1319 | sessionNotFoundError.message | @@ -1658,7 +1651,7 @@ describe('Database', () => { | 1658 | 1651 | .throws(ourException); | 1659 | 1652 |
| 1660 | 1653 | assert.rejects(async () => { | 1661 | | - const value = await database.runTransactionAsync(async txn => { | | 1654 | + await database.runTransactionAsync(async txn => { | 1662 | 1655 | const result = await txn.run('SELECT 1'); | 1663 | 1656 | await txn.commit(); | 1664 | 1657 | return result; | @@ -1724,8 +1717,6 @@ describe('Database', () => { | 1724 | 1717 | let fakeStream2: Transform; | 1725 | 1718 |
| 1726 | 1719 | let getSessionStub: sinon.SinonStub; | 1727 | | - let snapshotStub: sinon.SinonStub; | 1728 | | - let runStreamStub: sinon.SinonStub; | 1729 | 1720 |
| 1730 | 1721 | beforeEach(() => { | 1731 | 1722 | fakePool = database.pool_; | @@ -1746,15 +1737,11 @@ describe('Database', () => { | 1746 | 1737 | .onSecondCall() | 1747 | 1738 | .callsFake(callback => callback(null, fakeSession2)); | 1748 | 1739 |
| 1749 | | - snapshotStub = sandbox | 1750 | | - .stub(fakeSession, 'snapshot') | 1751 | | - .returns(fakeSnapshot); | | 1740 | + sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot); | 1752 | 1741 |
| 1753 | 1742 | sandbox.stub(fakeSession2, 'snapshot').returns(fakeSnapshot2); | 1754 | 1743 |
| 1755 | | - runStreamStub = sandbox | 1756 | | - .stub(fakeSnapshot, 'runStream') | 1757 | | - .returns(fakeStream); | | 1744 | + sandbox.stub(fakeSnapshot, 'runStream').returns(fakeStream); | 1758 | 1745 |
| 1759 | 1746 | sandbox.stub(fakeSnapshot2, 'runStream').returns(fakeStream2); | 1760 | 1747 | }); | @@ -1975,7 +1962,6 @@ describe('Database', () => { | 1975 | 1962 |
| 1976 | 1963 | let getSessionStub; | 1977 | 1964 | let beginStub; | 1978 | | - let runUpdateStub; | 1979 | 1965 |
| 1980 | 1966 | beforeEach(() => { | 1981 | 1967 | fakePool = database.pool_; | @@ -1996,7 +1982,7 @@ describe('Database', () => { | 1996 | 1982 | sandbox.stub(fakePartitionedDml, 'begin') as sinon.SinonStub | 1997 | 1983 | ).callsFake(callback => callback(null)); | 1998 | 1984 |
| 1999 | | - runUpdateStub = ( | | 1985 | + ( | 2000 | 1986 | sandbox.stub(fakePartitionedDml, 'runUpdate') as sinon.SinonStub | 2001 | 1987 | ).callsFake((_, callback) => callback(null)); | 2002 | 1988 | }); | @@ -2031,7 +2017,6 @@ describe('Database', () => { | 2031 | 2017 |
| 2032 | 2018 | it('with pool errors', done => { | 2033 | 2019 | const fakeError = new Error('err'); | 2034 | | - const fakeCallback = sandbox.spy(); | 2035 | 2020 |
| 2036 | 2021 | getSessionStub.callsFake(callback => callback(fakeError)); | 2037 | 2022 | database.runPartitionedUpdate(QUERY, async (err, rowCount) => { | @@ -2132,7 +2117,7 @@ describe('Database', () => { | 2132 | 2117 | sandbox.stub(fakePool, 'release') as sinon.SinonStub | 2133 | 2118 | ).withArgs(fakeSession); | 2134 | 2119 |
| 2135 | | - database.runPartitionedUpdate(QUERY, async (err, rowCount) => { | | 2120 | + database.runPartitionedUpdate(QUERY, async () => { | 2136 | 2121 | const exportResults = await getTraceExportResults(); | 2137 | 2122 | const actualSpanNames = exportResults.spanNames; | 2138 | 2123 | const spans = exportResults.spans; | |
Collapse file Copy file name to clipboardExpand all lines: handwritten/spanner/observability-test/helper.ts +46Lines changed: 46 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -19,6 +19,25 @@ import * as assert from 'assert'; | 19 | 19 | const {ReadableSpan} = require('@opentelemetry/sdk-trace-base'); | 20 | 20 | import {SEMATTRS_DB_NAME} from '@opentelemetry/semantic-conventions'; | 21 | 21 |
| | 22 | +export const batchCreateSessionsEvents = [ | | 23 | + 'Requesting 25 sessions', | | 24 | + 'Creating 25 sessions', | | 25 | + 'Requested for 25 sessions returned 25', | | 26 | +]; | | 27 | + | | 28 | +export const waitingSessionsEvents = [ | | 29 | + 'Acquiring session', | | 30 | + 'Waiting for a session to become available', | | 31 | + 'Acquired session', | | 32 | + 'Using Session', | | 33 | +]; | | 34 | + | | 35 | +export const cacheSessionEvents = [ | | 36 | + 'Acquiring session', | | 37 | + 'Cache hit: has usable session', | | 38 | + 'Acquired session', | | 39 | +]; | | 40 | + | 22 | 41 | /** | 23 | 42 | * This utility exists as a test helper because mocha has builtin "context" | 24 | 43 | * and referring to context causes type/value collision errors. | @@ -47,3 +66,30 @@ export function generateWithAllSpansHaveDBName(dbName: String): Function { | 47 | 66 | }); | 48 | 67 | }; | 49 | 68 | } | | 69 | + | | 70 | +export async function verifySpansAndEvents( | | 71 | + traceExporter, | | 72 | + expectedSpans, | | 73 | + expectedEvents | | 74 | +) { | | 75 | + await traceExporter.forceFlush(); | | 76 | + const spans = traceExporter.getFinishedSpans(); | | 77 | + const actualEventNames: string[] = []; | | 78 | + const actualSpanNames: string[] = []; | | 79 | + spans.forEach(span => { | | 80 | + actualSpanNames.push(span.name); | | 81 | + span.events.forEach(event => { | | 82 | + actualEventNames.push(event.name); | | 83 | + }); | | 84 | + }); | | 85 | + assert.deepStrictEqual( | | 86 | + actualSpanNames, | | 87 | + expectedSpans, | | 88 | + `span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpans}` | | 89 | + ); | | 90 | + assert.deepStrictEqual( | | 91 | + actualEventNames, | | 92 | + expectedEvents, | | 93 | + `Unexpected events:\n\tGot: ${actualEventNames}\n\tWant: ${expectedEvents}` | | 94 | + ); | | 95 | +} |
|