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 5.9KB

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