Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Spanner/src/SpannerClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion Spanner/tests/System/PartitionedDmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
4 changes: 2 additions & 2 deletions Spanner/tests/System/PgPartitionedDmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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();
});
Expand Down
9 changes: 5 additions & 4 deletions Spanner/tests/System/PgTransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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'))
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 4 additions & 3 deletions Spanner/tests/System/TransactionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

Expand Down Expand Up @@ -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) {
Expand Down
20 changes: 20 additions & 0 deletions Spanner/tests/Unit/SpannerClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Loading