26
26
27
27
import static org .junit .jupiter .api .Assertions .assertEquals ;
28
28
import static org .junit .jupiter .api .Assertions .assertFalse ;
29
+ import static org .junit .jupiter .api .Assertions .assertNotEquals ;
29
30
import static org .junit .jupiter .api .Assertions .assertNotNull ;
30
31
import static org .junit .jupiter .api .Assertions .assertNull ;
31
32
import static org .junit .jupiter .api .Assertions .assertThrows ;
42
43
import java .nio .file .Files ;
43
44
import java .nio .file .Path ;
44
45
import java .nio .file .Paths ;
46
+ import java .nio .file .attribute .FileTime ;
45
47
import java .util .Date ;
46
48
import java .util .Iterator ;
47
49
import java .util .LinkedList ;
63
65
import org .opengrok .indexer .configuration .IgnoredNames ;
64
66
import org .opengrok .indexer .configuration .RuntimeEnvironment ;
65
67
import org .opengrok .indexer .util .IOUtils ;
66
- import org .opengrok .indexer .util .TandemPath ;
67
68
import org .opengrok .indexer .util .TestRepository ;
68
69
69
70
/**
@@ -80,7 +81,6 @@ class FileHistoryCacheTest {
80
81
private FileHistoryCache cache ;
81
82
82
83
private boolean savedFetchHistoryWhenNotInCache ;
83
- private int savedHistoryReaderTimeLimit ;
84
84
private boolean savedIsHandleHistoryOfRenamedFiles ;
85
85
private boolean savedIsTagsEnabled ;
86
86
@@ -96,7 +96,6 @@ public void setUp() throws Exception {
96
96
cache .initialize ();
97
97
98
98
savedFetchHistoryWhenNotInCache = env .isFetchHistoryWhenNotInCache ();
99
- savedHistoryReaderTimeLimit = env .getHistoryReaderTimeLimit ();
100
99
savedIsHandleHistoryOfRenamedFiles = env .isHandleHistoryOfRenamedFiles ();
101
100
savedIsTagsEnabled = env .isTagsEnabled ();
102
101
}
@@ -112,7 +111,6 @@ public void tearDown() {
112
111
cache = null ;
113
112
114
113
env .setFetchHistoryWhenNotInCache (savedFetchHistoryWhenNotInCache );
115
- env .setHistoryReaderTimeLimit (savedHistoryReaderTimeLimit );
116
114
env .setIgnoredNames (new IgnoredNames ());
117
115
env .setIncludedNames (new Filter ());
118
116
env .setHandleHistoryOfRenamedFiles (savedIsHandleHistoryOfRenamedFiles );
@@ -156,6 +154,57 @@ private void assertSameEntry(HistoryEntry expected, HistoryEntry actual, boolean
156
154
}
157
155
}
158
156
157
+ /**
158
+ * {@link FileHistoryCache#get(File, Repository, boolean)} should not disturb history cache
159
+ * if run between repository update and reindex.
160
+ */
161
+ @ EnabledOnOs ({OS .LINUX , OS .MAC , OS .SOLARIS , OS .AIX , OS .OTHER })
162
+ @ EnabledForRepository (MERCURIAL )
163
+ @ Test
164
+ void testStoreTouchGet () throws Exception {
165
+ File reposRoot = new File (repositories .getSourceRoot (), "mercurial" );
166
+ Repository repo = RepositoryFactory .getRepository (reposRoot );
167
+ History historyToStore = repo .getHistory (reposRoot );
168
+
169
+ cache .store (historyToStore , repo );
170
+
171
+ // This makes sure that the file which contains the latest revision has indeed been created.
172
+ assertEquals ("9:8b340409b3a8" , cache .getLatestCachedRevision (repo ));
173
+
174
+ File file = new File (reposRoot , "main.c" );
175
+ assertTrue (file .exists ());
176
+ FileTime fileTimeBeforeImport = Files .getLastModifiedTime (file .toPath ());
177
+ History historyBeforeImport = cache .get (file , repo , false );
178
+
179
+ MercurialRepositoryTest .runHgCommand (reposRoot , "import" ,
180
+ Paths .get (getClass ().getResource ("/history/hg-export.txt" ).toURI ()).toString ());
181
+ FileTime fileTimeAfterImport = Files .getLastModifiedTime (file .toPath ());
182
+ assertTrue (fileTimeBeforeImport .compareTo (fileTimeAfterImport ) < 0 );
183
+
184
+ // This get() call basically mimics a request through the UI or API.
185
+ History historyAfterImport = cache .get (file , repo , false );
186
+ assertNotNull (historyAfterImport );
187
+ assertNotEquals (historyBeforeImport , historyAfterImport );
188
+
189
+ // Simulates reindex, or at least its first part when history cache is updated.
190
+ repo .createCache (cache , cache .getLatestCachedRevision (repo ));
191
+
192
+ // This makes sure that the file which contains the latest revision has indeed been created.
193
+ assertEquals ("11:bbb3ce75e1b8" , cache .getLatestCachedRevision (repo ));
194
+
195
+ /*
196
+ * The history should not be disturbed.
197
+ * Make sure that get() retrieved the history from cache. Mocking/spying static methods
198
+ * (FileHistoryCache#readCache() in this case) is tricky so use the cache hits metric.
199
+ */
200
+ double cacheHitsBeforeGet = cache .getFileHistoryCacheHits ();
201
+ History historyAfterReindex = cache .get (file , repo , false );
202
+ double cacheHitsAfterGet = cache .getFileHistoryCacheHits ();
203
+ assertNotNull (historyAfterReindex );
204
+ assertEquals (historyAfterImport , historyAfterReindex );
205
+ assertEquals (1 , cacheHitsAfterGet - cacheHitsBeforeGet );
206
+ }
207
+
159
208
/**
160
209
* Basic tests for the {@code store()} method on cache with disabled
161
210
* handling of renamed files.
@@ -838,8 +887,7 @@ void testRenamedFile() throws Exception {
838
887
updatedHistory .getHistoryEntries (), false );
839
888
}
840
889
841
- private void checkNoHistoryFetchRepo (String reponame , String filename ,
842
- boolean hasHistory , boolean historyFileExists ) throws Exception {
890
+ private void checkNoHistoryFetchRepo (String reponame , String filename , boolean hasHistory ) throws Exception {
843
891
844
892
File reposRoot = new File (repositories .getSourceRoot (), reponame );
845
893
Repository repo = RepositoryFactory .getRepository (reposRoot );
@@ -853,13 +901,6 @@ private void checkNoHistoryFetchRepo(String reponame, String filename,
853
901
// in history cache.
854
902
History retrievedHistory = cache .get (repoFile , repo , true );
855
903
assertEquals (hasHistory , retrievedHistory != null );
856
-
857
- // The file in history cache should not exist since
858
- // FetchHistoryWhenNotInCache is set to false.
859
- File dataRoot = new File (repositories .getDataRoot (),
860
- "historycache" + File .separatorChar + reponame );
861
- File fileHistory = new File (dataRoot , TandemPath .join (filename , ".gz" ));
862
- assertEquals (historyFileExists , fileHistory .exists ());
863
904
}
864
905
865
906
/*
@@ -871,19 +912,13 @@ void testNoHistoryFetch() throws Exception {
871
912
// Do not create history cache for files which do not have it cached.
872
913
env .setFetchHistoryWhenNotInCache (false );
873
914
874
- // Make cache.get() predictable. Normally when the retrieval of
875
- // history of given file is faster than the limit, the history of this
876
- // file is not stored. For the sake of this test we want the history
877
- // to be always stored.
878
- env .setHistoryReaderTimeLimit (0 );
879
-
880
915
// Pretend we are done with first phase of indexing.
881
916
cache .setHistoryIndexDone ();
882
917
883
918
// First try repo with ability to fetch history for directories.
884
- checkNoHistoryFetchRepo ("mercurial" , "main.c" , false , false );
919
+ checkNoHistoryFetchRepo ("mercurial" , "main.c" , false );
885
920
// Second try repo which can fetch history of individual files only.
886
- checkNoHistoryFetchRepo ("teamware" , "header.h" , true , true );
921
+ checkNoHistoryFetchRepo ("teamware" , "header.h" , true );
887
922
}
888
923
889
924
/**
0 commit comments