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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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.assertThrows;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.io.File;
  17. import java.io.OutputStream;
  18. import java.nio.charset.StandardCharsets;
  19. import org.eclipse.jgit.api.Git;
  20. import org.eclipse.jgit.api.errors.JGitInternalException;
  21. import org.eclipse.jgit.errors.LockFailedException;
  22. import org.eclipse.jgit.junit.RepositoryTestCase;
  23. import org.eclipse.jgit.revwalk.RevCommit;
  24. import org.junit.Test;
  25. /**
  26. * Unit tests of {@link LockFile}
  27. */
  28. public class LockFileTest extends RepositoryTestCase {
  29. @Test
  30. public void lockFailedExceptionRecovery() throws Exception {
  31. try (Git git = new Git(db)) {
  32. writeTrashFile("file.txt", "content");
  33. git.add().addFilepattern("file.txt").call();
  34. RevCommit commit1 = git.commit().setMessage("create file").call();
  35. assertNotNull(commit1);
  36. writeTrashFile("file.txt", "content2");
  37. git.add().addFilepattern("file.txt").call();
  38. assertNotNull(git.commit().setMessage("edit file").call());
  39. LockFile lf = new LockFile(db.getIndexFile());
  40. assertTrue(lf.lock());
  41. try {
  42. git.checkout().setName(commit1.name()).call();
  43. fail("JGitInternalException not thrown");
  44. } catch (JGitInternalException e) {
  45. assertTrue(e.getCause() instanceof LockFailedException);
  46. lf.unlock();
  47. git.checkout().setName(commit1.name()).call();
  48. }
  49. }
  50. }
  51. @Test
  52. public void testLockTwice() throws Exception {
  53. File f = writeTrashFile("somefile", "content");
  54. LockFile lock = new LockFile(f);
  55. assertTrue(lock.lock());
  56. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  57. lock.commit();
  58. assertFalse(lock.isLocked());
  59. checkFile(f, "other");
  60. assertTrue(lock.lock());
  61. assertTrue(lock.isLocked());
  62. try (OutputStream out = lock.getOutputStream()) {
  63. out.write("second".getBytes(StandardCharsets.US_ASCII));
  64. }
  65. lock.commit();
  66. assertFalse(lock.isLocked());
  67. checkFile(f, "second");
  68. }
  69. @Test
  70. public void testLockTwiceUnlock() throws Exception {
  71. File f = writeTrashFile("somefile", "content");
  72. LockFile lock = new LockFile(f);
  73. assertTrue(lock.lock());
  74. assertTrue(lock.isLocked());
  75. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  76. lock.unlock();
  77. assertFalse(lock.isLocked());
  78. checkFile(f, "content");
  79. assertTrue(lock.lock());
  80. assertTrue(lock.isLocked());
  81. try (OutputStream out = lock.getOutputStream()) {
  82. out.write("second".getBytes(StandardCharsets.US_ASCII));
  83. }
  84. lock.commit();
  85. assertFalse(lock.isLocked());
  86. checkFile(f, "second");
  87. }
  88. @Test
  89. public void testLockWriteTwiceThrows1() throws Exception {
  90. File f = writeTrashFile("somefile", "content");
  91. LockFile lock = new LockFile(f);
  92. assertTrue(lock.lock());
  93. assertTrue(lock.isLocked());
  94. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  95. assertThrows(Exception.class,
  96. () -> lock.write("second".getBytes(StandardCharsets.US_ASCII)));
  97. lock.unlock();
  98. }
  99. @Test
  100. public void testLockWriteTwiceThrows2() throws Exception {
  101. File f = writeTrashFile("somefile", "content");
  102. LockFile lock = new LockFile(f);
  103. assertTrue(lock.lock());
  104. assertTrue(lock.isLocked());
  105. try (OutputStream out = lock.getOutputStream()) {
  106. out.write("other".getBytes(StandardCharsets.US_ASCII));
  107. }
  108. assertThrows(Exception.class,
  109. () -> lock.write("second".getBytes(StandardCharsets.US_ASCII)));
  110. lock.unlock();
  111. }
  112. @Test
  113. public void testLockWriteTwiceThrows3() throws Exception {
  114. File f = writeTrashFile("somefile", "content");
  115. LockFile lock = new LockFile(f);
  116. assertTrue(lock.lock());
  117. assertTrue(lock.isLocked());
  118. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  119. assertThrows(Exception.class, () -> {
  120. try (OutputStream out = lock.getOutputStream()) {
  121. out.write("second".getBytes(StandardCharsets.US_ASCII));
  122. }
  123. });
  124. lock.unlock();
  125. }
  126. @Test
  127. public void testLockWriteTwiceThrows4() throws Exception {
  128. File f = writeTrashFile("somefile", "content");
  129. LockFile lock = new LockFile(f);
  130. assertTrue(lock.lock());
  131. assertTrue(lock.isLocked());
  132. try (OutputStream out = lock.getOutputStream()) {
  133. out.write("other".getBytes(StandardCharsets.US_ASCII));
  134. }
  135. assertThrows(Exception.class, () -> {
  136. try (OutputStream out = lock.getOutputStream()) {
  137. out.write("second".getBytes(StandardCharsets.US_ASCII));
  138. }
  139. });
  140. lock.unlock();
  141. }
  142. @Test
  143. public void testLockUnclosedCommitThrows() throws Exception {
  144. File f = writeTrashFile("somefile", "content");
  145. LockFile lock = new LockFile(f);
  146. assertTrue(lock.lock());
  147. assertTrue(lock.isLocked());
  148. try (OutputStream out = lock.getOutputStream()) {
  149. out.write("other".getBytes(StandardCharsets.US_ASCII));
  150. assertThrows(Exception.class, () -> lock.commit());
  151. }
  152. }
  153. @Test
  154. public void testLockNested() throws Exception {
  155. File f = writeTrashFile("somefile", "content");
  156. LockFile lock = new LockFile(f);
  157. assertTrue(lock.lock());
  158. assertTrue(lock.isLocked());
  159. assertThrows(IllegalStateException.class, () -> lock.lock());
  160. assertTrue(lock.isLocked());
  161. lock.unlock();
  162. }
  163. @Test
  164. public void testLockHeld() throws Exception {
  165. File f = writeTrashFile("somefile", "content");
  166. LockFile lock = new LockFile(f);
  167. assertTrue(lock.lock());
  168. assertTrue(lock.isLocked());
  169. LockFile lock2 = new LockFile(f);
  170. assertFalse(lock2.lock());
  171. assertFalse(lock2.isLocked());
  172. assertTrue(lock.isLocked());
  173. lock.unlock();
  174. }
  175. @Test
  176. public void testLockForAppend() throws Exception {
  177. File f = writeTrashFile("somefile", "content");
  178. LockFile lock = new LockFile(f);
  179. assertTrue(lock.lockForAppend());
  180. assertTrue(lock.isLocked());
  181. lock.write("other".getBytes(StandardCharsets.US_ASCII));
  182. lock.commit();
  183. assertFalse(lock.isLocked());
  184. checkFile(f, "contentother");
  185. }
  186. }