diff --git a/Spanner/src/SpannerClient.php b/Spanner/src/SpannerClient.php index 822e87b23a79..ed289bed9976 100644 --- a/Spanner/src/SpannerClient.php +++ b/Spanner/src/SpannerClient.php @@ -1057,7 +1057,6 @@ private function configureMetrics(array $options): void { $metricsClient = $this->pluck('metricServiceClient', $options, false); $timeoutMillis = $this->pluck('metricsTimeoutMillis', $options, false) ?? 100; - $location = $this->getLocation(); if (!$this->pluck('enableBuiltInMetrics', $options, false)) { return; @@ -1087,6 +1086,7 @@ private function configureMetrics(array $options): void throw new ValidationException('The "metricServiceClient" option must be a MetricServiceClient instance.'); } + $location = $this->getLocation(); $metricsClientId = RUUID::uuid4()->toString() . '-' . getmypid(); $exporter = new MetricsExporter($metricsClient, $this->projectId, $metricsClientId, $timeoutMillis); $reader = new ExportingReader($exporter); diff --git a/Spanner/tests/System/PartitionedDmlTest.php b/Spanner/tests/System/PartitionedDmlTest.php index ecf4b3fb6f1a..a12b2d6a3f53 100644 --- a/Spanner/tests/System/PartitionedDmlTest.php +++ b/Spanner/tests/System/PartitionedDmlTest.php @@ -88,7 +88,7 @@ private function seedTable() private function executeInsert(array $rows) { self::$database->runTransaction(function ($t) use ($rows) { - $t->insertBatch(self::PDML_TABLE, $rows); + $t->insertOrUpdateBatch(self::PDML_TABLE, $rows); $t->commit(); }); diff --git a/Spanner/tests/System/PgPartitionedDmlTest.php b/Spanner/tests/System/PgPartitionedDmlTest.php index 48bce3dbf0e2..b72c3d0d537a 100644 --- a/Spanner/tests/System/PgPartitionedDmlTest.php +++ b/Spanner/tests/System/PgPartitionedDmlTest.php @@ -47,7 +47,7 @@ public function testPdml() $db = self::$database; - $db->updateDdl('CREATE TABLE ' . self::PDML_TABLE . '( + $db->updateDdl('CREATE TABLE IF NOT EXISTS ' . self::PDML_TABLE . '( id bigint NOT NULL, stringField varchar(1024), boolField BOOL, @@ -95,7 +95,7 @@ private function seedTable() private function executeInsert(array $rows) { self::$database->runTransaction(function ($t) use ($rows) { - $t->insertBatch(self::PDML_TABLE, $rows); + $t->insertOrUpdateBatch(self::PDML_TABLE, $rows); $t->commit(); }); diff --git a/Spanner/tests/System/PgTransactionTest.php b/Spanner/tests/System/PgTransactionTest.php index e0718a5f07d7..a76b716505cf 100644 --- a/Spanner/tests/System/PgTransactionTest.php +++ b/Spanner/tests/System/PgTransactionTest.php @@ -53,7 +53,7 @@ public static function setUpTestFixtures(): void self::$tableName = 'transactions_test'; self::$database->updateDdlBatch([ - 'CREATE TABLE ' . self::$tableName . ' ( + 'CREATE TABLE IF NOT EXISTS ' . self::$tableName . ' ( id bigint NOT NULL, number bigint NOT NULL, PRIMARY KEY (id) @@ -77,7 +77,7 @@ public function testRunTransaction() $db->runTransaction(function ($t) { $id = rand(1, 346464); - $t->insert(self::TEST_TABLE_NAME, [ + $t->insertOrUpdate(self::TEST_TABLE_NAME, [ 'id' => $id, 'name' => uniqid(self::TESTING_PREFIX), 'birthday' => new Date(new \DateTime('2000-01-01')) @@ -142,15 +142,16 @@ public function testRunTransactionWithDbRole($db, $values, $expected) $this->skipEmulatorTests(); $error = null; + $newName = uniqid('Doug'); $row = $this->getRow(); - $row['name'] = 'Doug'; + $row['name'] = $newName; $db->runTransaction(function ($t) use ($row) { $t->update(self::TEST_TABLE_NAME, $row); $t->commit(); }); $row = $this->getRow(); - $this->assertEquals('Doug', $row['name']); + $this->assertEquals($newName, $row['name']); try { $db->runTransaction(function ($t) use ($values) { diff --git a/Spanner/tests/System/TransactionTest.php b/Spanner/tests/System/TransactionTest.php index 40af04f895bb..74d7f7649b1a 100644 --- a/Spanner/tests/System/TransactionTest.php +++ b/Spanner/tests/System/TransactionTest.php @@ -93,7 +93,7 @@ public function testRunTransaction() $cols = array_keys($row); $db->runTransaction(function ($t) use ($row) { - $t->insert(self::TEST_TABLE_NAME, $row); + $t->insertOrUpdate(self::TEST_TABLE_NAME, $row); $t->commit(); }); @@ -272,15 +272,16 @@ public function testRunTransactionWithDbRole($db, $values, $expected) $this->skipEmulatorTests(); $error = null; + $newName = uniqid('Doug'); $row = $this->getRow(); - $row['name'] = 'Doug'; + $row['name'] = $newName; $db->runTransaction(function ($t) use ($row) { $t->update(self::TEST_TABLE_NAME, $row); $t->commit(); }); $row = $this->getRow(); - $this->assertEquals('Doug', $row['name']); + $this->assertEquals($newName, $row['name']); try { $db->runTransaction(function ($t) use ($values) { diff --git a/Spanner/tests/Unit/SpannerClientTest.php b/Spanner/tests/Unit/SpannerClientTest.php index 75dcbec33665..00e464028f6a 100644 --- a/Spanner/tests/Unit/SpannerClientTest.php +++ b/Spanner/tests/Unit/SpannerClientTest.php @@ -900,4 +900,24 @@ public function testBuiltinMetricsCanBeEnabled() 'enableBuiltInMetrics' => true, ]); } + + /** + * @runInSeparateProcess + */ + public function testSpannerClientInstantiatesWithoutDelayWhenMetricsDisabled() + { + // This test ensures that the SpannerClient does not hit the GCE metadata server + // when enableBuiltInMetrics is false, which prevents a 1.5 second delay. + // It's run in a separate process to ensure the GCE static cache is empty. + $start = microtime(true); + new SpannerClient([ + 'projectId' => self::PROJECT, + 'credentials' => Fixtures::KEYFILE_STUB_FIXTURE() + ]); + $end = microtime(true); + + // Assert that the client instantiated quickly. + // If it probed the GCE metadata server without a mock, it would take ~1.5s to time out. + $this->assertLessThan(1.0, $end - $start); + } }