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.

RacyGitTests.java 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2010, Christian Halstrick <christian.halstrick@sap.com>
  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. package org.eclipse.jgit.lib;
  44. import static java.lang.Long.valueOf;
  45. import static org.junit.Assert.assertEquals;
  46. import static org.junit.Assert.assertTrue;
  47. import java.io.File;
  48. import java.io.FileOutputStream;
  49. import java.io.IOException;
  50. import java.util.TreeSet;
  51. import org.eclipse.jgit.junit.RepositoryTestCase;
  52. import org.eclipse.jgit.treewalk.FileTreeIterator;
  53. import org.eclipse.jgit.treewalk.FileTreeIteratorWithTimeControl;
  54. import org.eclipse.jgit.treewalk.NameConflictTreeWalk;
  55. import org.eclipse.jgit.util.FileUtils;
  56. public class RacyGitTests extends RepositoryTestCase {
  57. public void testIterator() throws IllegalStateException, IOException,
  58. InterruptedException {
  59. TreeSet<Long> modTimes = new TreeSet<>();
  60. File lastFile = null;
  61. for (int i = 0; i < 10; i++) {
  62. lastFile = new File(db.getWorkTree(), "0." + i);
  63. FileUtils.createNewFile(lastFile);
  64. if (i == 5)
  65. fsTick(lastFile);
  66. }
  67. modTimes.add(valueOf(fsTick(lastFile)));
  68. for (int i = 0; i < 10; i++) {
  69. lastFile = new File(db.getWorkTree(), "1." + i);
  70. FileUtils.createNewFile(lastFile);
  71. }
  72. modTimes.add(valueOf(fsTick(lastFile)));
  73. for (int i = 0; i < 10; i++) {
  74. lastFile = new File(db.getWorkTree(), "2." + i);
  75. FileUtils.createNewFile(lastFile);
  76. if (i % 4 == 0)
  77. fsTick(lastFile);
  78. }
  79. FileTreeIteratorWithTimeControl fileIt = new FileTreeIteratorWithTimeControl(
  80. db, modTimes);
  81. try (NameConflictTreeWalk tw = new NameConflictTreeWalk(db)) {
  82. tw.addTree(fileIt);
  83. tw.setRecursive(true);
  84. FileTreeIterator t;
  85. long t0 = 0;
  86. for (int i = 0; i < 10; i++) {
  87. assertTrue(tw.next());
  88. t = tw.getTree(0, FileTreeIterator.class);
  89. if (i == 0) {
  90. t0 = t.getEntryLastModified();
  91. } else {
  92. assertEquals(t0, t.getEntryLastModified());
  93. }
  94. }
  95. long t1 = 0;
  96. for (int i = 0; i < 10; i++) {
  97. assertTrue(tw.next());
  98. t = tw.getTree(0, FileTreeIterator.class);
  99. if (i == 0) {
  100. t1 = t.getEntryLastModified();
  101. assertTrue(t1 > t0);
  102. } else {
  103. assertEquals(t1, t.getEntryLastModified());
  104. }
  105. }
  106. long t2 = 0;
  107. for (int i = 0; i < 10; i++) {
  108. assertTrue(tw.next());
  109. t = tw.getTree(0, FileTreeIterator.class);
  110. if (i == 0) {
  111. t2 = t.getEntryLastModified();
  112. assertTrue(t2 > t1);
  113. } else {
  114. assertEquals(t2, t.getEntryLastModified());
  115. }
  116. }
  117. }
  118. }
  119. public void testRacyGitDetection() throws IOException,
  120. IllegalStateException, InterruptedException {
  121. TreeSet<Long> modTimes = new TreeSet<>();
  122. File lastFile;
  123. // wait to ensure that modtimes of the file doesn't match last index
  124. // file modtime
  125. modTimes.add(valueOf(fsTick(db.getIndexFile())));
  126. // create two files
  127. addToWorkDir("a", "a");
  128. lastFile = addToWorkDir("b", "b");
  129. // wait to ensure that file-modTimes and therefore index entry modTime
  130. // doesn't match the modtime of index-file after next persistance
  131. modTimes.add(valueOf(fsTick(lastFile)));
  132. // now add both files to the index. No racy git expected
  133. resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
  134. assertEquals(
  135. "[a, mode:100644, time:t0, length:1, content:a]" +
  136. "[b, mode:100644, time:t0, length:1, content:b]",
  137. indexState(SMUDGE | MOD_TIME | LENGTH | CONTENT));
  138. // Remember the last modTime of index file. All modifications times of
  139. // further modification are translated to this value so it looks that
  140. // files have been modified in the same time slot as the index file
  141. modTimes.add(Long.valueOf(db.getIndexFile().lastModified()));
  142. // modify one file
  143. addToWorkDir("a", "a2");
  144. // now update the index the index. 'a' has to be racily clean -- because
  145. // it's modification time is exactly the same as the previous index file
  146. // mod time.
  147. resetIndex(new FileTreeIteratorWithTimeControl(db, modTimes));
  148. db.readDirCache();
  149. // although racily clean a should not be reported as being dirty
  150. assertEquals(
  151. "[a, mode:100644, time:t1, smudged, length:0, content:a2]" +
  152. "[b, mode:100644, time:t0, length:1, content:b]",
  153. indexState(SMUDGE|MOD_TIME|LENGTH|CONTENT));
  154. }
  155. private File addToWorkDir(String path, String content) throws IOException {
  156. File f = new File(db.getWorkTree(), path);
  157. FileOutputStream fos = new FileOutputStream(f);
  158. try {
  159. fos.write(content.getBytes(Constants.CHARACTER_ENCODING));
  160. return f;
  161. } finally {
  162. fos.close();
  163. }
  164. }
  165. }