Skip to content

Commit

Permalink
test: add test for aborted blind writes (googleapis#1904)
Browse files Browse the repository at this point in the history
* test: add test for aborted blind writes

* chore: remove unrelated change

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* chore: fix linting errors

---------

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
Co-authored-by: surbhigarg92 <surbhigarg.92@gmail.com>
  • Loading branch information
3 people authored and alkatrivedi committed Dec 7, 2023
1 parent 1502c8b commit 8888cae
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions test/spanner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3194,6 +3194,56 @@ describe('Spanner with mock server', () => {
assert.ok(!beginTxnRequest, 'beginTransaction was called');
});

it('should apply blind writes only once', async () => {
const database = newTestDatabase();
let attempts = 0;
await database.runTransactionAsync(async tx => {
attempts++;
if (attempts === 1) {
spannerMock.abortTransaction(tx);
}
tx!.insert('foo', {id: 1, value: 'One'});
await tx!.run(insertSql);
await tx.commit();
});
await database.close();

assert.strictEqual(2, attempts);
// Verify that we have 2 ExecuteSqlRequests. The first one should use inline-begin. The second one should use a
// transaction ID.
const firstExecuteSqlRequest = spannerMock.getRequests().find(val => {
return (
(val as v1.ExecuteSqlRequest).sql === insertSql &&
(val as v1.ExecuteSqlRequest).transaction?.begin
);
}) as v1.ExecuteSqlRequest;
assert.ok(firstExecuteSqlRequest.transaction?.begin?.readWrite);
const secondExecuteSqlRequest = spannerMock.getRequests().find(val => {
return (
(val as v1.ExecuteSqlRequest).sql === insertSql &&
(val as v1.ExecuteSqlRequest).transaction?.id
);
}) as v1.ExecuteSqlRequest;
assert.ok(secondExecuteSqlRequest.transaction?.id);
// Verify that we have a BeginTransaction request for the retry.
const beginTxnRequest = spannerMock.getRequests().find(val => {
return (val as v1.BeginTransactionRequest).options?.readWrite;
}) as v1.BeginTransactionRequest;
assert.ok(beginTxnRequest, 'beginTransaction was called');
// Verify that we have a single Commit request, and that the Commit request contains only one mutation.
assert.strictEqual(
1,
spannerMock.getRequests().filter(val => {
return (val as v1.CommitRequest).mutations;
}).length
);
const commitRequest = spannerMock.getRequests().find(val => {
return (val as v1.CommitRequest).mutations;
}) as v1.CommitRequest;
assert.ok(commitRequest, 'Commit was called');
assert.strictEqual(commitRequest.mutations.length, 1);
});

it('should use optimistic lock for runTransactionAsync', async () => {
const database = newTestDatabase();
await database.runTransactionAsync(
Expand Down

0 comments on commit 8888cae

Please sign in to comment.