Skip to content

Commit 4fe4306

Browse files
committed
add cache class validation, add autoloader validation
1 parent 905075e commit 4fe4306

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

Spanner/tests/Unit/Session/SessionCacheTest.php

Lines changed: 6 additions & 3 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\SysVCacheItemPool;
2021
use Google\Cloud\Spanner\Session\SessionCache;
2122
use Google\Cloud\Spanner\V1\Client\SpannerClient;
2223
use Google\Cloud\Spanner\V1\Session;
@@ -26,6 +27,7 @@
2627
use Prophecy\PhpUnit\ProphecyTrait;
2728
use Psr\Cache\CacheItemInterface;
2829
use Psr\Cache\CacheItemPoolInterface;
30+
use ReflectionClass;
2931
use Symfony\Component\Process\Process;
3032

3133
/**
@@ -72,9 +74,7 @@ public function testEnsureValidSessionCacheHit()
7274
$session = new SessionCache(
7375
$this->spannerClient->reveal(),
7476
$this->databaseName,
75-
[
76-
'cacheItemPool' => $cacheItemPool->reveal(),
77-
]
77+
['cacheItemPool' => $cacheItemPool->reveal()]
7878
);
7979
$name = $session->name();
8080
$this->assertEquals($this->sessionName, $name);
@@ -181,6 +181,9 @@ public function testCacheLocking()
181181
$databaseName,
182182
);
183183

184+
$cacheItemPoolProp = (new ReflectionClass($sessionCache))->getProperty('cacheItemPool');
185+
$this->assertInstanceOf(SysVCacheItemPool::class, $cacheItemPoolProp->getValue($sessionCache));
186+
184187
$process = new Process(['php', __DIR__ . '/lock_test_process.php', $databaseName]);
185188
$process->setTimeout(5);
186189

Spanner/tests/Unit/Session/lock_test_process.php

Lines changed: 21 additions & 6 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\SysVCacheItemPool;
68
use Google\Cloud\Spanner\Session\SessionCache;
79
use Google\Cloud\Spanner\V1\Client\SpannerClient;
810
use Prophecy\Argument;
@@ -15,12 +17,21 @@
1517
die('Usage: lock_test_process.php DATABASE_NAME' . PHP_EOL);
1618
}
1719

18-
if (file_exists(__DIR__ . '/../../../vendor/autoload.php')) {
20+
$spannerAutoload = __DIR__ . '/../../../vendor/autoload.php';
21+
$googleCloudAutoload = __DIR__ . '/../../../../vendor/autoload.php';
22+
23+
if (file_exists($spannerAutoload) && file_exists($googleCloudAutoload)) {
24+
throw new Exception('Both autoloaders exist, please remove one');
25+
}
26+
27+
if (file_exists($spannerAutoload)) {
1928
// google/cloud-spanner autoload
20-
require __DIR__ . '/../../../vendor/autoload.php';
21-
} elseif (file_exists(__DIR__ . '/../../../../vendor/autoload.php')) {
29+
require $spannerAutoload;
30+
} elseif (file_exists($googleCloudAutoload)) {
2231
// google/cloud autoload
23-
require __DIR__ . '/../../../../vendor/autoload.php';
32+
require $googleCloudAutoload;
33+
} else {
34+
throw new Exception('no autoloader found');
2435
}
2536

2637
BypassFinals::enable();
@@ -39,13 +50,17 @@ public function run(): string
3950
->will(function () {
4051
throw new \Exception('createSession called in child process - this shouldn\'t happen');
4152
});
42-
43-
$parts = explode('/', $this->databaseName);
4453
$sessionCache = new SessionCache(
4554
$spannerClient->reveal(),
4655
$this->databaseName,
4756
);
4857

58+
$cacheItemPoolProp = (new \ReflectionClass($sessionCache))->getProperty('cacheItemPool');
59+
$cacheItemPool = $cacheItemPoolProp->getValue($sessionCache);
60+
if (!$cacheItemPool instanceof SysVCacheItemPool) {
61+
throw new \Exception('Expected SysvCacheItemPool, found ' . get_class($cacheItemPool));
62+
}
63+
4964
return $sessionCache->name();
5065
}
5166

0 commit comments

Comments
 (0)