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  		Repository r;
98  
99  		r = new FileKey(db.getDirectory(), db.getFS()).open(true);
100 		assertNotNull(r);
101 		assertEqualsFile(db.getDirectory(), r.getDirectory());
102 		r.close();
103 
104 		r = new FileKey(db.getDirectory(), db.getFS()).open(false);
105 		assertNotNull(r);
106 		assertEqualsFile(db.getDirectory(), r.getDirectory());
107 		r.close();
108 	}
109 
110 	@Test
111 	public void testFileKeyOpenNew() throws IOException {
112 		final Repository n = createRepository(true, false);
113 		final File gitdir = n.getDirectory();
114 		n.close();
115 		recursiveDelete(gitdir);
116 		assertFalse(gitdir.exists());
117 
118 		try {
119 			new FileKey(gitdir, db.getFS()).open(true);
120 			fail("incorrectly opened a non existant repository");
121 		} catch (RepositoryNotFoundException e) {
122 			assertEquals("repository not found: " + gitdir.getCanonicalPath(),
123 					e.getMessage());
124 		}
125 
126 		final Repository o = new FileKey(gitdir, db.getFS()).open(false);
127 		assertNotNull(o);
128 		assertEqualsFile(gitdir, o.getDirectory());
129 		assertFalse(gitdir.exists());
130 	}
131 
132 	@Test
133 	public void testCacheRegisterOpen() throws Exception {
134 		final File dir = db.getDirectory();
135 		RepositoryCache.register(db);
136 		assertSame(db, RepositoryCache.open(FileKey.exact(dir, db.getFS())));
137 
138 		assertEquals(".git", dir.getName());
139 		final File parent = dir.getParentFile();
140 		assertSame(db, RepositoryCache.open(FileKey.lenient(parent, db.getFS())));
141 	}
142 
143 	@Test
144 	public void testCacheOpen() throws Exception {
145 		final FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
146 		final Repository d2 = RepositoryCache.open(loc);
147 		assertNotSame(db, d2);
148 		assertSame(d2, RepositoryCache.open(FileKey.exact(loc.getFile(), db.getFS())));
149 		d2.close();
150 		d2.close();
151 	}
152 
153 	@Test
154 	public void testGetRegisteredWhenEmpty() {
155 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
156 	}
157 
158 	@Test
159 	public void testGetRegistered() {
160 		RepositoryCache.register(db);
161 
162 		assertThat(RepositoryCache.getRegisteredKeys(),
163 				hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
164 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
165 	}
166 
167 	@Test
168 	public void testUnregister() {
169 		RepositoryCache.register(db);
170 		RepositoryCache
171 				.unregister(FileKey.exact(db.getDirectory(), db.getFS()));
172 
173 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
174 	}
175 
176 	@Test
177 	public void testRepositoryUsageCount() throws Exception {
178 		FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
179 		Repository d2 = RepositoryCache.open(loc);
180 		assertEquals(1, d2.useCnt.get());
181 		RepositoryCache.open(FileKey.exact(loc.getFile(), db.getFS()));
182 		assertEquals(2, d2.useCnt.get());
183 		d2.close();
184 		assertEquals(1, d2.useCnt.get());
185 		d2.close();
186 		assertEquals(0, d2.useCnt.get());
187 	}
188 
189 	@Test
190 	public void testRepositoryUsageCountWithRegisteredRepository()
191 			throws IOException {
192 		Repository repo = createRepository(false, false);
193 		assertEquals(1, repo.useCnt.get());
194 		RepositoryCache.register(repo);
195 		assertEquals(1, repo.useCnt.get());
196 		repo.close();
197 		assertEquals(0, repo.useCnt.get());
198 	}
199 
200 	@Test
201 	public void testRepositoryNotUnregisteringWhenClosing() throws Exception {
202 		FileKey loc = FileKey.exact(db.getDirectory(), db.getFS());
203 		Repository d2 = RepositoryCache.open(loc);
204 		assertEquals(1, d2.useCnt.get());
205 		assertThat(RepositoryCache.getRegisteredKeys(),
206 				hasItem(FileKey.exact(db.getDirectory(), db.getFS())));
207 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
208 		d2.close();
209 		assertEquals(0, d2.useCnt.get());
210 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
211 		assertTrue(RepositoryCache.isCached(d2));
212 	}
213 
214 	@Test
215 	public void testRepositoryUnregisteringWhenExpiredAndUsageCountNegative()
216 			throws Exception {
217 		Repository repoA = createBareRepository();
218 		RepositoryCache.register(repoA);
219 
220 		assertEquals(1, RepositoryCache.getRegisteredKeys().size());
221 		assertTrue(RepositoryCache.isCached(repoA));
222 
223 		// close the repo twice to make usage count negative
224 		repoA.close();
225 		repoA.close();
226 		// fake that repoA was closed more than 1 hour ago (default expiration
227 		// time)
228 		repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000);
229 
230 		RepositoryCache.clearExpired();
231 
232 		assertEquals(0, RepositoryCache.getRegisteredKeys().size());
233 	}
234 
235 	@Test
236 	public void testRepositoryUnregisteringWhenExpired() throws Exception {
237 		Repository repoA = createRepository(true, false);
238 		Repository repoB = createRepository(true, false);
239 		Repository repoC = createBareRepository();
240 		RepositoryCache.register(repoA);
241 		RepositoryCache.register(repoB);
242 		RepositoryCache.register(repoC);
243 
244 		assertEquals(3, RepositoryCache.getRegisteredKeys().size());
245 		assertTrue(RepositoryCache.isCached(repoA));
246 		assertTrue(RepositoryCache.isCached(repoB));
247 		assertTrue(RepositoryCache.isCached(repoC));
248 
249 		// fake that repoA was closed more than 1 hour ago (default expiration
250 		// time)
251 		repoA.close();
252 		repoA.closedAt.set(System.currentTimeMillis() - 65 * 60 * 1000);
253 		// close repoB but this one will not be expired
254 		repoB.close();
255 
256 		assertEquals(3, RepositoryCache.getRegisteredKeys().size());
257 		assertTrue(RepositoryCache.isCached(repoA));
258 		assertTrue(RepositoryCache.isCached(repoB));
259 		assertTrue(RepositoryCache.isCached(repoC));
260 
261 		RepositoryCache.clearExpired();
262 
263 		assertEquals(2, RepositoryCache.getRegisteredKeys().size());
264 		assertFalse(RepositoryCache.isCached(repoA));
265 		assertTrue(RepositoryCache.isCached(repoB));
266 		assertTrue(RepositoryCache.isCached(repoC));
267 	}
268 
269 	@Test
270 	public void testReconfigure() throws InterruptedException, IOException {
271 		Repository repo = createRepository(false, false);
272 		RepositoryCache.register(repo);
273 		assertTrue(RepositoryCache.isCached(repo));
274 		repo.close();
275 		assertTrue(RepositoryCache.isCached(repo));
276 
277 		// Actually, we would only need to validate that
278 		// WorkQueue.getExecutor().scheduleWithFixedDelay is called with proper
279 		// values but since we do not have a mock library, we test
280 		// reconfiguration from a black box perspective. I.e. reconfigure
281 		// expireAfter and cleanupDelay to 1 ms and wait until the Repository
282 		// is evicted to prove that reconfiguration worked.
283 		RepositoryCacheConfig config = new RepositoryCacheConfig();
284 		config.setExpireAfter(1);
285 		config.setCleanupDelay(1);
286 		config.install();
287 
288 		// Instead of using a fixed waiting time, start with small and increase:
289 		// sleep 1, 2, 4, 8, 16, ..., 1024 ms
290 		// This wait will time out after 2048 ms
291 		for (int i = 0; i <= 10; i++) {
292 			Thread.sleep(1 << i);
293 			if (!RepositoryCache.isCached(repo)) {
294 				return;
295 			}
296 		}
297 		fail("Repository should have been evicted from cache");
298 	}
299 }