88namespace Test \Cache ;
99
1010use OC \Files \Storage \Local ;
11+ use OC \Files \Storage \Temporary ;
1112use OCP \Files \Mount \IMountManager ;
13+ use OCP \Lock \ILockingProvider ;
1214use Test \Traits \UserTrait ;
1315
1416/**
@@ -33,10 +35,6 @@ class FileCacheTest extends TestCache {
3335 * @var \OC\Files\Storage\Storage
3436 * */
3537 private $ storage ;
36- /**
37- * @var \OC\Files\View
38- * */
39- private $ rootView ;
4038
4139 public function skip () {
4240 //$this->skipUnless(OC_User::isLoggedIn());
@@ -58,12 +56,8 @@ protected function setUp(): void {
5856 $ manager = \OC ::$ server ->get (IMountManager::class);
5957 $ manager ->removeMount ('/test ' );
6058
61- $ storage = new \OC \Files \Storage \Temporary ([]);
62- \OC \Files \Filesystem::mount ($ storage , [], '/test/cache ' );
63-
64- //set up the users dir
65- $ this ->rootView = new \OC \Files \View ('' );
66- $ this ->rootView ->mkdir ('/test ' );
59+ $ this ->storage = new \OC \Files \Storage \Temporary ([]);
60+ \OC \Files \Filesystem::mount ($ this ->storage , [], '/test/cache ' );
6761
6862 $ this ->instance = new \OC \Cache \File ();
6963
@@ -86,71 +80,45 @@ protected function tearDown(): void {
8680 parent ::tearDown ();
8781 }
8882
89- private function setupMockStorage () {
90- $ mockStorage = $ this ->getMockBuilder (Local::class)
91- ->setMethods (['filemtime ' , 'unlink ' ])
92- ->setConstructorArgs ([['datadir ' => \OC ::$ server ->getTempManager ()->getTemporaryFolder ()]])
93- ->getMock ();
94-
95- \OC \Files \Filesystem::mount ($ mockStorage , [], '/test/cache ' );
96-
97- return $ mockStorage ;
98- }
99-
10083 public function testGarbageCollectOldKeys (): void {
101- $ mockStorage = $ this ->setupMockStorage ( );
84+ $ this ->instance -> set ( ' key1 ' , ' value1 ' );
10285
103- $ mockStorage ->expects ($ this ->atLeastOnce ())
104- ->method ('filemtime ' )
105- ->willReturn (100 );
106- $ mockStorage ->expects ($ this ->once ())
107- ->method ('unlink ' )
108- ->with ('key1 ' )
109- ->willReturn (true );
86+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
87+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => 100 ]);
11088
111- $ this ->instance ->set ('key1 ' , 'value1 ' );
11289 $ this ->instance ->gc ();
90+ $ this ->assertFalse ($ this ->storage ->file_exists ('key1 ' ));
11391 }
11492
11593 public function testGarbageCollectLeaveRecentKeys (): void {
116- $ mockStorage = $ this ->setupMockStorage ();
117-
118- $ mockStorage ->expects ($ this ->atLeastOnce ())
119- ->method ('filemtime ' )
120- ->willReturn (time () + 3600 );
121- $ mockStorage ->expects ($ this ->never ())
122- ->method ('unlink ' )
123- ->with ('key1 ' );
12494 $ this ->instance ->set ('key1 ' , 'value1 ' );
95+
96+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
97+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => time () + 3600 ]);
98+
12599 $ this ->instance ->gc ();
126- }
127100
128- public function lockExceptionProvider () {
129- return [
130- [new \OCP \Lock \LockedException ('key1 ' )],
131- [new \OCP \Files \LockNotAcquiredException ('key1 ' , 1 )],
132- ];
101+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
133102 }
134103
135- /**
136- * @dataProvider lockExceptionProvider
137- */
138- public function testGarbageCollectIgnoreLockedKeys ($ testException ): void {
139- $ mockStorage = $ this ->setupMockStorage ();
140-
141- $ mockStorage ->expects ($ this ->atLeastOnce ())
142- ->method ('filemtime ' )
143- ->willReturn (100 );
144- $ mockStorage ->expects ($ this ->atLeastOnce ())
145- ->method ('unlink ' )
146- ->will ($ this ->onConsecutiveCalls (
147- $ this ->throwException ($ testException ),
148- $ this ->returnValue (true )
149- ));
104+ public function testGarbageCollectIgnoreLockedKeys (): void {
105+ $ lockingProvider = \OC ::$ server ->get (ILockingProvider::class);
150106
151107 $ this ->instance ->set ('key1 ' , 'value1 ' );
108+ $ this ->storage ->getCache ()->put ('key1 ' , ['mtime ' => 100 ]);
152109 $ this ->instance ->set ('key2 ' , 'value2 ' );
110+ $ this ->storage ->getCache ()->put ('key2 ' , ['mtime ' => 100 ]);
111+ $ this ->storage ->acquireLock ('key2 ' , ILockingProvider::LOCK_SHARED , $ lockingProvider );
112+
113+ $ this ->assertTrue ($ this ->storage ->file_exists ('key1 ' ));
114+ $ this ->assertTrue ($ this ->storage ->file_exists ('key2 ' ));
153115
154116 $ this ->instance ->gc ();
117+
118+ $ this ->storage ->releaseLock ('key2 ' , ILockingProvider::LOCK_SHARED , $ lockingProvider );
119+
120+ $ this ->assertFalse ($ this ->storage ->file_exists ('key1 ' ));
121+ $ this ->assertFalse ($ this ->storage ->file_exists ('key2 ' ));
122+
155123 }
156124}
0 commit comments