PageRenderTime 149ms CodeModel.GetById 72ms RepoModel.GetById 1ms app.codeStats 0ms

/Cache/tests/replacement_strategy_lfu_test.php

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