View Javadoc
1   /*
2    * Copyright (C) 2009, Google Inc.
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   */
43  
44  package org.eclipse.jgit.lib;
45  
46  import static org.hamcrest.CoreMatchers.hasItem;
47  import static org.junit.Assert.assertEquals;
48  import static org.junit.Assert.assertFalse;
49  import static org.junit.Assert.assertNotNull;
50  import static org.junit.Assert.assertNotSame;
51  import static org.junit.Assert.assertSame;
52  import static org.junit.Assert.assertThat;
53  import static org.junit.Assert.assertTrue;
54  import static org.junit.Assert.fail;
55  
56  import java.io.File;
57  import java.io.IOException;
58  
59  import org.eclipse.jgit.errors.RepositoryNotFoundException;
60  import org.eclipse.jgit.junit.RepositoryTestCase;
61  import org.eclipse.jgit.lib.RepositoryCache.FileKey;
62  import org.junit.Test;
63  
64  public class RepositoryCacheTest extends RepositoryTestCase {
65  	@Test
66  	public void testNonBareFileKey() throws IOException {
67  		File gitdir = db.getDirectory();
68  		File parent = gitdir.getParentFile();
69  		File other = new File(parent, "notagit");
70  		assertEqualsFile(gitdir, FileKey.exact(gitdir, db.getFS()).getFile());
71  		assertEqualsFile(parent, FileKey.exact(parent, db.getFS()).getFile());
72  		assertEqualsFile(other, FileKey.exact(other, db.getFS()).getFile());
73  
74  		assertEqualsFile(gitdir, FileKey.lenient(gitdir, db.getFS()).getFile());
75  		assertEqualsFile(gitdir, FileKey.lenient(parent, db.getFS()).getFile());
76  		assertEqualsFile(other, FileKey.lenient(other, db.getFS()).getFile());
77  	}
78  
79  	@Test
80  	public void testBareFileKey() throws IOException {
81  		Repository bare = createBareRepository();
82  		File gitdir = bare.getDirectory();
83  		File parent = gitdir.getParentFile();
84  		String name = gitdir.getName();
85  		assertTrue(name.endsWith(".git"));
86  		name = name.substring(0, name.length() - 4);
87  
88  		assertEqualsFile(gitdir, FileKey.exact(gitdir, db.getFS()).getFile());
89  
90  		assertEqualsFile(gitdir, FileKey.lenient(gitdir, db.getFS()).getFile());
91  		assertEqualsFile(gitdir,
92  				FileKey.lenient(new File(parent, name), db.getFS()).getFile());
93  	}
94  
95  	@Test
96  	public void testFileKeyOpenExisting() throws IOException {
97  		try (Repository r = new FileKey(db.getDirectory(), db.getFS())
98  				.open(true)) {
99  			assertNotNull(r);
100 			assertEqualsFile(db.getDirectory(), r.getDirectory());
101 		}
102 
103 		try (Repository r = new FileKey(db.getDirectory(), db.getFS())
104 				.open(false)) {
105 			assertNotNull(r);
106 			assertEqualsFile(db.getDirectory(), r.getDirectory());
107 		}
108 	}
109 
110 	@Test
111 	public void testFileKeyOpenNew() throws IOException {
112 		File gitdir;
113 		try (Repository n = createRepository(true)) {
114 			gitdir = n.getDirectory();
115 		}
116 		recursiveDelete(gitdir);
117 		assertFalse(gitdir.exists());
118 
119 		try {
120 			new FileKey(gitdir, db.getFS()).open(true);
121 			fail("incorrectly opened a non existant repository");
122 		} catch (RepositoryNotFoundException e) {
123 			assertEquals("repository not found: " + gitdir.getCanonicalPath(),
124 					e.getMessage());
125 		}
126 
127 		final Repository o = new FileKey(gitdir, db.getFS()).open(false);
128 		assertNotNull(o);
129 		assertEqualsFile(gitdir, o.getDirectory());
130 		assertFalse(gitdir.exists());
131 	}
132 
133 	@Test
134 	public void testCacheRegisterOpen() throws Exception {
135 		final File dir = db.getDirectory();
136 		RepositoryCache.register(db);
137 		assertSame(db, RepositoryCache.open(FileKey.exact(dir, db.getFS())));
138 
139 		assertEquals(".git", dir.getName());
140 		final File parent = dir.getParentFile();
141 		assertSame(db, RepositoryCache.open(FileKey.lenient(parent, db.getFS())));
142 	}
143 
144 	@Test
145 	public void testCacheOpen() throws Exception {
146 		final FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
147 		@SuppressWarnings("resource") // We are testing the close() method
148 		final Repository d2 = RepositoryCache.open(loc);
149 		assertNotSame(db, d2);
150 		assertSame(d2, RepositoryCache.open(FileKey.exact(loc.getFile(), db.getFS())));
151 		d2.close();
152 		d2.close();
153 	}
154 
155 	@Test
156 	public void testGetRegisteredWhenEmpty() {
157 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
158 	}
159 
160 	@Test
161 	public void testGetRegistered() {
162 		RepositoryCache.register(db);
163 
164 		assertThat(RepositoryCache.getRegisteredKeys(),
165 				hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
166 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
167 	}
168 
169 	@Test
170 	public void testUnregister() {
171 		RepositoryCache.register(db);
172 		RepositoryCache
173 				.unregister(FileKey.exact(db.getDirectory(), db.getFS()));
174 
175 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
176 	}
177 
178 	@Test
179 	public void testRepositoryUsageCount() throws Exception {
180 		FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
181 		@SuppressWarnings("resource") // We are testing the close() method
182 		Repository d2 = RepositoryCache.open(loc);
183 		assertEquals(1, d2.useCnt.get());
184 		RepositoryCache.open(FileKey.exact(loc.getFile(), db.getFS()));
185 		assertEquals(2, d2.useCnt.get());
186 		d2.close();
187 		assertEquals(1, d2.useCnt.get());
188 		d2.close();
189 		assertEquals(0, d2.useCnt.get());
190 	}
191 
192 	@Test
193 	public void testRepositoryUsageCountWithRegisteredRepository()
194 			throws IOException {
195 		@SuppressWarnings({"resource", "deprecation"}) // We are testing the close() method
196 		Repository repo = createRepository(false, false);
197 		assertEquals(1, repo.useCnt.get());
198 		RepositoryCache.register(repo);
199 		assertEquals(1, repo.useCnt.get());
200 		repo.close();
201 		assertEquals(0, repo.useCnt.get());
202 	}
203 
204 	@Test
205 	public void testRepositoryNotUnregisteringWhenClosing() throws Exception {
206 		FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
207 		@SuppressWarnings("resource") // We are testing the close() method
208 		Repository d2 = RepositoryCache.open(loc);
209 		assertEquals(1, d2.useCnt.get());
210 		assertThat(RepositoryCache.getRegisteredKeys(),
211 				hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
212 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
213 		d2.close();
214 		assertEquals(0, d2.useCnt.get());
215 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
216 		assertTrue(RepositoryCache.isCached(d2));
217 	}
218 
219 	@Test
220 	public void testRepositoryUnregisteringWhenExpiredAndUsageCountNegative()
221 			throws Exception {
222 		@SuppressWarnings("resource") // We are testing the close() method
223 		Repository repoA = createBareRepository();
224 		RepositoryCache.register(repoA);
225 
226 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
227 		assertTrue(RepositoryCache.isCached(repoA));
228 
229 		// close the repo twice to make usage count negative
230 		repoA.close();
231 		repoA.close();
232 		// fake that repoA was closed more than 1 hour ago (default expiration
233 		// time)
234 		repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000);
235 
236 		RepositoryCache.clearExpired();
237 
238 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
239 	}
240 
241 	@Test
242 	public void testRepositoryUnregisteringWhenExpired() throws Exception {
243 		@SuppressWarnings({"resource", "deprecation"}) // We are testing the close() method
244 		Repository repoA = createRepository(true, false);
245 		@SuppressWarnings({"resource", "deprecation"}) // We are testing the close() method
246 		Repository repoB = createRepository(true, false);
247 		Repository repoC = createBareRepository();
248 		RepositoryCache.register(repoA);
249 		RepositoryCache.register(repoB);
250 		RepositoryCache.register(repoC);
251 
252 		assertEquals(3, RepositoryCache.getRegisteredKeys().size());
253 		assertTrue(RepositoryCache.isCached(repoA));
254 		assertTrue(RepositoryCache.isCached(repoB));
255 		assertTrue(RepositoryCache.isCached(repoC));
256 
257 		// fake that repoA was closed more than 1 hour ago (default expiration
258 		// time)
259 		repoA.close();
260 		repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000);
261 		// close repoB but this one will not be expired
262 		repoB.close();
263 
264 		assertEquals(3, RepositoryCache.getRegisteredKeys().size());
265 		assertTrue(RepositoryCache.isCached(repoA));
266 		assertTrue(RepositoryCache.isCached(repoB));
267 		assertTrue(RepositoryCache.isCached(repoC));
268 
269 		RepositoryCache.clearExpired();
270 
271 		assertEquals(2, RepositoryCache.getRegisteredKeys().size());
272 		assertFalse(RepositoryCache.isCached(repoA));
273 		assertTrue(RepositoryCache.isCached(repoB));
274 		assertTrue(RepositoryCache.isCached(repoC));
275 	}
276 
277 	@Test
278 	public void testReconfigure() throws InterruptedException, IOException {
279 		@SuppressWarnings({"resource", "deprecation"}) // We are testing the close() method
280 		Repository repo = createRepository(false, false);
281 		RepositoryCache.register(repo);
282 		assertTrue(RepositoryCache.isCached(repo));
283 		repo.close();
284 		assertTrue(RepositoryCache.isCached(repo));
285 
286 		// Actually, we would only need to validate that
287 		// WorkQueue.getExecutor().scheduleWithFixedDelay is called with proper
288 		// values but since we do not have a mock library, we test
289 		// reconfiguration from a black box perspective. I.e. reconfigure
290 		// expireAfter and cleanupDelay to 1 ms and wait until the Repository
291 		// is evicted to prove that reconfiguration worked.
292 		RepositoryCacheConfig config = new RepositoryCacheConfig();
293 		config.setExpireAfter(1);
294 		config.setCleanupDelay(1);
295 		config.install();
296 
297 		// Instead of using a fixed waiting time, start with small and increase:
298 		// sleep 1, 2, 4, 8, 16, ..., 1024 ms
299 		// This wait will time out after 2048 ms
300 		for (int i = 0; i <= 10; i++) {
301 			Thread.sleep(1 << i);
302 			if (!RepositoryCache.isCached(repo)) {
303 				return;
304 			}
305 		}
306 		fail("Repository should have been evicted from cache");
307 	}
308 }