Skip to content

Commit 7382103

Browse files
committed
test: add mock spanner test
1 parent 1f32e90 commit 7382103

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

system-test/spanner.ts

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9214,13 +9214,11 @@ describe('Spanner', () => {
92149214

92159215
// increament the shared counter
92169216
sync.count++;
9217-
if (sync.count === sync.target) {
9218-
sync.openPromise();
9219-
}
92209217

9221-
// both the transactions will pause till count equals target
9222-
// (or wait for the both the transaction to ready for commit)
9223-
await sync.promise;
9218+
while (sync.count !== sync.target) {
9219+
// wait for 500 milliseconds
9220+
await new Promise(resolve => setTimeout(resolve, 500));
9221+
}
92249222

92259223
// commit the transaction once both the transactions are ready to commit
92269224
await transaction!.commit();
@@ -9229,15 +9227,9 @@ describe('Spanner', () => {
92299227

92309228
it('GOOGLE_STANDARD_SQL should insert and commit transaction when running parallely', async () => {
92319229
const promises: Promise<void>[] = [];
9232-
let resolvePromise;
9233-
const commitPromise = new Promise(
9234-
resolve => (resolvePromise = resolve),
9235-
);
92369230
const sync = {
92379231
target: 2, // both the transactions to be ready
9238-
count: 0, // 0 transactions are so far
9239-
promise: commitPromise, // the promise both the transactions wait at
9240-
openPromise: () => resolvePromise(),
9232+
count: 0, // 0 transactions are ready so far
92419233
};
92429234
// run the transactions in parallel
92439235
promises.push(
@@ -9253,15 +9245,9 @@ describe('Spanner', () => {
92539245

92549246
it('POSTGRESQL should insert and commit transactions when running parallely', async () => {
92559247
const promises: Promise<void>[] = [];
9256-
let resolvePromise;
9257-
const commitPromise = new Promise(
9258-
resolve => (resolvePromise = resolve),
9259-
);
92609248
const sync = {
92619249
target: 2, // both the transactions to be ready
9262-
count: 0, // 0 transactions are so far
9263-
promise: commitPromise, // the promise both the transactions wait at
9264-
openPromise: () => resolvePromise(),
9250+
count: 0, // 0 transactions are ready so far
92659251
};
92669252
// run the transactions in parallel
92679253
promises.push(

test/spanner.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4734,6 +4734,65 @@ describe('Spanner with mock server', () => {
47344734
await database.close();
47354735
});
47364736
});
4737+
4738+
// parallel transactions
4739+
describe('parallel transactions', async () => {
4740+
async function insertAndCommit() {
4741+
const database = newTestDatabase();
4742+
await database.runTransactionAsync(async tx => {
4743+
tx.upsert('foo', [
4744+
{id: 1, name: 'One'},
4745+
{id: 2, name: 'Two'},
4746+
]);
4747+
await tx.commit();
4748+
});
4749+
}
4750+
it('should have different precommit tokens for each transactions when running parallely', async () => {
4751+
const promises: Promise<void>[] = [];
4752+
4753+
// run the transactions parallely
4754+
promises.push(insertAndCommit());
4755+
promises.push(insertAndCommit());
4756+
4757+
// wait for the transaction to complete its execution
4758+
await Promise.all(promises);
4759+
4760+
const commitRequest = spannerMock.getRequests().filter(val => {
4761+
return (val as v1.CommitRequest).precommitToken;
4762+
}) as v1.CommitRequest[];
4763+
4764+
// assert that there are two commit requests one for each transaction
4765+
assert.strictEqual(commitRequest.length, 2);
4766+
4767+
// assert that precommitToken is not null during first request to commit
4768+
assert.notEqual(commitRequest[0].precommitToken, null);
4769+
4770+
// assert that precommitToken is instance of Buffer
4771+
assert.ok(
4772+
commitRequest[0].precommitToken?.precommitToken instanceof Buffer,
4773+
);
4774+
4775+
// assert that precommitToken is not null during second request to commit
4776+
assert.notEqual(commitRequest[1].precommitToken, null);
4777+
4778+
// assert that precommitToken is instance of Buffer
4779+
assert.ok(
4780+
commitRequest[1].precommitToken?.precommitToken instanceof Buffer,
4781+
);
4782+
4783+
// assert that precommitToken is different in both the commit request
4784+
assert.notEqual(
4785+
commitRequest[0].precommitToken.precommitToken,
4786+
commitRequest[1].precommitToken.precommitToken,
4787+
);
4788+
4789+
// assert that seqNum is different in both the commit request
4790+
assert.notEqual(
4791+
commitRequest[0].precommitToken.seqNum,
4792+
commitRequest[1].precommitToken.seqNum,
4793+
);
4794+
});
4795+
});
47374796
});
47384797
});
47394798

0 commit comments

Comments
 (0)