Tests garbage collection.
public function testGarbageCollection() {
$collection = $this
->randomName();
$store = new DatabaseStorageExpirable($collection, Database::getConnection());
// Insert some items and confirm that they're set.
for ($i = 0; $i <= 3; $i++) {
$store
->setWithExpire('key_' . $i, $this
->randomObject(), rand(500, 100000));
}
$this
->assertIdentical(sizeof($store
->getAll()), 4, 'Four items were written to the storage.');
// Manually expire the data.
for ($i = 0; $i <= 3; $i++) {
db_merge('key_value_expire')
->key(array(
'name' => 'key_' . $i,
'collection' => $collection,
))
->fields(array(
'expire' => REQUEST_TIME - 1,
))
->execute();
}
// Perform a new set operation and then manually unset the object to
// trigger garbage collection.
$store
->setWithExpire('autumn', 'winter', rand(500, 1000000));
unset($store);
// Query the database and confirm that the stale records were deleted.
$result = db_query('SELECT name, value FROM {key_value_expire} WHERE collection = :collection', array(
':collection' => $collection,
))
->fetchAll();
$this
->assertIdentical(sizeof($result), 1, 'Only one item remains after garbage collection');
}