Skip to content

Commit

Permalink
Ensure releasing a session after pool has been cleared does not error (
Browse files Browse the repository at this point in the history
…#1071)

* ensure releasing a session after pool has been cleared does not error

* fix spacing
  • Loading branch information
dwsupplee authored and jdpedrie committed May 25, 2018
1 parent 41ff6c4 commit 78196c4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Spanner/src/Session/CacheSessionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,17 @@ public function release(Session $session)
$this->config['lock']->synchronize(function () use ($session) {
$item = $this->cacheItemPool->getItem($this->cacheKey);
$data = $item->get();

unset($data['inUse'][$session->name()]);
array_push($data['queue'], [
'name' => $session->name(),
'expiration' => $session->expiration()
?: $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS
]);
$this->cacheItemPool->save($item->set($data));
$name = $session->name();

if (isset($data['inUse'][$name])) {
unset($data['inUse'][$name]);
array_push($data['queue'], [
'name' => $name,
'expiration' => $session->expiration()
?: $this->time() + SessionPoolInterface::SESSION_EXPIRATION_SECONDS
]);
$this->cacheItemPool->save($item->set($data));
}
});
}

Expand Down
21 changes: 21 additions & 0 deletions Spanner/tests/Unit/Session/CacheSessionPoolTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,27 @@ public function testClearPool()
$this->assertNull($actualCacheData);
}

public function testReleaseSessionAfterClearingPoolSucceeds()
{
$pool = new CacheSessionPoolStub($this->getCacheItemPool());
$pool->setDatabase($this->getDatabase());
$session = $pool->acquire();
$itemPool = $pool->cacheItemPool();
$pool->clear();
$cacheData = $itemPool->getItem(
sprintf(self::CACHE_KEY_TEMPLATE, self::PROJECT_ID, self::INSTANCE_NAME, self::DATABASE_NAME)
)->get();

$this->assertNull($cacheData);

$pool->release($session);
$cacheData = $itemPool->getItem(
sprintf(self::CACHE_KEY_TEMPLATE, self::PROJECT_ID, self::INSTANCE_NAME, self::DATABASE_NAME)
)->get();

$this->assertNull($cacheData);
}

/**
* @dataProvider acquireDataProvider
*/
Expand Down

0 comments on commit 78196c4

Please sign in to comment.