Skip to content

Commit 4b60c45

Browse files
committed
test(spanner): avoid ID collisions by using self::randId() with a larger range
test(spanner): restore seedTable in BatchTest to fix test coverage test(spanner): fix array_rand bug and batch mutations in seedTable test(spanner): fix fundamentally broken partitionRead test logic in BatchTest fix: move seedTable back to its original location fix(cs): fix style issues in BatchTest.php fix(cs): format multi-line args
1 parent c0549d3 commit 4b60c45

5 files changed

Lines changed: 47 additions & 28 deletions

File tree

Core/src/Testing/System/SystemTestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public static function processQueue()
7676
*/
7777
public static function randId()
7878
{
79-
return rand(1, 9999999);
79+
return rand(1, 999999999);
8080
}
8181

8282
/**

Spanner/tests/System/BatchTest.php

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,30 @@ class BatchTest extends SystemTestCase
4444
public static function setUpTestFixtures(): void
4545
{
4646
self::setUpTestDatabase();
47+
if (self::$isSetup) {
48+
return;
49+
}
50+
self::seedTable();
51+
self::$isSetup = true;
52+
}
53+
54+
private static function seedTable()
55+
{
56+
$decades = [1950, 1960, 1970, 1980, 1990, 2000];
57+
$mutations = [];
58+
59+
for ($i = 0; $i < 250; $i++) {
60+
$mutations[] = [
61+
'id' => self::randId(),
62+
'decade' => $decades[array_rand($decades)]
63+
];
64+
}
65+
66+
self::$database->insertOrUpdateBatch(
67+
self::TABLE_NAME,
68+
$mutations,
69+
['timeoutMillis' => 50000]
70+
);
4771
}
4872

4973
public function testBatch()
@@ -83,19 +107,10 @@ public function testBatch()
83107
}
84108
$this->assertEquals(count($resultSet), $this->executePartitions($batch, $snapshot, $partitions));
85109

86-
$keySet = new KeySet([
87-
'ranges' => [
88-
new KeyRange([
89-
'start' => $parameters['earlyBound'],
90-
'startType' => KeyRange::TYPE_OPEN,
91-
'end' => $parameters['lateBound'],
92-
'endType' => KeyRange::TYPE_OPEN
93-
])
94-
]
95-
]);
110+
$keySet = new KeySet(['all' => true]);
96111

97112
$partitions = $snapshot->partitionRead(self::TABLE_NAME, $keySet, ['id', 'decade']);
98-
$this->assertEquals(count($resultSet), $this->executePartitions($batch, $snapshot, $partitions));
113+
$this->assertEquals(250, $this->executePartitions($batch, $snapshot, $partitions));
99114
}
100115

101116
/**

Spanner/tests/System/PgBatchTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,13 +121,17 @@ private function executePartitions(BatchClient $client, BatchSnapshot $snapshot,
121121
private static function seedTable()
122122
{
123123
$decades = [1950, 1960, 1970, 1980, 1990, 2000];
124+
$mutations = [];
125+
124126
for ($i = 0; $i < 250; $i++) {
125-
self::$database->insert(self::TABLE_NAME, [
127+
$mutations[] = [
126128
'id' => self::randId(),
127-
'decade' => array_rand($decades)
128-
], [
129-
'timeoutMillis' => 50000
130-
]);
129+
'decade' => $decades[array_rand($decades)]
130+
];
131131
}
132+
133+
self::$database->insertBatch(self::TABLE_NAME, $mutations, [
134+
'timeoutMillis' => 50000
135+
]);
132136
}
133137
}

Spanner/tests/System/PgTransactionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public static function setUpTestFixtures(): void
5151
}
5252
self::setUpTestDatabase();
5353

54-
self::$id1 = rand(1000, 9999);
54+
self::$id1 = self::randId();
5555
self::$row = [
5656
'id' => self::$id1,
5757
'name' => uniqid(self::TESTING_PREFIX),
@@ -67,7 +67,7 @@ public function testRunTransaction()
6767
$db = self::$database;
6868

6969
$db->runTransaction(function ($t) {
70-
$id = rand(1, 346464);
70+
$id = self::randId();
7171
$t->insertOrUpdate(self::TEST_TABLE_NAME, [
7272
'id' => $id,
7373
'name' => uniqid(self::TESTING_PREFIX),
@@ -146,7 +146,7 @@ public function testRunTransactionWithDbRole($db, $values, $expected)
146146

147147
try {
148148
$db->runTransaction(function ($t) use ($values) {
149-
$id = rand(1, 346464);
149+
$id = self::randId();
150150
$t->insert(self::TEST_TABLE_NAME, $values);
151151

152152
$t->commit();

Spanner/tests/System/TransactionTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function setUpTestFixtures(): void
5858
}
5959
self::setUpTestDatabase();
6060

61-
self::$id1 = rand(1000, 9999);
61+
self::$id1 = self::randId();
6262

6363
self::$row = [
6464
'id' => self::$id1,
@@ -74,7 +74,7 @@ public static function setUpTestFixtures(): void
7474
public function testRunTransaction()
7575
{
7676
$db = self::$database;
77-
$id = rand(1, 346464);
77+
$id = self::randId();
7878
$keySet = new KeySet([
7979
'keys' => [$id]
8080
]);
@@ -278,7 +278,7 @@ public function testRunTransactionWithDbRole($db, $values, $expected)
278278

279279
try {
280280
$db->runTransaction(function ($t) use ($values) {
281-
$id = rand(1, 346464);
281+
$id = self::randId();
282282
$t->insert(self::TEST_TABLE_NAME, $values);
283283

284284
$t->commit();
@@ -405,7 +405,7 @@ public function testRunTransactionILBWithMultipleOperations()
405405
$db = self::$database;
406406

407407
$res = $db->runTransaction(function ($t) {
408-
$id = rand(1, 346464);
408+
$id = self::randId();
409409
$row = [
410410
'id' => $id,
411411
'name' => uniqid(self::TESTING_PREFIX),
@@ -415,7 +415,7 @@ public function testRunTransactionILBWithMultipleOperations()
415415
$t->insert(self::TEST_TABLE_NAME, $row);
416416
$this->assertNull($t->id());
417417

418-
$id = rand(1, 346464);
418+
$id = self::randId();
419419
$t->executeUpdate(
420420
'INSERT INTO ' . self::TEST_TABLE_NAME . ' (id, name, birthday) VALUES (@id, @name, @birthday)',
421421
[
@@ -489,7 +489,7 @@ public function testTransactionToChannelAffinity()
489489
};
490490

491491
$res = $db->runTransaction(function ($t) use ($getChannel) {
492-
$id = rand(1, 346464);
492+
$id = self::randId();
493493
$row = [
494494
'id' => $id,
495495
'name' => uniqid(self::TESTING_PREFIX),
@@ -499,7 +499,7 @@ public function testTransactionToChannelAffinity()
499499
$t->insert(self::TEST_TABLE_NAME, $row);
500500
$this->assertNull($t->id());
501501

502-
$id = rand(1, 346464);
502+
$id = self::randId();
503503
$t->executeUpdate(
504504
'INSERT INTO ' . self::TEST_TABLE_NAME . ' (id, name, birthday) VALUES (@id, @name, @birthday)',
505505
[
@@ -677,7 +677,7 @@ private function getMultipleRows(int $total)
677677
// if $total is 10, then we will generate 9 rows.
678678
for ($i = 0; $i < $total; $i++) {
679679
$rows[] = [
680-
'id' => rand(1, 346464),
680+
'id' => self::randId(),
681681
'name' => uniqid(self::TESTING_PREFIX),
682682
'birthday' => new Date(new \DateTime('2000-01-01'))
683683
];

0 commit comments

Comments
 (0)