Skip to content

Commit

Permalink
feat: (observability): trace Database.runPartitionedUpdate (googleapi…
Browse files Browse the repository at this point in the history
…s#2176)

This change traces Database.runPartitionedUpdate along with the appropriate tests for it with and without errors.

Updates googleapis#2079
  • Loading branch information
odeke-em authored and surbhigarg92 committed Oct 29, 2024
1 parent 4e85d45 commit a588465
Show file tree
Hide file tree
Showing 9 changed files with 104 additions and 247 deletions.
4 changes: 2 additions & 2 deletions observability-test/batch-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ describe('BatchTransaction', () => {
};

it('createQueryPartitions', done => {
const res = batchTransaction.createQueryPartitions(QUERY, err => {
batchTransaction.createQueryPartitions(QUERY, err => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
Expand Down Expand Up @@ -217,7 +217,7 @@ describe('BatchTransaction', () => {
const response = {};
REQUEST.callsFake((_, callback) => callback(null, response));

const res = batchTransaction.createReadPartitions(QUERY, err => {
batchTransaction.createReadPartitions(QUERY, err => {
assert.ifError(err);
traceExporter.forceFlush();
const spans = traceExporter.getFinishedSpans();
Expand Down
26 changes: 6 additions & 20 deletions observability-test/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,6 @@ describe('Database', () => {

let beginSnapshotStub: sinon.SinonStub;
let getSessionStub: sinon.SinonStub;
let snapshotStub: sinon.SinonStub;

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -524,9 +523,7 @@ describe('Database', () => {
sandbox.stub(fakePool, 'getSession') as sinon.SinonStub
).callsFake(callback => callback(null, fakeSession));

snapshotStub = sandbox
.stub(fakeSession, 'snapshot')
.returns(fakeSnapshot);
sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot);
});

it('with error', done => {
Expand Down Expand Up @@ -1250,7 +1247,6 @@ describe('Database', () => {
let fakeSession: FakeSession;
let fakeDataStream: Transform;
let getSessionStub: sinon.SinonStub;
let requestStreamStub: sinon.SinonStub;

const options = {
requestOptions: {
Expand All @@ -1269,9 +1265,7 @@ describe('Database', () => {
sandbox.stub(fakePool, 'getSession') as sinon.SinonStub
).callsFake(callback => callback(null, fakeSession));

requestStreamStub = sandbox
.stub(database, 'requestStream')
.returns(fakeDataStream);
sandbox.stub(database, 'requestStream').returns(fakeDataStream);
});

it('on retry with "Session not found" error', done => {
Expand Down Expand Up @@ -1723,8 +1717,6 @@ describe('Database', () => {
let fakeStream2: Transform;

let getSessionStub: sinon.SinonStub;
let snapshotStub: sinon.SinonStub;
let runStreamStub: sinon.SinonStub;

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -1745,15 +1737,11 @@ describe('Database', () => {
.onSecondCall()
.callsFake(callback => callback(null, fakeSession2));

snapshotStub = sandbox
.stub(fakeSession, 'snapshot')
.returns(fakeSnapshot);
sandbox.stub(fakeSession, 'snapshot').returns(fakeSnapshot);

sandbox.stub(fakeSession2, 'snapshot').returns(fakeSnapshot2);

runStreamStub = sandbox
.stub(fakeSnapshot, 'runStream')
.returns(fakeStream);
sandbox.stub(fakeSnapshot, 'runStream').returns(fakeStream);

sandbox.stub(fakeSnapshot2, 'runStream').returns(fakeStream2);
});
Expand Down Expand Up @@ -1974,7 +1962,6 @@ describe('Database', () => {

let getSessionStub;
let beginStub;
let runUpdateStub;

beforeEach(() => {
fakePool = database.pool_;
Expand All @@ -1995,7 +1982,7 @@ describe('Database', () => {
sandbox.stub(fakePartitionedDml, 'begin') as sinon.SinonStub
).callsFake(callback => callback(null));

runUpdateStub = (
(
sandbox.stub(fakePartitionedDml, 'runUpdate') as sinon.SinonStub
).callsFake((_, callback) => callback(null));
});
Expand Down Expand Up @@ -2030,7 +2017,6 @@ describe('Database', () => {

it('with pool errors', done => {
const fakeError = new Error('err');
const fakeCallback = sandbox.spy();

getSessionStub.callsFake(callback => callback(fakeError));
database.runPartitionedUpdate(QUERY, async (err, rowCount) => {
Expand Down Expand Up @@ -2131,7 +2117,7 @@ describe('Database', () => {
sandbox.stub(fakePool, 'release') as sinon.SinonStub
).withArgs(fakeSession);

database.runPartitionedUpdate(QUERY, async (err, rowCount) => {
database.runPartitionedUpdate(QUERY, async () => {
const exportResults = await getTraceExportResults();
const actualSpanNames = exportResults.spanNames;
const spans = exportResults.spans;
Expand Down
4 changes: 2 additions & 2 deletions observability-test/observability.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('startTrace', () => {
'aSpan',
{opts: {tracerProvider: overridingProvider}},
async span => {
await new Promise((resolve, reject) => setTimeout(resolve, 400));
await new Promise(resolve => setTimeout(resolve, 400));
span.end();

const gotSpansFromGlobal = globalExporter.getFinishedSpans();
Expand Down Expand Up @@ -250,7 +250,7 @@ describe('startTrace', () => {
'aSpan',
{opts: {tracerProvider: overridingProvider}},
async span => {
await new Promise((resolve, reject) => setTimeout(resolve, 400));
await new Promise(resolve => setTimeout(resolve, 400));
span.end();

const gotSpansFromGlobal = globalExporter.getFinishedSpans();
Expand Down
4 changes: 2 additions & 2 deletions observability-test/session-pool.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ describe('SessionPool', () => {
it('on exception from Database.batchCreateSessions', async () => {
const ourException = new Error('this fails intentionally');
sandbox.stub(DATABASE, 'batchCreateSessions').throws(ourException);
const releaseStub = sandbox.stub(sessionPool, 'release');
sandbox.stub(sessionPool, 'release');

assert.rejects(async () => {
await sessionPool._createSessions(OPTIONS);
Expand Down Expand Up @@ -167,7 +167,7 @@ describe('SessionPool', () => {
const RESPONSE = [[{}, {}, {}]];

sandbox.stub(DATABASE, 'batchCreateSessions').resolves(RESPONSE);
const releaseStub = sandbox.stub(sessionPool, 'release');
sandbox.stub(sessionPool, 'release');

await sessionPool._createSessions(OPTIONS);
assert.strictEqual(sessionPool.size, 3);
Expand Down
Loading

0 comments on commit a588465

Please sign in to comment.