You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

LockFileTest.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2012, 2021 GitHub Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.internal.storage.file;
  11. import static org.junit.Assert.assertFalse;
  12. import static org.junit.Assert.assertNotNull;
  13. import static org.junit.Assert.assertTrue;
  14. import static org.junit.Assert.fail;
  15. import java.io.File;
  16. import java.io.OutputStream;
  17. import java.nio.charset.StandardCharsets;
  18. import org.eclipse.jgit.api.Git;
  19. import org.eclipse.jgit.api.errors.JGitInternalException;
  20. import org.eclipse.jgit.errors.LockFailedException;
  21. import org.eclipse.jgit.junit.RepositoryTestCase;
  22. import org.eclipse.jgit.revwalk.RevCommit;
  23. import org.junit.Test;
  24. /**
  25. * Unit tests of {@link LockFile}
  26. */
  27. public class LockFileTest extends RepositoryTestCase {
  28. @Test
  29. public void lockFailedExceptionRecovery() throws Exception {
  30. try (Git git = new Git(db)) {
  31. writeTrashFile("file.txt", "content");
  32. git.add().addFilepattern("file.txt").call();
  33. RevCommit commit1 = git.commit().setMessage("create file").call();
  34. assertNotNull(commit1);
  35. writeTrashFile("file.txt", "content2");
  36. git.add().addFilepattern("file.txt").call();
  37. assertNotNull(git.commit().setMessage("edit file").call());
  38. LockFile lf = new LockFile(db.getIndexFile());
  39. assertTrue(lf.lock());
  40. try {
  41. git.checkout().setName(commit1.name()).call();
  42. fail("JGitInternalException not thrown");
  43. } catch (JGitInternalException e) {
  44. assertTrue(e.getCause() instanceof LockFailedException);
  45. lf.unlock();
  46. git.checkout().setName(commit1.name()).call();
  47. }
  48. }
  49. }
  50. @Test
  51. public void testLockTwice() throws Exception {
  52. File f = writeTrashFile("somefile", "content");
  53. LockFile lock = new LockFile(f);
  54. assertTrue(lock.lock());
  55. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  56. lock.commit();
  57. assertFalse(lock.isLocked());
  58. checkFile(f, "other");
  59. assertTrue(lock.lock());
  60. assertTrue(lock.isLocked());
  61. try (OutputStream out = lock.getOutputStream()) {
  62. out.write("second".getBytes(StandardCharsets.US_ASCII));
  63. }
  64. lock.commit();
  65. assertFalse(lock.isLocked());
  66. checkFile(f, "second");
  67. }
  68. @Test
  69. public void testLockTwiceUnlock() throws Exception {
  70. File f = writeTrashFile("somefile", "content");
  71. LockFile lock = new LockFile(f);
  72. assertTrue(lock.lock());
  73. assertTrue(lock.isLocked());
  74. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  75. lock.unlock();
  76. assertFalse(lock.isLocked());
  77. checkFile(f, "content");
  78. assertTrue(lock.lock());
  79. assertTrue(lock.isLocked());
  80. try (OutputStream out = lock.getOutputStream()) {
  81. out.write("second".getBytes(StandardCharsets.US_ASCII));
  82. }
  83. lock.commit();
  84. assertFalse(lock.isLocked());
  85. checkFile(f, "second");
  86. }
  87. @Test
  88. public void testLockWriteTwiceThrows1() throws Exception {
  89. File f = writeTrashFile("somefile", "content");
  90. LockFile lock = new LockFile(f);
  91. assertTrue(lock.lock());
  92. assertTrue(lock.isLocked());
  93. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  94. try {
  95. lock.write("second".getBytes(StandardCharsets.US_ASCII));
  96. fail();
  97. } catch (Exception e) {
  98. // expected
  99. }
  100. lock.unlock();
  101. }
  102. @Test
  103. public void testLockWriteTwiceThrows2() throws Exception {
  104. File f = writeTrashFile("somefile", "content");
  105. LockFile lock = new LockFile(f);
  106. assertTrue(lock.lock());
  107. assertTrue(lock.isLocked());
  108. try (OutputStream out = lock.getOutputStream()) {
  109. out.write("other".getBytes(StandardCharsets.US_ASCII));
  110. }
  111. try {
  112. lock.write("second".getBytes(StandardCharsets.US_ASCII));
  113. fail();
  114. } catch (Exception e) {
  115. // expected
  116. }
  117. lock.unlock();
  118. }
  119. @Test
  120. public void testLockWriteTwiceThrows3() throws Exception {
  121. File f = writeTrashFile("somefile", "content");
  122. LockFile lock = new LockFile(f);
  123. assertTrue(lock.lock());
  124. assertTrue(lock.isLocked());
  125. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  126. try (OutputStream out = lock.getOutputStream()) {
  127. out.write("second".getBytes(StandardCharsets.US_ASCII));
  128. fail();
  129. } catch (Exception e) {
  130. // expected
  131. }
  132. lock.unlock();
  133. }
  134. @Test
  135. public void testLockWriteTwiceThrows4() throws Exception {
  136. File f = writeTrashFile("somefile", "content");
  137. LockFile lock = new LockFile(f);
  138. assertTrue(lock.lock());
  139. assertTrue(lock.isLocked());
  140. try (OutputStream out = lock.getOutputStream()) {
  141. out.write("other".getBytes(StandardCharsets.US_ASCII));
  142. }
  143. try (OutputStream out = lock.getOutputStream()) {
  144. out.write("second".getBytes(StandardCharsets.US_ASCII));
  145. fail();
  146. } catch (Exception e) {
  147. // expected
  148. }
  149. lock.unlock();
  150. }
  151. @Test
  152. public void testLockUnclosedCommitThrows() throws Exception {
  153. File f = writeTrashFile("somefile", "content");
  154. LockFile lock = new LockFile(f);
  155. assertTrue(lock.lock());
  156. assertTrue(lock.isLocked());
  157. try (OutputStream out = lock.getOutputStream()) {
  158. out.write("other".getBytes(StandardCharsets.US_ASCII));
  159. lock.commit();
  160. fail();
  161. } catch (Exception e) {
  162. // expected
  163. }
  164. }
  165. @Test
  166. public void testLockNested() throws Exception {
  167. File f = writeTrashFile("somefile", "content");
  168. LockFile lock = new LockFile(f);
  169. assertTrue(lock.lock());
  170. assertTrue(lock.isLocked());
  171. try {
  172. lock.lock();
  173. fail();
  174. } catch (IllegalStateException e) {
  175. // expected
  176. }
  177. assertTrue(lock.isLocked());
  178. lock.unlock();
  179. }
  180. @Test
  181. public void testLockHeld() throws Exception {
  182. File f = writeTrashFile("somefile", "content");
  183. LockFile lock = new LockFile(f);
  184. assertTrue(lock.lock());
  185. assertTrue(lock.isLocked());
  186. LockFile lock2 = new LockFile(f);
  187. assertFalse(lock2.lock());
  188. assertFalse(lock2.isLocked());
  189. assertTrue(lock.isLocked());
  190. lock.unlock();
  191. }
  192. @Test
  193. public void testLockForAppend() throws Exception {
  194. File f = writeTrashFile("somefile", "content");
  195. LockFile lock = new LockFile(f);
  196. assertTrue(lock.lockForAppend());
  197. assertTrue(lock.isLocked());
  198. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  199. lock.commit();
  200. assertFalse(lock.isLocked());
  201. checkFile(f, "contentother");
  202. }
  203. }