Skip to content

Commit c164ecb

Browse files
committed
refactor test
1 parent a446455 commit c164ecb

File tree

4 files changed

+12
-53
lines changed

4 files changed

+12
-53
lines changed

src/transaction.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,8 @@ export class Snapshot extends EventEmitter {
390390
if (
391391
this._latestPreCommitToken === null ||
392392
this._latestPreCommitToken === undefined ||
393-
this._latestPreCommitToken!.seqNum! < resp.precommitToken!.seqNum!
393+
(resp.precommitToken &&
394+
this._latestPreCommitToken!.seqNum! < resp.precommitToken!.seqNum!)
394395
) {
395396
this._latestPreCommitToken = resp.precommitToken;
396397
}

system-test/spanner.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -9241,40 +9241,6 @@ describe('Spanner', () => {
92419241
// wait for both the transactions to complete their execution
92429242
await Promise.all(promises);
92439243
});
9244-
9245-
it('POSTGRESQL should insert and commit transactions when running parallely', async () => {
9246-
const promises: Promise<void>[] = [];
9247-
let resolvePromise;
9248-
const commitPromise = new Promise(
9249-
resolve => (resolvePromise = resolve),
9250-
);
9251-
const sync = {
9252-
target: 2, // both the transactions to be ready
9253-
count: 0, // 0 transactions are ready so far
9254-
promise: commitPromise, // the promise both the transactions wait at
9255-
resolveCommitPromise: () => resolvePromise(), // the function to resolve the commit promise
9256-
};
9257-
// run the transactions in parallel
9258-
promises.push(
9259-
insertAndCommitTransaction(
9260-
PG_DATABASE,
9261-
sync,
9262-
postgreSqlTable,
9263-
'k1102',
9264-
),
9265-
);
9266-
promises.push(
9267-
insertAndCommitTransaction(
9268-
PG_DATABASE,
9269-
sync,
9270-
postgreSqlTable,
9271-
'k1103',
9272-
),
9273-
);
9274-
9275-
// wait for both the transactions to complete their execution
9276-
await Promise.all(promises);
9277-
});
92789244
});
92799245

92809246
const rollbackTransaction = (done, database) => {

test/mockserver/mockspanner.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ export class MockSpanner {
321321
private metadata: Metadata[] = [];
322322
private frozen = 0;
323323
private sessionCounter = 0;
324+
private _seqNum = 0;
324325
private sessions: Map<string, protobuf.Session> = new Map<
325326
string,
326327
protobuf.Session
@@ -371,6 +372,7 @@ export class MockSpanner {
371372
this.partitionRead = this.partitionRead.bind(this);
372373
this.batchWrite = this.batchWrite.bind(this);
373374
this.mutationOnly = false;
375+
this._seqNum = 1;
374376
}
375377

376378
/**
@@ -704,7 +706,7 @@ export class MockSpanner {
704706
const precommitToken = session?.multiplexed
705707
? protobuf.MultiplexedSessionPrecommitToken.create({
706708
precommitToken: Buffer.from('mock-precommit-token'),
707-
seqNum: randomInt(1, 1000),
709+
seqNum: this._seqNum++,
708710
})
709711
: null;
710712
if (txn instanceof Error) {
@@ -816,6 +818,7 @@ export class MockSpanner {
816818
const partial = protobuf.PartialResultSet.create({
817819
resumeToken: Buffer.from(token),
818820
values: [],
821+
precommitToken: resultSet.precommitToken,
819822
});
820823
for (
821824
let row = i;
@@ -1022,7 +1025,7 @@ export class MockSpanner {
10221025
const precommitToken = session?.multiplexed
10231026
? protobuf.MultiplexedSessionPrecommitToken.create({
10241027
precommitToken: Buffer.from('mock-precommit-token'),
1025-
seqNum: randomInt(1, 1000),
1028+
seqNum: this._seqNum++,
10261029
})
10271030
: null;
10281031
if (txn instanceof Error) {
@@ -1277,7 +1280,7 @@ export class MockSpanner {
12771280
this.mutationOnly && session.multiplexed && options?.readWrite
12781281
? {
12791282
precommitToken: Buffer.from('mock-precommit-token'),
1280-
seqNum: randomInt(1, 1000),
1283+
seqNum: this._seqNum++,
12811284
}
12821285
: null;
12831286
const transaction = protobuf.Transaction.create({

test/spanner.ts

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4671,7 +4671,7 @@ describe('Spanner with mock server', () => {
46714671
MultiplexedSessionRetry: 'precommitToken',
46724672
precommitToken: {
46734673
precommitToken: fakeRetryToken,
4674-
seqNum: 1,
4674+
seqNum: 100,
46754675
},
46764676
commitTimestamp: mock.now(),
46774677
};
@@ -4701,7 +4701,7 @@ describe('Spanner with mock server', () => {
47014701
await tx!.run(selectSql);
47024702

47034703
// perform mutations
4704-
await tx.upsert('foo', [
4704+
tx.upsert('foo', [
47054705
{id: 1, name: 'One'},
47064706
{id: 2, name: 'Two'},
47074707
]);
@@ -4715,13 +4715,7 @@ describe('Spanner with mock server', () => {
47154715
2,
47164716
'The mock commit method should have been called exactly twice.',
47174717
);
4718-
const firstRequest = capturedCommitRequests[0];
4719-
// assert that during the first request to commit
4720-
// the precommitToken was missing
4721-
assert.ok(
4722-
!firstRequest.precommitToken,
4723-
'The first commit request should not have a precommitToken.',
4724-
);
4718+
47254719
const secondRequest = capturedCommitRequests[1];
47264720
// assert that during the second request to commit
47274721
// the precommitToken was present
@@ -4740,6 +4734,7 @@ describe('Spanner with mock server', () => {
47404734
async function insertAndCommit() {
47414735
const database = newTestDatabase();
47424736
await database.runTransactionAsync(async tx => {
4737+
await tx.run(selectSql);
47434738
tx.upsert('foo', [
47444739
{id: 1, name: 'One'},
47454740
{id: 2, name: 'Two'},
@@ -4785,12 +4780,6 @@ describe('Spanner with mock server', () => {
47854780
commitRequest[0].precommitToken.precommitToken,
47864781
commitRequest[1].precommitToken.precommitToken,
47874782
);
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-
);
47944783
});
47954784
});
47964785
});

0 commit comments

Comments
 (0)