PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Cache/tests/replacement_strategy_lru_test.php

https://github.com/Yannix/zetacomponents
PHP | 1595 lines | 287 code | 41 blank | 1267 comment | 0 complexity | e42bfb28d9c2c807b251a0c278ebb8c1 MD5 | raw file
  1. <?php
  2. /**
  3. * ezcCacheStackLruReplacementStrategyTest
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Cache
  23. * @subpackage Tests
  24. * @version //autogen//
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. /**
  28. * Test suite for ezcCacheManager class.
  29. *
  30. * @package Cache
  31. * @subpackage Tests
  32. */
  33. class ezcCacheStackLruReplacementStrategyTest extends ezcTestCase
  34. {
  35. /**
  36. * suite
  37. *
  38. * @static
  39. * @access public
  40. */
  41. public static function suite()
  42. {
  43. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  44. }
  45. public function testCreateMetaData()
  46. {
  47. $this->assertEquals(
  48. new ezcCacheStackLruMetaData(),
  49. ezcCacheStackLruReplacementStrategy::createMetaData()
  50. );
  51. }
  52. public function testInvalidMetaData()
  53. {
  54. $conf = new ezcCacheStackStorageConfiguration(
  55. 'storage_id_1',
  56. new ezcCacheStorageFileArray(
  57. $this->createTempDir( __FUNCTION__ )
  58. ),
  59. 5,
  60. 0.5
  61. );
  62. $meta = new ezcCacheStackLfuMetaData();
  63. try
  64. {
  65. ezcCacheStackLruReplacementStrategy::store(
  66. $conf,
  67. $meta,
  68. 'barid',
  69. 'baz content'
  70. );
  71. $this->fail( 'Exception not thrown on invalid meta data.' );
  72. }
  73. catch ( ezcCacheInvalidMetaDataException $e ) {}
  74. $this->removeTempDir();
  75. }
  76. public function testStoreNewMetaNewItemNoFree()
  77. {
  78. $conf = new ezcCacheStackStorageConfiguration(
  79. 'storage_id_1',
  80. new ezcCacheStorageFileArray(
  81. $this->createTempDir( __FUNCTION__ )
  82. ),
  83. 5,
  84. 0.5
  85. );
  86. $meta = new ezcCacheStackLruMetaData();
  87. ezcCacheStackLruReplacementStrategy::store(
  88. $conf,
  89. $meta,
  90. 'id_1',
  91. 'baz content'
  92. );
  93. $this->assertEquals(
  94. 'baz content',
  95. $conf->storage->restore( 'id_1' ),
  96. 'Data not stored correctly.'
  97. );
  98. $metaData = $meta->getState();
  99. $this->assertGreaterThan(
  100. // Creation date of test case. Clock correct?
  101. 1209503807,
  102. $metaData['replacementData']['id_1'],
  103. 'Meta data entry not created correctly.'
  104. );
  105. $this->assertEquals(
  106. array(
  107. 'id_1' => true,
  108. ),
  109. $metaData['storageData']['storage_id_1']
  110. );
  111. $this->removeTempDir();
  112. }
  113. public function testStoreNoNewMetaNewItemNoFree()
  114. {
  115. $conf = new ezcCacheStackStorageConfiguration(
  116. 'storage_id_1',
  117. new ezcCacheStorageFileArray(
  118. $this->createTempDir( __FUNCTION__ )
  119. ),
  120. 5,
  121. 0.5
  122. );
  123. $meta = new ezcCacheStackLruMetaData();
  124. $meta->setState( array(
  125. 'replacementData' => array(
  126. 'id_42' => 23,
  127. ),
  128. 'storageData' => array(
  129. 'storage_id_42' => array(
  130. 'id_42' => true,
  131. ),
  132. ),
  133. ) );
  134. ezcCacheStackLruReplacementStrategy::store(
  135. $conf,
  136. $meta,
  137. 'id_1',
  138. 'id_1_content'
  139. );
  140. $metaData = $meta->getState();
  141. $this->assertEquals(
  142. 'id_1_content',
  143. $conf->storage->restore( 'id_1' ),
  144. 'Data not stored correctly.'
  145. );
  146. $this->assertGreaterThan(
  147. // Creation date of test case. Clock correct?
  148. 1209503807,
  149. $metaData['replacementData']['id_1'],
  150. 'Meta data entry not created correctly.'
  151. );
  152. $this->assertEquals(
  153. 23,
  154. $metaData['replacementData']['id_42'],
  155. 'Meta data entry not kept correctly.'
  156. );
  157. $this->assertEquals(
  158. array(
  159. 'storage_id_42' => array(
  160. 'id_42' => true,
  161. ),
  162. 'storage_id_1' => array(
  163. 'id_1' => true,
  164. )
  165. ),
  166. $metaData['storageData']
  167. );
  168. $this->removeTempDir();
  169. }
  170. public function testStoreOldItemNoFree()
  171. {
  172. $conf = new ezcCacheStackStorageConfiguration(
  173. 'storage_id_1',
  174. new ezcCacheStorageFileArray(
  175. $this->createTempDir( __FUNCTION__ )
  176. ),
  177. 5,
  178. 0.5
  179. );
  180. $meta = new ezcCacheStackLruMetaData();
  181. $meta->setState( array(
  182. 'replacementData' => array(
  183. 'id_1' => 23,
  184. ),
  185. 'storageData' => array(
  186. 'storage_id_1' => array(
  187. 'id_1' => true,
  188. ),
  189. ),
  190. ) );
  191. $conf->storage->store( 'id_1', 'id_1_content' );
  192. ezcCacheStackLruReplacementStrategy::store(
  193. $conf,
  194. $meta,
  195. 'id_1',
  196. 'id_1_content'
  197. );
  198. $metaData = $meta->getState();
  199. $this->assertEquals(
  200. 'id_1_content',
  201. $conf->storage->restore( 'id_1' ),
  202. 'Data not stored correctly.'
  203. );
  204. $this->assertGreaterThan(
  205. // Creation date of test case. Clock correct?
  206. 1209503807,
  207. $metaData['replacementData']['id_1'],
  208. 'Meta data entry not created correctly.'
  209. );
  210. $this->assertEquals(
  211. array(
  212. 'id_1' => true,
  213. ),
  214. $metaData['storageData']['storage_id_1']
  215. );
  216. $this->removeTempDir();
  217. }
  218. public function testStoreNoNewMetaNewItemFreePurge()
  219. {
  220. // Prepare faked test data
  221. $tmpDir = $this->createTempDir( __FUNCTION__ );
  222. $conf = new ezcCacheStackStorageConfiguration(
  223. 'storage_id_1',
  224. new ezcCacheStorageFileArray(
  225. $tmpDir,
  226. array( 'ttl' => 30 )
  227. ),
  228. 5,
  229. 0.5
  230. );
  231. $now = time();
  232. // Store max number of items
  233. $conf->storage->store( 'id_1', 'id_1_content' );
  234. $conf->storage->store( 'id_2', 'id_2_content' );
  235. $conf->storage->store( 'id_3', 'id_3_content' );
  236. $conf->storage->store( 'id_4', 'id_4_content' );
  237. $conf->storage->store( 'id_5', 'id_5_content' );
  238. // Expire 3 items
  239. touch(
  240. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_2' ),
  241. ( $now - 40 ),
  242. ( $now - 40 )
  243. );
  244. touch(
  245. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_3' ),
  246. ( $now - 40 ),
  247. ( $now - 40 )
  248. );
  249. touch(
  250. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_4' ),
  251. ( $now - 40 ),
  252. ( $now - 40 )
  253. );
  254. $meta = new ezcCacheStackLruMetaData();
  255. $meta->setState( array(
  256. 'replacementData' => array(
  257. // Fake access times, not necessarily reflect file mtimes
  258. 'id_1' => ( $now - 10 ),
  259. 'id_2' => ( $now - 40 ), // Expired
  260. 'id_3' => ( $now - 40 ), // Expired
  261. 'id_4' => ( $now - 40 ), // Expired
  262. 'id_5' => ( $now - 13 ),
  263. ),
  264. 'storageData' => array(
  265. 'storage_id_1' => array(
  266. 'id_1' => true,
  267. 'id_2' => true,
  268. 'id_3' => true,
  269. 'id_4' => true,
  270. 'id_5' => true,
  271. ),
  272. 'storage_id_42' => array(
  273. 'id_2' => true,
  274. 'id_5' => true,
  275. ),
  276. ),
  277. ) );
  278. // Perform actual action
  279. ezcCacheStackLruReplacementStrategy::store(
  280. $conf,
  281. $meta,
  282. 'id_6',
  283. 'id_6_content'
  284. );
  285. $metaData = $meta->getState();
  286. // Assert correct behaviour
  287. // Data has actually been stored
  288. $this->assertEquals(
  289. 'id_6_content',
  290. $conf->storage->restore( 'id_6' ),
  291. 'Data not stored correctly.'
  292. );
  293. // Time stamp has been stored correctly
  294. $this->assertGreaterThanOrEqual(
  295. $now,
  296. $metaData['replacementData']['id_6'],
  297. 'Meta data entry not created correctly.'
  298. );
  299. // Storage has been saved correctly
  300. $this->assertEquals(
  301. array(
  302. 'id_1' => true,
  303. 'id_5' => true,
  304. 'id_6' => true,
  305. ),
  306. $metaData['storageData']['storage_id_1'],
  307. 'Storage meta data not inserted correctly.'
  308. );
  309. // Remove stored item data from meta data again for comfortability
  310. unset( $metaData['replacementData']['id_6'] );
  311. // LRU data correctly updated
  312. $this->assertEquals(
  313. // Note the sorting was not performed!
  314. array(
  315. 'id_1' => ( $now - 10 ),
  316. 'id_2' => ( $now - 40 ),
  317. 'id_5' => ( $now - 13 ),
  318. ),
  319. $metaData['replacementData'],
  320. 'Meta data entries not correctly updated.'
  321. );
  322. // Storage data correctly updated
  323. $this->assertEquals(
  324. array(
  325. 'storage_id_1' => array(
  326. 'id_1' => true,
  327. 'id_5' => true,
  328. 'id_6' => true,
  329. ),
  330. 'storage_id_42' => array(
  331. 'id_2' => true,
  332. 'id_5' => true,
  333. ),
  334. ),
  335. $metaData['storageData'],
  336. 'Storage data not correctly updated.'
  337. );
  338. // Make sure items have been deleted from disc
  339. $this->assertEquals(
  340. 3, // id_1, id_5 and id_6
  341. count( glob( "$tmpDir/*" ) ),
  342. 'Number of items on disc incorrect.'
  343. );
  344. // Restore existing items to check correct items exist
  345. $this->assertEquals(
  346. 'id_1_content',
  347. $conf->storage->restore( 'id_1' ),
  348. 'Data on disc incorrect.'
  349. );
  350. $this->assertEquals(
  351. 'id_5_content',
  352. $conf->storage->restore( 'id_5' ),
  353. 'Data on disc incorrect.'
  354. );
  355. $this->removeTempDir();
  356. }
  357. public function testStoreNoNewMetaNewItemFreeDelete()
  358. {
  359. // Prepare faked test data
  360. $tmpDir = $this->createTempDir( __FUNCTION__ );
  361. $conf = new ezcCacheStackStorageConfiguration(
  362. 'storage_id_1',
  363. new ezcCacheStorageFileArray(
  364. $tmpDir,
  365. array( 'ttl' => 30 )
  366. ),
  367. 5,
  368. 0.5
  369. );
  370. $now = time();
  371. // Store max number of items
  372. $conf->storage->store( 'id_1', 'id_1_content' );
  373. $conf->storage->store( 'id_2', 'id_2_content' );
  374. $conf->storage->store( 'id_3', 'id_3_content' );
  375. $conf->storage->store( 'id_4', 'id_4_content' );
  376. $conf->storage->store( 'id_5', 'id_5_content' );
  377. // None expired
  378. $meta = new ezcCacheStackLruMetaData();
  379. $meta->setState( array(
  380. 'replacementData' => array(
  381. // Fake access times, not necessarily reflect file mtimes
  382. 'id_1' => ( $now - 10 ),
  383. 'id_2' => ( $now - 15 ), // Throwen out
  384. 'id_3' => ( $now - 12 ), // Throwen out
  385. 'id_4' => ( $now - 9 ),
  386. 'id_5' => ( $now - 13 ), // Throwen out, but kept for other storage
  387. ),
  388. 'storageData' => array(
  389. 'storage_id_1' => array(
  390. 'id_1' => true,
  391. 'id_2' => true,
  392. 'id_3' => true,
  393. 'id_4' => true,
  394. 'id_5' => true,
  395. ),
  396. 'storage_id_42' => array(
  397. 'id_1' => true,
  398. 'id_5' => true,
  399. ),
  400. ),
  401. ) );
  402. // Perform actual action
  403. ezcCacheStackLruReplacementStrategy::store(
  404. $conf,
  405. $meta,
  406. 'id_6',
  407. 'id_6_content'
  408. );
  409. $metaData = $meta->getState();
  410. // Assert correct behaviour
  411. // Data has actually been stored
  412. $this->assertEquals(
  413. 'id_6_content',
  414. $conf->storage->restore( 'id_6' ),
  415. 'Data not stored correctly.'
  416. );
  417. // Time stamp has been stored correctly
  418. $this->assertGreaterThanOrEqual(
  419. $now,
  420. $metaData['replacementData']['id_6'],
  421. 'Meta data entry not created correctly.'
  422. );
  423. // Storage has been saved correctly
  424. $this->assertEquals(
  425. array(
  426. 'id_1' => true,
  427. 'id_4' => true,
  428. 'id_6' => true,
  429. ),
  430. $metaData['storageData']['storage_id_1'],
  431. 'Storage meta data not inserted correctly.'
  432. );
  433. // Remove stored item data from meta data again to comfortably assert
  434. // removal operations
  435. unset( $metaData['replacementData']['id_6'] );
  436. // LRU data correctly updated
  437. $this->assertEquals(
  438. // Note the sorting
  439. array(
  440. 'id_5' => ( $now - 13 ), // Throwen out, but kept for other storage
  441. 'id_1' => ( $now - 10 ),
  442. 'id_4' => ( $now - 9 ),
  443. ),
  444. $metaData['replacementData'],
  445. 'Meta data entries not correctly updated.'
  446. );
  447. // Storage data correctly updated
  448. $this->assertEquals(
  449. array(
  450. 'storage_id_1' => array(
  451. 'id_1' => true,
  452. 'id_4' => true,
  453. 'id_6' => true,
  454. ),
  455. 'storage_id_42' => array(
  456. 'id_1' => true,
  457. 'id_5' => true,
  458. ),
  459. ),
  460. $metaData['storageData'],
  461. 'Storage data not correctly updated.'
  462. );
  463. // Make sure items have been deleted from disc
  464. $this->assertEquals(
  465. // id_1, id_4 and id_6
  466. 3,
  467. count( glob( "$tmpDir/*" ) ),
  468. 'Number of items on disc incorrect.'
  469. );
  470. // Restore existing items to check correct items exist
  471. $this->assertEquals(
  472. 'id_1_content',
  473. $conf->storage->restore( 'id_1' ),
  474. 'Data on disc incorrect.'
  475. );
  476. $this->assertEquals(
  477. 'id_4_content',
  478. $conf->storage->restore( 'id_4' ),
  479. 'Data on disc incorrect.'
  480. );
  481. $this->removeTempDir();
  482. }
  483. public function testStoreNoNewMetaNewItemFreePurgeWithAttributes()
  484. {
  485. // Prepare faked test data
  486. $tmpDir = $this->createTempDir( __FUNCTION__ );
  487. $conf = new ezcCacheStackStorageConfiguration(
  488. 'storage_id_1',
  489. new ezcCacheStorageFileArray(
  490. $tmpDir,
  491. array( 'ttl' => 30 )
  492. ),
  493. 5,
  494. 0.5
  495. );
  496. $now = time();
  497. // Store max number of items
  498. $conf->storage->store( 'id_1', 'id_1_content', array( 'lang' => 'de', 'section' => 'news' ) );
  499. $conf->storage->store( 'id_2', 'id_2_content', array( 'lang' => 'en', 'section' => 'news' ) );
  500. $conf->storage->store( 'id_3', 'id_3_content', array( 'lang' => 'no', 'section' => 'news' ) );
  501. $conf->storage->store( 'id_4', 'id_4_content', array( 'lang' => 'en', 'section' => 'news' ) );
  502. $conf->storage->store( 'id_5', 'id_5_content', array( 'lang' => 'en', 'section' => 'news' ) );
  503. // Expire 3 items
  504. touch(
  505. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_2', array( 'lang' => 'en', 'section' => 'news' ) ),
  506. ( $now - 40 ),
  507. ( $now - 40 )
  508. );
  509. touch(
  510. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_3', array( 'lang' => 'no', 'section' => 'news' ) ),
  511. ( $now - 40 ),
  512. ( $now - 40 )
  513. );
  514. touch(
  515. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_4', array( 'lang' => 'en', 'section' => 'news' ) ),
  516. ( $now - 40 ),
  517. ( $now - 40 )
  518. );
  519. $meta = new ezcCacheStackLruMetaData();
  520. $meta->setState( array(
  521. 'replacementData' => array(
  522. // Fake access times, not necessarily reflect file mtimes
  523. 'id_1' => ( $now - 10 ),
  524. 'id_2' => ( $now - 40 ), // Expired
  525. 'id_3' => ( $now - 40 ), // Expired
  526. 'id_4' => ( $now - 40 ), // Expired
  527. 'id_5' => ( $now - 13 ),
  528. ),
  529. 'storageData' => array(
  530. 'storage_id_1' => array(
  531. 'id_1' => true,
  532. 'id_2' => true,
  533. 'id_3' => true,
  534. 'id_4' => true,
  535. 'id_5' => true,
  536. ),
  537. 'storage_id_42' => array(
  538. 'id_2' => true,
  539. 'id_5' => true,
  540. ),
  541. ),
  542. ) );
  543. // Perform actual action
  544. ezcCacheStackLruReplacementStrategy::store(
  545. $conf,
  546. $meta,
  547. 'id_6',
  548. 'id_6_content',
  549. array( 'lang' => 'de', 'section' => 'news' )
  550. );
  551. $metaData = $meta->getState();
  552. // Assert correct behaviour
  553. // Data has actually been stored
  554. $this->assertEquals(
  555. 'id_6_content',
  556. $conf->storage->restore( 'id_6', null, true ),
  557. 'Data not stored correctly.'
  558. );
  559. // Time stamp has been stored correctly
  560. $this->assertGreaterThanOrEqual(
  561. $now,
  562. $metaData['replacementData']['id_6'],
  563. 'Meta data entry not created correctly.'
  564. );
  565. // Storage has been saved correctly
  566. $this->assertEquals(
  567. array(
  568. 'id_1' => true,
  569. 'id_5' => true,
  570. 'id_6' => true,
  571. ),
  572. $metaData['storageData']['storage_id_1'],
  573. 'Storage meta data not inserted correctly.'
  574. );
  575. // Remove stored item data from meta data again for comfortability
  576. unset( $metaData['replacementData']['id_6'] );
  577. // LRU data correctly updated
  578. $this->assertEquals(
  579. // Note the sorting was not performed!
  580. array(
  581. 'id_1' => ( $now - 10 ),
  582. 'id_2' => ( $now - 40 ),
  583. 'id_5' => ( $now - 13 ),
  584. ),
  585. $metaData['replacementData'],
  586. 'Meta data entries not correctly updated.'
  587. );
  588. // Storage data correctly updated
  589. $this->assertEquals(
  590. array(
  591. 'storage_id_1' => array(
  592. 'id_1' => true,
  593. 'id_5' => true,
  594. 'id_6' => true,
  595. ),
  596. 'storage_id_42' => array(
  597. 'id_2' => true,
  598. 'id_5' => true,
  599. ),
  600. ),
  601. $metaData['storageData'],
  602. 'Storage data not correctly updated.'
  603. );
  604. // Make sure items have been deleted from disc
  605. $this->assertEquals(
  606. 3, // id_1, id_5 and id_6
  607. count( glob( "$tmpDir/*" ) ),
  608. 'Number of items on disc incorrect.'
  609. );
  610. // Restore existing items to check correct items exist
  611. $this->assertEquals(
  612. 'id_1_content',
  613. $conf->storage->restore( 'id_1', null, true ),
  614. 'Data on disc incorrect.'
  615. );
  616. $this->assertEquals(
  617. 'id_5_content',
  618. $conf->storage->restore( 'id_5', null, true ),
  619. 'Data on disc incorrect.'
  620. );
  621. $this->removeTempDir();
  622. }
  623. public function testStoreNoNewMetaNewItemFreeDeleteWithAttributes()
  624. {
  625. // Prepare faked test data
  626. $tmpDir = $this->createTempDir( __FUNCTION__ );
  627. $conf = new ezcCacheStackStorageConfiguration(
  628. 'storage_id_1',
  629. new ezcCacheStorageFileArray(
  630. $tmpDir,
  631. array( 'ttl' => 30 )
  632. ),
  633. 5,
  634. 0.5
  635. );
  636. $now = time();
  637. // Store max number of items
  638. $conf->storage->store( 'id_1', 'id_1_content', array( 'lang' => 'de', 'section' => 'news' ) );
  639. $conf->storage->store( 'id_2', 'id_2_content', array( 'lang' => 'en', 'section' => 'news' ) );
  640. $conf->storage->store( 'id_3', 'id_3_content', array( 'lang' => 'no', 'section' => 'news' ) );
  641. $conf->storage->store( 'id_4', 'id_4_content', array( 'lang' => 'en', 'section' => 'news' ) );
  642. $conf->storage->store( 'id_5', 'id_5_content', array( 'lang' => 'en', 'section' => 'news' ) );
  643. // None expired
  644. $meta = new ezcCacheStackLruMetaData();
  645. $meta->setState( array(
  646. 'replacementData' => array(
  647. // Fake access times, not necessarily reflect file mtimes
  648. 'id_1' => ( $now - 10 ),
  649. 'id_2' => ( $now - 15 ), // Throwen out
  650. 'id_3' => ( $now - 12 ), // Throwen out
  651. 'id_4' => ( $now - 9 ),
  652. 'id_5' => ( $now - 13 ), // Throwen out, but kept for other storage
  653. ),
  654. 'storageData' => array(
  655. 'storage_id_1' => array(
  656. 'id_1' => true,
  657. 'id_2' => true,
  658. 'id_3' => true,
  659. 'id_4' => true,
  660. 'id_5' => true,
  661. ),
  662. 'storage_id_42' => array(
  663. 'id_1' => true,
  664. 'id_5' => true,
  665. ),
  666. ),
  667. ) );
  668. // Perform actual action
  669. ezcCacheStackLruReplacementStrategy::store(
  670. $conf,
  671. $meta,
  672. 'id_6',
  673. 'id_6_content',
  674. array( 'lang' => 'de', 'section' => 'news' )
  675. );
  676. $metaData = $meta->getState();
  677. // Assert correct behaviour
  678. // Data has actually been stored
  679. $this->assertEquals(
  680. 'id_6_content',
  681. $conf->storage->restore( 'id_6', null, true ),
  682. 'Data not stored correctly.'
  683. );
  684. // Time stamp has been stored correctly
  685. $this->assertGreaterThanOrEqual(
  686. $now,
  687. $metaData['replacementData']['id_6'],
  688. 'Meta data entry not created correctly.'
  689. );
  690. // Storage has been saved correctly
  691. $this->assertEquals(
  692. array(
  693. 'id_1' => true,
  694. 'id_4' => true,
  695. 'id_6' => true,
  696. ),
  697. $metaData['storageData']['storage_id_1'],
  698. 'Storage meta data not inserted correctly.'
  699. );
  700. // Remove stored item data from meta data again to comfortably assert
  701. // removal operations
  702. unset( $metaData['replacementData']['id_6'] );
  703. // LRU data correctly updated
  704. $this->assertEquals(
  705. // Note the sorting
  706. array(
  707. 'id_5' => ( $now - 13 ), // Throwen out, but kept for other storage
  708. 'id_1' => ( $now - 10 ),
  709. 'id_4' => ( $now - 9 ),
  710. ),
  711. $metaData['replacementData'],
  712. 'Meta data entries not correctly updated.'
  713. );
  714. // Storage data correctly updated
  715. $this->assertEquals(
  716. array(
  717. 'storage_id_1' => array(
  718. 'id_1' => true,
  719. 'id_4' => true,
  720. 'id_6' => true,
  721. ),
  722. 'storage_id_42' => array(
  723. 'id_1' => true,
  724. 'id_5' => true,
  725. ),
  726. ),
  727. $metaData['storageData'],
  728. 'Storage data not correctly updated.'
  729. );
  730. // Make sure items have been deleted from disc
  731. $this->assertEquals(
  732. // id_1, id_4 and id_6
  733. 3,
  734. count( glob( "$tmpDir/*" ) ),
  735. 'Number of items on disc incorrect.'
  736. );
  737. // Restore existing items to check correct items exist
  738. $this->assertEquals(
  739. 'id_1_content',
  740. $conf->storage->restore( 'id_1', null, true ),
  741. 'Data on disc incorrect.'
  742. );
  743. $this->assertEquals(
  744. 'id_4_content',
  745. $conf->storage->restore( 'id_4', null, true ),
  746. 'Data on disc incorrect.'
  747. );
  748. $this->removeTempDir();
  749. }
  750. public function testStoreNoNewMetaNewItemFreePurgeDelete()
  751. {
  752. // Prepare faked test data
  753. $tmpDir = $this->createTempDir( __FUNCTION__ );
  754. $conf = new ezcCacheStackStorageConfiguration(
  755. 'storage_id_1',
  756. new ezcCacheStorageFileArray(
  757. $tmpDir,
  758. array( 'ttl' => 30 )
  759. ),
  760. 5,
  761. 0.5
  762. );
  763. $now = time();
  764. // Store max number of items
  765. $conf->storage->store( 'id_1', 'id_1_content' );
  766. $conf->storage->store( 'id_2', 'id_2_content' );
  767. $conf->storage->store( 'id_3', 'id_3_content' );
  768. $conf->storage->store( 'id_4', 'id_4_content' );
  769. $conf->storage->store( 'id_5', 'id_5_content' );
  770. // id_2 expired
  771. touch(
  772. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_2' ),
  773. ( $now - 40 ),
  774. ( $now - 40 )
  775. );
  776. $meta = new ezcCacheStackLruMetaData();
  777. $meta->setState( array(
  778. 'replacementData' => array(
  779. // Fake access times, not necessarily reflect file mtimes
  780. 'id_1' => ( $now - 10 ),
  781. 'id_2' => ( $now - 40 ), // Expired
  782. 'id_3' => ( $now - 12 ), // Throwen out
  783. 'id_4' => ( $now - 9 ),
  784. 'id_5' => ( $now - 13 ), // Throwen out but kept for other storage
  785. ),
  786. 'storageData' => array(
  787. 'storage_id_1' => array(
  788. 'id_1' => true,
  789. 'id_2' => true,
  790. 'id_3' => true,
  791. 'id_4' => true,
  792. 'id_5' => true,
  793. ),
  794. 'storage_id_42' => array(
  795. 'id_1' => true,
  796. 'id_5' => true,
  797. )
  798. ),
  799. ) );
  800. // Perform actual action
  801. ezcCacheStackLruReplacementStrategy::store(
  802. $conf,
  803. $meta,
  804. 'id_6',
  805. 'id_6_content'
  806. );
  807. $metaData = $meta->getState();
  808. // Assert correct behaviour
  809. // Data has actually been stored
  810. $this->assertEquals(
  811. 'id_6_content',
  812. $conf->storage->restore( 'id_6' ),
  813. 'Data not stored correctly.'
  814. );
  815. // Time stamp has been stored correctly
  816. $this->assertGreaterThanOrEqual(
  817. $now,
  818. $metaData['replacementData']['id_6'],
  819. 'Meta data entry not created correctly.'
  820. );
  821. // Storage has been saved correctly
  822. $this->assertEquals(
  823. array(
  824. 'storage_id_1' => array(
  825. 'id_1' => true,
  826. 'id_4' => true,
  827. 'id_6' => true,
  828. ),
  829. 'storage_id_42' => array(
  830. 'id_1' => true,
  831. 'id_5' => true,
  832. )
  833. ),
  834. $metaData['storageData'],
  835. 'Storage meta data not inserted correctly.'
  836. );
  837. // Remove stored item data from meta data again to comfortably assert
  838. // removal operations
  839. unset( $metaData['replacementData']['id_6'] );
  840. // LRU data correctly updated
  841. $this->assertEquals(
  842. // Note the sorting
  843. array(
  844. 'id_5' => ( $now - 13 ), // Throwen out but kept for other storage
  845. 'id_1' => ( $now - 10 ),
  846. 'id_4' => ( $now - 9 ),
  847. ),
  848. $metaData['replacementData'],
  849. 'Meta data entries not correctly updated.'
  850. );
  851. // Make sure items have been deleted from disc
  852. $this->assertEquals(
  853. // id_1, id_4 and id_6
  854. 3,
  855. count( glob( "$tmpDir/*" ) ),
  856. 'Number of items on disc incorrect.'
  857. );
  858. // Restore existing items to check correct items exist
  859. $this->assertEquals(
  860. 'id_1_content',
  861. $conf->storage->restore( 'id_1' ),
  862. 'Data on disc incorrect.'
  863. );
  864. $this->assertEquals(
  865. 'id_4_content',
  866. $conf->storage->restore( 'id_4' ),
  867. 'Data on disc incorrect.'
  868. );
  869. // Throwen out of this storage
  870. $this->assertFalse(
  871. $conf->storage->restore( 'id_5' ),
  872. 'Data on disc incorrect.'
  873. );
  874. $this->removeTempDir();
  875. }
  876. // This test changes, sine the behaviour as it was tested here was incorrect
  877. public function testStoreNoNewMetaNewItemFreePurgeDeleteComplex()
  878. {
  879. // Prepare faked test data
  880. $tmpDir = $this->createTempDir( __FUNCTION__ );
  881. $conf = new ezcCacheStackStorageConfiguration(
  882. 'storage_id_1',
  883. new ezcCacheStorageFileArray(
  884. $tmpDir,
  885. array( 'ttl' => 30 )
  886. ),
  887. 5,
  888. 0.5
  889. );
  890. $now = time();
  891. // Store max number of items
  892. $conf->storage->store( 'id_1', 'id_1_content' );
  893. $conf->storage->store( 'id_2', 'id_2_content' );
  894. $conf->storage->store( 'id_3', 'id_3_content' );
  895. $conf->storage->store( 'id_4', 'id_4_content' );
  896. // $conf->storage->store( 'id_5', 'id_5_content' );
  897. // id_2 expired
  898. touch(
  899. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_2' ),
  900. ( $now - 40 ),
  901. ( $now - 40 )
  902. );
  903. $meta = new ezcCacheStackLruMetaData();
  904. $meta->setState( array(
  905. 'replacementData' => array(
  906. // Fake access times, not necessarily reflect file mtimes
  907. 'id_1' => ( $now - 10 ),
  908. 'id_2' => ( $now - 40 ), // Expired
  909. 'id_3' => ( $now - 12 ),
  910. 'id_4' => ( $now - 9 ),
  911. 'id_5' => ( $now - 13 ), // Kept, since not in actual storage
  912. ),
  913. 'storageData' => array(
  914. 'storage_id_1' => array(
  915. 'id_1' => true,
  916. 'id_2' => true,
  917. 'id_3' => true,
  918. 'id_4' => true,
  919. ),
  920. 'storage_id_42' => array(
  921. 'id_5' => true,
  922. ),
  923. ),
  924. ) );
  925. // Perform actual action
  926. ezcCacheStackLruReplacementStrategy::store(
  927. $conf,
  928. $meta,
  929. 'id_6',
  930. 'id_6_content'
  931. );
  932. $metaData = $meta->getState();
  933. // Assert correct behaviour
  934. // Data has actually been stored
  935. $this->assertEquals(
  936. 'id_6_content',
  937. $conf->storage->restore( 'id_6' ),
  938. 'Data not stored correctly.'
  939. );
  940. // Time stamp has been stored correctly
  941. $this->assertGreaterThanOrEqual(
  942. $now,
  943. $metaData['replacementData']['id_6'],
  944. 'Meta data entry not created correctly.'
  945. );
  946. // Storage has been saved correctly
  947. $this->assertEquals(
  948. array(
  949. 'storage_id_1' => array(
  950. 'id_1' => true,
  951. 'id_2' => true,
  952. 'id_3' => true,
  953. 'id_4' => true,
  954. 'id_6' => true,
  955. ),
  956. 'storage_id_42' => array(
  957. 'id_5' => true,
  958. ),
  959. ),
  960. $metaData['storageData'],
  961. 'Storage meta data not inserted correctly.'
  962. );
  963. // Remove stored item data from meta data again to comfortably assert
  964. // removal operations
  965. unset( $metaData['replacementData']['id_6'] );
  966. // LRU data correctly updated
  967. $this->assertEquals(
  968. // Note the sorting has not been performed!
  969. array(
  970. 'id_1' => ( $now - 10 ),
  971. 'id_2' => ( $now - 40 ), // Expired
  972. 'id_3' => ( $now - 12 ),
  973. 'id_4' => ( $now - 9 ),
  974. 'id_5' => ( $now - 13 ), // Kept, since not in actual storage
  975. ),
  976. $metaData['replacementData'],
  977. 'Meta data entries not correctly updated.'
  978. );
  979. // Make sure items have been deleted from disc
  980. $this->assertEquals(
  981. // All 4 previous + id_6
  982. 5,
  983. count( glob( "$tmpDir/*" ) ),
  984. 'Number of items on disc incorrect.'
  985. );
  986. $this->removeTempDir();
  987. }
  988. public function testRestoreSuccess()
  989. {
  990. // Prepare faked test data
  991. $tmpDir = $this->createTempDir( __FUNCTION__ );
  992. $conf = new ezcCacheStackStorageConfiguration(
  993. 'storage_id_1',
  994. new ezcCacheStorageFileArray(
  995. $tmpDir,
  996. array( 'ttl' => 30 )
  997. ),
  998. 5,
  999. 0.5
  1000. );
  1001. $now = time();
  1002. // Store max number of items
  1003. $conf->storage->store( 'id_1', 'id_1_content' );
  1004. $meta = new ezcCacheStackLruMetaData();
  1005. $meta->setState( array(
  1006. 'replacementData' => array(
  1007. // Fake access times, not necessarily reflect file mtimes
  1008. 'id_1' => ( $now - 10 ),
  1009. ),
  1010. 'storageData' => array(
  1011. 'storage_id_1' => array(
  1012. 'id_1' => true,
  1013. ),
  1014. 'storage_id_42' => array(
  1015. 'id_1' => true,
  1016. ),
  1017. ),
  1018. ) );
  1019. // Perform actual action
  1020. $item = ezcCacheStackLruReplacementStrategy::restore(
  1021. $conf,
  1022. $meta,
  1023. 'id_1'
  1024. );
  1025. $metaData = $meta->getState();
  1026. // Assert correct behavior
  1027. // Item data correctly restored
  1028. $this->assertEquals(
  1029. 'id_1_content',
  1030. $item,
  1031. 'Item not restored correctly.'
  1032. );
  1033. // Meta data actualized correctly
  1034. $this->assertGreaterThan(
  1035. ( $now - 10 ),
  1036. $metaData['replacementData']['id_1']
  1037. );
  1038. // Storage data kept correctly
  1039. $this->assertEquals(
  1040. array(
  1041. 'storage_id_1' => array(
  1042. 'id_1' => true,
  1043. ),
  1044. 'storage_id_42' => array(
  1045. 'id_1' => true,
  1046. ),
  1047. ),
  1048. $metaData['storageData'],
  1049. 'Storage data not correctly updated.'
  1050. );
  1051. }
  1052. public function testRestoreFailureExpired()
  1053. {
  1054. // Prepare faked test data
  1055. $tmpDir = $this->createTempDir( __FUNCTION__ );
  1056. $conf = new ezcCacheStackStorageConfiguration(
  1057. 'storage_id_1',
  1058. new ezcCacheStorageFileArray(
  1059. $tmpDir,
  1060. array( 'ttl' => 30 )
  1061. ),
  1062. 5,
  1063. 0.5
  1064. );
  1065. $now = time();
  1066. // Store max number of items
  1067. $conf->storage->store( 'id_1', 'id_1_content' );
  1068. // Expire
  1069. touch(
  1070. $tmpDir . '/' . $conf->storage->generateIdentifier( 'id_1' ),
  1071. ( $now - 40 ),
  1072. ( $now - 40 )
  1073. );
  1074. $meta = new ezcCacheStackLruMetaData();
  1075. $meta->setState( array(
  1076. 'replacementData' => array(
  1077. // Fake access times, not necessarily reflect file mtimes
  1078. 'id_1' => ( $now - 40 ),
  1079. ),
  1080. 'storageData' => array(
  1081. 'storage_id_1' => array(
  1082. 'id_1' => true,
  1083. ),
  1084. 'storage_id_42' => array(
  1085. 'id_1' => true,
  1086. ),
  1087. ),
  1088. ) );
  1089. // Perform actual action
  1090. $item = ezcCacheStackLruReplacementStrategy::restore(
  1091. $conf,
  1092. $meta,
  1093. 'id_1'
  1094. );
  1095. $metaData = $meta->getState();
  1096. // Assert correct behavior
  1097. // Item data correctly restored
  1098. $this->assertFalse(
  1099. $item,
  1100. 'Item exists although expired.'
  1101. );
  1102. // Meta data actualized correctly
  1103. $this->assertEquals(
  1104. array(
  1105. // Not removed, since available in other storage
  1106. 'id_1' => ( $now - 40 ),
  1107. ),
  1108. $metaData['replacementData']
  1109. );
  1110. // Storage data kept correctly
  1111. $this->assertEquals(
  1112. array(
  1113. 'storage_id_42' => array(
  1114. 'id_1' => true,
  1115. ),
  1116. ),
  1117. $metaData['storageData'],
  1118. 'Storage data not correctly updated.'
  1119. );
  1120. }
  1121. public function testRestoreFailureNonexistent()
  1122. {
  1123. // Prepare faked test data
  1124. $tmpDir = $this->createTempDir( __FUNCTION__ );
  1125. $conf = new ezcCacheStackStorageConfiguration(
  1126. 'storage_id_1',
  1127. new ezcCacheStorageFileArray(
  1128. $tmpDir,
  1129. array( 'ttl' => 30 )
  1130. ),
  1131. 5,
  1132. 0.5
  1133. );
  1134. $meta = new ezcCacheStackLruMetaData();
  1135. $meta->setState( array(
  1136. 'replacementData' => array(),
  1137. 'storageData' => array(),
  1138. ) );
  1139. // Perform actual action
  1140. $item = ezcCacheStackLruReplacementStrategy::restore(
  1141. $conf,
  1142. $meta,
  1143. 'id_1'
  1144. );
  1145. $metaData = $meta->getState();
  1146. // Assert correct behavior
  1147. // Item data correctly restored
  1148. $this->assertFalse(
  1149. $item,
  1150. 'Item exists although expired.'
  1151. );
  1152. // Meta data actualized correctly
  1153. $this->assertEquals(
  1154. array(),
  1155. $metaData['replacementData']
  1156. );
  1157. // Storage data kept correctly
  1158. $this->assertEquals(
  1159. array(),
  1160. $metaData['storageData'],
  1161. 'Storage data not correctly updated.'
  1162. );
  1163. }
  1164. public function testDeleteSuccess()
  1165. {
  1166. // Prepare faked test data
  1167. $tmpDir = $this->createTempDir( __FUNCTION__ );
  1168. $conf = new ezcCacheStackStorageConfiguration(
  1169. 'storage_id_1',
  1170. new ezcCacheStorageFileArray(
  1171. $tmpDir,
  1172. array( 'ttl' => 30 )
  1173. ),
  1174. 5,
  1175. 0.5
  1176. );
  1177. $now = time();
  1178. // Store max number of items
  1179. $conf->storage->store( 'id_1', 'id_1_content' );
  1180. // Cache location not empty
  1181. $this->assertEquals(
  1182. 1,
  1183. count( glob( "$tmpDir/*" ) ),
  1184. 'Cache location contains unknown items.'
  1185. );
  1186. $meta = new ezcCacheStackLruMetaData();
  1187. $meta->setState( array(
  1188. 'replacementData' => array(
  1189. 'id_1' => $now,
  1190. ),
  1191. 'storageData' => array(
  1192. 'storage_id_1' => array(
  1193. 'id_1' => true,
  1194. ),
  1195. 'storage_id_42' => array(
  1196. 'id_1' => true,
  1197. ),
  1198. ),
  1199. ) );
  1200. // Perform actual action
  1201. $deletedItems = ezcCacheStackLruReplacementStrategy::delete(
  1202. $conf,
  1203. $meta,
  1204. 'id_1'
  1205. );
  1206. $metaData = $meta->getState();
  1207. // Assert correct behavior
  1208. // Item data correctly restored
  1209. $this->assertEquals(
  1210. array( 'id_1' ),
  1211. $deletedItems,
  1212. 'Item not indicated to be delted.'
  1213. );
  1214. // Meta data actualized correctly
  1215. $this->assertEquals(
  1216. array(
  1217. 'id_1' => $now,
  1218. ),
  1219. $metaData['replacementData']
  1220. );
  1221. // Storage data kept correctly
  1222. $this->assertEquals(
  1223. array(
  1224. 'storage_id_42' => array(
  1225. 'id_1' => true,
  1226. ),
  1227. ),
  1228. $metaData['storageData'],
  1229. 'Storage data not correctly updated.'
  1230. );
  1231. // Cache location empty
  1232. $this->assertEquals(
  1233. 0,
  1234. count( glob( "$tmpDir/*" ) ),
  1235. 'Cache location contains unknown items.'
  1236. );
  1237. }
  1238. public function testDeleteNonexistent()
  1239. {
  1240. // Prepare faked test data
  1241. $tmpDir = $this->createTempDir( __FUNCTION__ );
  1242. $conf = new ezcCacheStackStorageConfiguration(
  1243. 'storage_id_1',
  1244. new ezcCacheStorageFileArray(
  1245. $tmpDir,
  1246. array( 'ttl' => 30 )
  1247. ),
  1248. 5,
  1249. 0.5
  1250. );
  1251. $now = time();
  1252. $meta = new ezcCacheStackLruMetaData();
  1253. $meta->setState( array(
  1254. 'replacementData' => array(
  1255. 'id_1' => ( $now - 40 ),
  1256. ),
  1257. 'storageData' => array(
  1258. 'storage_id_42' => array(
  1259. 'id_1' => true,
  1260. ),
  1261. ),
  1262. ) );
  1263. // Perform actual action
  1264. $deletedItems = ezcCacheStackLruReplacementStrategy::delete(
  1265. $conf,
  1266. $meta,
  1267. 'id_1'
  1268. );
  1269. $metaData = $meta->getState();
  1270. // Assert correct behavior
  1271. // Item data correctly restored
  1272. $this->assertEquals(
  1273. array(),
  1274. $deletedItems,
  1275. 'Item not indicated to be delted.'
  1276. );
  1277. // Meta data actualized correctly
  1278. $this->assertEquals(
  1279. array(
  1280. 'id_1' => ( $now - 40 ),
  1281. ),
  1282. $metaData['replacementData']
  1283. );
  1284. // Storage data kept correctly
  1285. $this->assertEquals(
  1286. array(
  1287. 'storage_id_42' => array(
  1288. 'id_1' => true,
  1289. ),
  1290. ),
  1291. $metaData['storageData'],
  1292. 'Storage data not correctly updated.'
  1293. );
  1294. // Cache location empty
  1295. $this->assertEquals(
  1296. 0,
  1297. count( glob( "$tmpDir/*" ) ),
  1298. 'Cache location contains unknown items.'
  1299. );
  1300. }
  1301. public function testDeleteSuccessSearch()
  1302. {
  1303. // Prepare faked test data
  1304. $tmpDir = $this->createTempDir( __FUNCTION__ );
  1305. $conf = new ezcCacheStackStorageConfiguration(
  1306. 'storage_id_1',
  1307. new ezcCacheStorageFileArray(
  1308. $tmpDir,
  1309. array( 'ttl' => 30 )
  1310. ),
  1311. 5,
  1312. 0.5
  1313. );
  1314. $now = time();
  1315. // Store max number of items
  1316. $conf->storage->store( 'id_1', 'id_1_content', array( 'lang' => 'en' ) );
  1317. $conf->storage->store( 'id_2', 'id_2_content' );
  1318. $conf->storage->store( 'id_3', 'id_3_content', array( 'lang' => 'en' ) );
  1319. // Cache location not empty
  1320. $this->assertEquals(
  1321. 3,
  1322. count( glob( "$tmpDir/*" ) ),
  1323. 'Cache location contains unknown items.'
  1324. );
  1325. $meta = new ezcCacheStackLruMetaData();
  1326. $meta->setState( array(
  1327. 'replacementData' => array(
  1328. 'id_1' => $now,
  1329. 'id_2' => $now,
  1330. 'id_3' => $now,
  1331. ),
  1332. 'storageData' => array(
  1333. 'storage_id_1' => array(
  1334. 'id_1' => true,
  1335. 'id_2' => true,
  1336. 'id_3' => true,
  1337. ),
  1338. 'storage_id_42' => array(
  1339. 'id_1' => true,
  1340. ),
  1341. ),
  1342. ) );
  1343. // Perform actual action
  1344. $deletedItems = ezcCacheStackLruReplacementStrategy::delete(
  1345. $conf,
  1346. $meta,
  1347. null,
  1348. array( 'lang' => 'en' ),
  1349. true
  1350. );
  1351. $metaData = $meta->getState();
  1352. // Assert correct behavior
  1353. // Item data correctly restored
  1354. $this->assertEquals(
  1355. array( 'id_1', 'id_3' ),
  1356. $deletedItems,
  1357. 'Item not indicated to be delted.'
  1358. );
  1359. // Meta data actualized correctly
  1360. $this->assertEquals(
  1361. array(
  1362. 'id_1' => $now,
  1363. 'id_2' => $now,
  1364. ),
  1365. $metaData['replacementData'],
  1366. "Meta data not actualized correctly."
  1367. );
  1368. // Storage data kept correctly
  1369. $this->assertEquals(
  1370. array(
  1371. 'storage_id_1' => array(
  1372. 'id_2' => true,
  1373. ),
  1374. 'storage_id_42' => array(
  1375. 'id_1' => true,
  1376. ),
  1377. ),
  1378. $metaData['storageData'],
  1379. 'Storage data not correctly updated.'
  1380. );
  1381. // Cache location empty
  1382. $this->assertEquals(
  1383. 1,
  1384. count( glob( "$tmpDir/*" ) ),
  1385. 'Cache location contains incorrect number of items.'
  1386. );
  1387. }
  1388. }
  1389. ?>