Skip to content

Commit 94604ac

Browse files
committed
fix: use google/auth caches by default
1 parent 01a2383 commit 94604ac

6 files changed

Lines changed: 34 additions & 31 deletions

File tree

Spanner/composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": "^8.1",
88
"ext-grpc": "*",
99
"google/cloud-core": "^1.68",
10-
"google/gax": "^1.38.0"
10+
"google/gax": "^1.38.1"
1111
},
1212
"require-dev": {
1313
"phpunit/phpunit": "^9.6",
@@ -19,7 +19,6 @@
1919
"google/cloud-pubsub": "^2.0",
2020
"dg/bypass-finals": "^1.7",
2121
"dms/phpunit-arraysubset-asserts": "^0.5.0",
22-
"symfony/cache": "^6.4",
2322
"symfony/process": "^6.4"
2423
},
2524
"suggest": {

Spanner/src/Session/SessionCache.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function __construct(
9393

9494
$this->routeToLeader = $options['routeToLeader'] ?? false;
9595
$this->cacheItemPool = $options['cacheItemPool'] ?? (
96-
extension_loaded('sysvshm')
96+
extension_loaded('sysvshm') && extension_loaded('sysvsem')
9797
? new SysVCacheItemPool()
9898
: new FileSystemCacheItemPool(sys_get_temp_dir() . '/spanner_cache/')
9999
);
@@ -207,6 +207,7 @@ public function __debugInfo()
207207
{
208208
return [
209209
'session' => $this->session,
210+
'cacheKey' => $this->cacheKey,
210211
'cacheItemPool' => $this->cacheItemPool,
211212
];
212213
}

Spanner/tests/System/SystemTestCaseTrait.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Google\Cloud\Spanner\SpannerClient;
2424
use Google\Cloud\Spanner\V1\Client\SpannerClient as SpannerGapicClient;
2525
use Google\Cloud\Spanner\Admin\Database\V1\DatabaseDialect;
26-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
2726

2827
trait SystemTestCaseTrait
2928
{
@@ -71,7 +70,6 @@ private static function getClient()
7170

7271
$clientConfig = [
7372
'keyFilePath' => $keyFilePath,
74-
'cacheItemPool' => self::getCacheItemPool(),
7573
];
7674

7775
$serviceAddress = getenv('SPANNER_SERVICE_ADDRESS');
@@ -192,11 +190,4 @@ private static function getDatabaseFromInstance($instance, $dbName, $options = [
192190
$instance = self::getClient()->instance($instance);
193191
return $instance->database($dbName, $options);
194192
}
195-
196-
private static function getCacheItemPool()
197-
{
198-
return new FilesystemAdapter(
199-
directory: __DIR__ . '/../../../.cache'
200-
);
201-
}
202193
}

Spanner/tests/Unit/Session/SessionCacheTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
namespace Google\Cloud\Spanner\Tests\Unit\Session;
1919

20+
use Google\Auth\Cache\FileSystemCacheItemPool;
2021
use Google\Cloud\Spanner\Session\SessionCache;
2122
use Google\Cloud\Spanner\V1\Client\SpannerClient;
2223
use Google\Cloud\Spanner\V1\Session;
@@ -26,7 +27,6 @@
2627
use Prophecy\PhpUnit\ProphecyTrait;
2728
use Psr\Cache\CacheItemInterface;
2829
use Psr\Cache\CacheItemPoolInterface;
29-
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
3030
use Symfony\Component\Process\Process;
3131

3232
/**
@@ -73,9 +73,7 @@ public function testEnsureValidSessionCacheHit()
7373
$session = new SessionCache(
7474
$this->spannerClient->reveal(),
7575
$this->databaseName,
76-
[
77-
'cacheItemPool' => $cacheItemPool->reveal(),
78-
]
76+
['cacheItemPool' => $cacheItemPool->reveal()]
7977
);
8078
$name = $session->name();
8179
$this->assertEquals($this->sessionName, $name);
@@ -177,13 +175,14 @@ public function testCacheLocking()
177175
// Use mt_rand to ensure the cache key is unique for each test run
178176
$databaseId = mt_rand();
179177
$databaseName = SpannerClient::databaseName(self::PROJECT, self::INSTANCE, $databaseId);
178+
$cachePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spannercache';
180179
$sessionCache = new SessionCache(
181180
$this->spannerClient->reveal(),
182181
$databaseName,
183-
['cacheItemPool' => new FilesystemAdapter($databaseId)]
182+
['cacheItemPool' => new FileSystemCacheItemPool($cachePath)],
184183
);
185184

186-
$process = new Process(['php', __DIR__ . '/lock_test_process.php', $databaseName]);
185+
$process = new Process(['php', __DIR__ . '/lock_test_process.php', $databaseName, $cachePath]);
187186
$process->setTimeout(5);
188187

189188
// Mock fetching the session from the API

Spanner/tests/Unit/Session/lock_test_process.php

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace Google\Cloud\Spanner\Tests\Unit\Session;
44

55
use DG\BypassFinals;
6+
use Exception;
7+
use Google\Auth\Cache\FileSystemCacheItemPool;
68
use Google\Cloud\Spanner\Session\SessionCache;
79
use Google\Cloud\Spanner\V1\Client\SpannerClient;
810
use Prophecy\Argument;
@@ -12,25 +14,38 @@
1214
/**
1315
* Runs a process which is designed to wait while a session is acquired.
1416
*/
15-
if (count($argv) !== 2) {
16-
die('Usage: lock_test_process.php DATABASE_NAME' . PHP_EOL);
17+
if (count($argv) !== 3) {
18+
die('Usage: lock_test_process.php DATABASE_NAME CACHE_PATH' . PHP_EOL);
1719
}
1820

19-
if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
21+
$spannerAutoload = __DIR__ . '/../../../vendor/autoload.php';
22+
$googleCloudAutoload = __DIR__ . '/../../../../vendor/autoload.php';
23+
24+
if (file_exists($spannerAutoload) && file_exists($googleCloudAutoload)) {
25+
throw new Exception('Both autoloaders exist, please remove one');
26+
}
27+
28+
if (file_exists($spannerAutoload)) {
2029
// google/cloud-spanner autoload
21-
require __DIR__ . '/../../../vendor/autoload.php';
22-
} elseif (file_exists(__DIR__ . '/../../../../vendor/autoload.php')) {
30+
require $spannerAutoload;
31+
} elseif (file_exists($googleCloudAutoload)) {
2332
// google/cloud autoload
24-
require __DIR__ . '/../../../../vendor/autoload.php';
33+
require $googleCloudAutoload;
34+
} else {
35+
throw new Exception('no autoloader found');
2536
}
2637

2738
BypassFinals::enable();
2839

29-
$acquireSession = new class($argv[1]) {
40+
[$_cmd, $databaseName, $cachePath] = $argv;
41+
42+
$acquireSession = new class($databaseName, $cachePath) {
3043
use ProphecyTrait;
3144

32-
public function __construct(private string $databaseName)
33-
{
45+
public function __construct(
46+
private string $databaseName,
47+
private string $cachePath
48+
) {
3449
}
3550

3651
public function run(): string
@@ -41,11 +56,10 @@ public function run(): string
4156
throw new \Exception('createSession called in child process - this shouldn\'t happen');
4257
});
4358

44-
$parts = explode('/', $this->databaseName);
4559
$sessionCache = new SessionCache(
4660
$spannerClient->reveal(),
4761
$this->databaseName,
48-
['cacheItemPool' => new FilesystemAdapter(array_pop($parts))]
62+
['cacheItemPool' => new FileSystemCacheItemPool($this->cachePath)]
4963
);
5064

5165
return $sessionCache->name();

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
"psr/http-message": "^1.0|^2.0",
6565
"ramsey/uuid": "^4.0",
6666
"google/common-protos": "^4.4",
67-
"google/gax": "^1.38.0",
67+
"google/gax": "^1.38.1",
6868
"google/auth": "^1.42"
6969
},
7070
"require-dev": {
@@ -80,7 +80,6 @@
8080
"dg/bypass-finals": "^1.7",
8181
"squizlabs/php_codesniffer": "3.*",
8282
"dms/phpunit-arraysubset-asserts": "^0.5.0",
83-
"symfony/cache": "^6.4",
8483
"symfony/process": "^6.4"
8584
},
8685
"replace": {

0 commit comments

Comments
 (0)