Skip to content

Commit 9949a4d

Browse files
committed
Adjust tests to ensure transaction.end() where necessary
1 parent e5bd2b3 commit 9949a4d

File tree

4 files changed

+48
-23
lines changed

4 files changed

+48
-23
lines changed

observability-test/database.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -774,6 +774,7 @@ describe('Database', () => {
774774
callback(null, RESPONSE);
775775
},
776776
once() {},
777+
end() {},
777778
};
778779

779780
database.batchTransaction = (identifier, options) => {
@@ -782,10 +783,14 @@ describe('Database', () => {
782783
return fakeTransaction;
783784
};
784785

785-
database.createBatchTransaction(opts, (err, transaction, resp) => {
786+
database.createBatchTransaction(opts, async (err, transaction, resp) => {
786787
assert.strictEqual(err, null);
787788
assert.strictEqual(transaction, fakeTransaction);
788789
assert.strictEqual(resp, RESPONSE);
790+
transaction!.end();
791+
792+
await provider.forceFlush();
793+
traceExporter.forceFlush();
789794
const spans = traceExporter.getFinishedSpans();
790795
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
791796
withAllSpansHaveDBName(spans);
@@ -839,8 +844,8 @@ describe('Database', () => {
839844
begin(callback) {
840845
callback(error, RESPONSE);
841846
},
842-
843847
once() {},
848+
end() {},
844849
};
845850

846851
database.batchTransaction = () => {
@@ -926,9 +931,11 @@ describe('Database', () => {
926931

927932
getSessionStub.callsFake(callback => callback(fakeError));
928933

929-
database.getTransaction(err => {
934+
database.getTransaction(async err => {
930935
assert.strictEqual(err, fakeError);
931936

937+
await provider.forceFlush();
938+
traceExporter.forceFlush();
932939
const spans = traceExporter.getFinishedSpans();
933940
assert.strictEqual(spans.length, 1, 'Exactly 1 span expected');
934941
withAllSpansHaveDBName(spans);
@@ -975,9 +982,10 @@ describe('Database', () => {
975982
});
976983

977984
it('with no errors', done => {
978-
database.getTransaction((err, transaction) => {
985+
database.getTransaction(async (err, transaction) => {
979986
assert.ifError(err);
980987
assert.strictEqual(transaction, fakeTransaction);
988+
transaction!.end();
981989

982990
const spans = traceExporter.getFinishedSpans();
983991
withAllSpansHaveDBName(spans);

observability-test/spanner.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,13 +273,17 @@ describe('EndToEnd', () => {
273273
const withAllSpansHaveDBName = generateWithAllSpansHaveDBName(
274274
database.formattedName_
275275
);
276-
database.getTransaction((err, transaction) => {
276+
database.getTransaction(async (err, transaction) => {
277277
assert.ifError(err);
278278
assert.ok(transaction);
279+
transaction!.end();
280+
transaction!.commit();
279281

282+
await tracerProvider.forceFlush();
280283
traceExporter.forceFlush();
281284
const spans = traceExporter.getFinishedSpans();
282285
withAllSpansHaveDBName(spans);
286+
console.log(`flushed spans: ${spans.toString()}`);
283287

284288
const actualEventNames: string[] = [];
285289
const actualSpanNames: string[] = [];
@@ -807,7 +811,10 @@ describe('ObservabilityOptions injection and propagation', async () => {
807811
database.getTransaction((err, tx) => {
808812
assert.ifError(err);
809813

810-
tx!.run('SELECT 1', (err, rows) => {
814+
tx!.run('SELECT 1', async (err, rows) => {
815+
tx!.end();
816+
817+
await tracerProvider.forceFlush();
811818
traceExporter.forceFlush();
812819

813820
const spans = traceExporter.getFinishedSpans();
@@ -860,9 +867,11 @@ describe('ObservabilityOptions injection and propagation', async () => {
860867
traceExporter.reset();
861868

862869
tx!.begin();
863-
tx!.runUpdate(updateSql, (err, rowCount) => {
870+
tx!.runUpdate(updateSql, async (err, rowCount) => {
864871
assert.ifError(err);
872+
tx!.end();
865873

874+
await tracerProvider.forceFlush();
866875
traceExporter.forceFlush();
867876

868877
const spans = traceExporter.getFinishedSpans();
@@ -914,9 +923,10 @@ describe('ObservabilityOptions injection and propagation', async () => {
914923
.on('data', () => rowCount++)
915924
.on('error', assert.ifError)
916925
.on('stats', _stats => {})
917-
.on('end', () => {
926+
.on('end', async () => {
918927
tx!.end();
919928

929+
await tracerProvider.forceFlush();
920930
traceExporter.forceFlush();
921931

922932
const spans = traceExporter.getFinishedSpans();
@@ -970,7 +980,9 @@ describe('ObservabilityOptions injection and propagation', async () => {
970980

971981
tx!.runUpdate(updateSql, async (err, rowCount) => {
972982
assert.ifError(err);
973-
tx!.rollback(err => {
983+
tx!.rollback(async err => {
984+
tx!.end();
985+
await tracerProvider.forceFlush();
974986
traceExporter.forceFlush();
975987

976988
const spans = traceExporter.getFinishedSpans();
@@ -1557,9 +1569,9 @@ SELECT 1p
15571569
const expectedSpanNames = [
15581570
'CloudSpanner.Database.batchCreateSessions',
15591571
'CloudSpanner.SessionPool.createSessions',
1572+
'CloudSpanner.Snapshot.runStream',
15601573
'CloudSpanner.Snapshot.run',
15611574
'CloudSpanner.Snapshot.begin',
1562-
'CloudSpanner.Snapshot.runStream',
15631575
'CloudSpanner.Snapshot.begin',
15641576
'CloudSpanner.Transaction.commit',
15651577
'CloudSpanner.Transaction.commit',
@@ -1570,7 +1582,7 @@ SELECT 1p
15701582
expectedSpanNames,
15711583
`span names mismatch:\n\tGot: ${actualSpanNames}\n\tWant: ${expectedSpanNames}`
15721584
);
1573-
const spanSnapshotRun = spans[2];
1585+
const spanSnapshotRun = spans[3];
15741586
assert.strictEqual(spanSnapshotRun.name, 'CloudSpanner.Snapshot.run');
15751587
const wantSpanErr = '6 ALREADY_EXISTS: ' + messageBadInsertAlreadyExistent;
15761588
assert.deepStrictEqual(
@@ -1653,12 +1665,12 @@ SELECT 1p
16531665
'Requesting 25 sessions',
16541666
'Creating 25 sessions',
16551667
'Requested for 25 sessions returned 25',
1656-
'Begin Transaction',
1657-
'Transaction Creation Done',
16581668
'Starting stream',
16591669
'Stream broken. Safe to retry',
16601670
'Begin Transaction',
16611671
'Transaction Creation Done',
1672+
'Begin Transaction',
1673+
'Transaction Creation Done',
16621674
'Starting Commit',
16631675
'Commit Done',
16641676
'Acquiring session',

src/database.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -856,6 +856,7 @@ class Database extends common.GrpcServiceObject {
856856
callback!(err as ServiceError, null, undefined);
857857
return;
858858
}
859+
859860
const transaction = this.batchTransaction(
860861
{session: session!},
861862
options
@@ -874,6 +875,7 @@ class Database extends common.GrpcServiceObject {
874875
return;
875876
}
876877
span.addEvent('Using Session', {'session.id': session?.id});
878+
span.end();
877879
callback!(null, transaction, resp!);
878880
});
879881
});
@@ -1130,7 +1132,9 @@ class Database extends common.GrpcServiceObject {
11301132
setSpanErrorAndException(span, e as Error);
11311133
this.emit('error', e);
11321134
} finally {
1133-
span.end();
1135+
if (span.isRecording()) {
1136+
span.end();
1137+
}
11341138
}
11351139
});
11361140
}
@@ -2213,11 +2217,10 @@ class Database extends common.GrpcServiceObject {
22132217
'session.id': session?.id,
22142218
});
22152219
setSpanError(span, err);
2216-
span.end();
22172220
} else {
22182221
setSpanError(span, err);
2219-
span.end();
22202222
}
2223+
span.end();
22212224
cb!(err as grpc.ServiceError | null, transaction);
22222225
});
22232226
});

src/transaction.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,12 +1370,14 @@ export class Snapshot extends EventEmitter {
13701370
span.end();
13711371
});
13721372

1373-
finished(resultStream, err => {
1374-
if (err) {
1375-
setSpanError(span, err);
1376-
}
1377-
span.end();
1378-
});
1373+
if (resultStream instanceof Stream) {
1374+
finished(resultStream, err => {
1375+
if (err) {
1376+
setSpanError(span, err);
1377+
}
1378+
span.end();
1379+
});
1380+
}
13791381
return resultStream;
13801382
});
13811383
}
@@ -2133,7 +2135,7 @@ export class Transaction extends Dml {
21332135
opts: this._observabilityOptions,
21342136
dbName: this._dbName!,
21352137
};
2136-
startTrace('Transaction.commit', traceConfig, async span => {
2138+
return startTrace('Transaction.commit', traceConfig, span => {
21372139
if (this.id) {
21382140
reqOpts.transactionId = this.id as Uint8Array;
21392141
} else if (!this._useInRunner) {

0 commit comments

Comments
 (0)