1
1
using System ;
2
+ using System . Collections . Concurrent ;
2
3
using System . IO ;
4
+ using System . Linq ;
3
5
using System . Threading ;
4
6
using CodeIndex . Common ;
5
7
using CodeIndex . IndexBuilder ;
6
8
using CodeIndex . MaintainIndex ;
7
9
using CodeIndex . Search ;
10
+ using Lucene . Net . Search ;
8
11
using NUnit . Framework ;
9
12
10
13
namespace CodeIndex . Test
@@ -14,7 +17,7 @@ public class CodeFilesIndexMaintainerTest : BaseTest
14
17
[ Test ]
15
18
public void TestMaintainerIndex ( )
16
19
{
17
- var waitMS = 2000 ;
20
+ var waitMS = 1500 ;
18
21
Directory . CreateDirectory ( MonitorFolder ) ;
19
22
Directory . CreateDirectory ( Path . Combine ( MonitorFolder , "FolderA" ) ) ;
20
23
Directory . CreateDirectory ( Path . Combine ( MonitorFolder , "FolderB" ) ) ;
@@ -27,23 +30,97 @@ public void TestMaintainerIndex()
27
30
File . Create ( fileBPath ) . Close ( ) ;
28
31
File . AppendAllText ( fileBPath , "this is a content for test, that's it\r \n a new line;" ) ;
29
32
33
+ var fileCPath = Path . Combine ( MonitorFolder , "CCC.xml" ) ;
34
+ File . Create ( fileCPath ) . Close ( ) ;
35
+ File . AppendAllText ( fileCPath , "this is a content for test" ) ;
36
+
37
+ var fileDPath = Path . Combine ( MonitorFolder , "DDD.txt" ) ;
38
+
30
39
CodeIndexBuilder . BuildIndex ( Config , true , true , true ,
31
- new [ ] { CodeSource . GetCodeSource ( new FileInfo ( fileAPath ) , "12345" ) ,
32
- CodeSource . GetCodeSource ( new FileInfo ( fileBPath ) , "this is a content for test, that's it\r \n a new line;" ) } ) ;
40
+ new [ ]
41
+ {
42
+ CodeSource . GetCodeSource ( new FileInfo ( fileAPath ) , "12345" ) ,
43
+ CodeSource . GetCodeSource ( new FileInfo ( fileBPath ) , "this is a content for test, that's it\r \n a new line;" ) ,
44
+ CodeSource . GetCodeSource ( new FileInfo ( fileCPath ) , "this is a content for test" )
45
+ } ) ;
46
+
33
47
LucenePool . SaveResultsAndClearLucenePool ( Config . LuceneIndexForCode ) ;
34
48
35
- using var maintainer = new CodeFilesIndexMaintainer ( Config , new [ ] { "dll" } , Array . Empty < string > ( ) , 1 ) ;
49
+ var codeSources = CodeIndexSearcher . SearchCode ( Config . LuceneIndexForCode , new MatchAllDocsQuery ( ) , 100 ) ;
50
+ CollectionAssert . AreEquivalent ( new [ ] { "AAA.cs" , "BBB.xml" , "CCC.xml" } , codeSources . Select ( u => u . FileName ) ) ;
51
+
52
+ using var maintainer = new CodeFilesIndexMaintainer ( Config , new [ ] { ".dll" } , Array . Empty < string > ( ) , 1 ) ;
36
53
maintainer . StartWatch ( ) ;
37
54
maintainer . SetInitalizeFinishedToTrue ( ) ;
38
- File . AppendAllText ( fileAPath , "56789" ) ;
55
+
56
+ File . AppendAllText ( fileAPath , "56789" ) ; // Changed
57
+ File . Delete ( fileBPath ) ; // Deleted
58
+ File . Move ( fileCPath , Path . Combine ( MonitorFolder , "NewCCC.xml" ) ) ; // Rename
59
+ File . Create ( fileDPath ) . Close ( ) ; // Created
60
+
39
61
Thread . Sleep ( waitMS ) ; // wait task finish saving
40
62
41
- var index = CodeIndexSearcher . Search ( Config . LuceneIndexForCode , Generator . GetQueryFromStr ( $ "{ nameof ( CodeSource . FileName ) } :\" AAA.cs\" ") , 10 ) ;
42
- Assert . AreEqual ( 1 , index . Length ) ;
43
- Assert . AreEqual ( "1234556789" , index [ 0 ] . Get ( nameof ( CodeSource . Content ) ) ) ;
63
+ codeSources = CodeIndexSearcher . SearchCode ( Config . LuceneIndexForCode , new MatchAllDocsQuery ( ) , 100 ) ;
64
+
65
+ Assert . Multiple ( ( ) =>
66
+ {
67
+ CollectionAssert . AreEquivalent ( new [ ] { "AAA.cs" , "NewCCC.xml" , "DDD.txt" } , codeSources . Select ( u => u . FileName ) ) ;
68
+ CollectionAssert . AreEquivalent ( new [ ] { "1234556789" , "this is a content for test" , string . Empty } , codeSources . Select ( u => u . Content ) ) ;
69
+ CollectionAssert . AreEquivalent ( new [ ] { fileAPath , Path . Combine ( MonitorFolder , "NewCCC.xml" ) , fileDPath } , codeSources . Select ( u => u . FilePath ) ) ;
70
+ } ) ;
71
+
72
+ maintainer . Dispose ( ) ;
73
+ }
74
+
75
+ [ Test ]
76
+ public void TestMaintainerIndex_RetryFailed ( )
77
+ {
78
+ var waitMS = 1500 ;
79
+ Directory . CreateDirectory ( MonitorFolder ) ;
80
+
81
+ var fileAPath = Path . Combine ( MonitorFolder , "AAA.cs" ) ;
82
+ File . Create ( fileAPath ) . Close ( ) ;
83
+ File . AppendAllText ( fileAPath , "12345" ) ;
84
+
85
+ using var maintainer = new CodeFilesIndexMaintainerForTest ( Config , new [ ] { ".dll" } , Array . Empty < string > ( ) , 1 ) ;
86
+ maintainer . StartWatch ( ) ;
87
+ maintainer . SetInitalizeFinishedToTrue ( ) ;
88
+
89
+ maintainer . PendingRetryCodeSources . Enqueue ( new PendingRetrySource ( )
90
+ {
91
+ ChangesType = WatcherChangeTypes . Created ,
92
+ FilePath = fileAPath ,
93
+ LastRetryUTCDate = DateTime . Now . AddDays ( - 1 )
94
+ } ) ;
95
+
96
+ var retryTime = 3 ;
97
+ var codeSources = Array . Empty < CodeSource > ( ) ;
98
+
99
+ while ( retryTime > 0 )
100
+ {
101
+ Thread . Sleep ( waitMS ) ; // wait task finish saving
102
+ retryTime -- ;
103
+ codeSources = CodeIndexSearcher . SearchCode ( Config . LuceneIndexForCode , new MatchAllDocsQuery ( ) , 100 ) ;
104
+
105
+ if ( codeSources . Length > 0 )
106
+ {
107
+ break ;
108
+ }
109
+ }
110
+
111
+ Assert . AreEqual ( 1 , codeSources . Length ) ;
112
+ Assert . AreEqual ( "AAA.cs" , codeSources [ 0 ] . FileName ) ;
44
113
}
45
114
46
- // TODO: Test False Tolerance
47
- // TODO: Test Directoy Change
115
+ class CodeFilesIndexMaintainerForTest : CodeFilesIndexMaintainer
116
+ {
117
+ public CodeFilesIndexMaintainerForTest ( CodeIndexConfiguration config , string [ ] excludedExtensions , string [ ] excludedPaths , int saveIntervalSeconds = 300 , string [ ] includedExtensions = null , ILog log = null ) : base ( config , excludedExtensions , excludedPaths , saveIntervalSeconds , includedExtensions , log )
118
+ {
119
+ }
120
+
121
+ public ConcurrentQueue < PendingRetrySource > PendingRetryCodeSources => pendingRetryCodeSources ;
122
+
123
+ protected override int SleepMilliseconds => 100 ;
124
+ }
48
125
}
49
126
}
0 commit comments