OLD | NEW |
1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library test.src.task.driver_test; | 5 library test.src.task.driver_test; |
6 | 6 |
7 import 'package:analyzer/src/context/cache.dart'; | 7 import 'package:analyzer/src/context/cache.dart'; |
8 import 'package:analyzer/src/generated/ast.dart'; | |
9 import 'package:analyzer/src/generated/engine.dart' | 8 import 'package:analyzer/src/generated/engine.dart' |
10 show AnalysisContext, CacheState, RetentionPriority; | 9 show |
| 10 AnalysisContext, |
| 11 CacheState, |
| 12 InternalAnalysisContext, |
| 13 RetentionPriority; |
11 import 'package:analyzer/src/generated/java_engine.dart'; | 14 import 'package:analyzer/src/generated/java_engine.dart'; |
12 import 'package:analyzer/src/generated/sdk_io.dart'; | 15 import 'package:analyzer/src/generated/sdk_io.dart'; |
13 import 'package:analyzer/src/generated/source.dart'; | 16 import 'package:analyzer/src/generated/source.dart'; |
14 import 'package:analyzer/src/generated/utilities_collection.dart'; | 17 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 18 import 'package:analyzer/src/task/model.dart'; |
15 import 'package:analyzer/task/model.dart'; | 19 import 'package:analyzer/task/model.dart'; |
| 20 import 'package:typed_mock/typed_mock.dart'; |
16 import 'package:unittest/unittest.dart'; | 21 import 'package:unittest/unittest.dart'; |
17 | 22 |
18 import '../../generated/engine_test.dart'; | 23 import '../../generated/engine_test.dart'; |
19 import '../../generated/test_support.dart'; | 24 import '../../generated/test_support.dart'; |
20 import '../../reflective_tests.dart'; | 25 import '../../reflective_tests.dart'; |
21 | 26 |
22 main() { | 27 main() { |
23 groupSep = ' | '; | 28 groupSep = ' | '; |
24 runReflectiveTests(AnalysisCacheTest); | 29 runReflectiveTests(AnalysisCacheTest); |
25 runReflectiveTests(CacheEntryTest); | 30 runReflectiveTests(CacheEntryTest); |
| 31 runReflectiveTests(CacheFlushManagerTest); |
26 runReflectiveTests(SdkCachePartitionTest); | 32 runReflectiveTests(SdkCachePartitionTest); |
27 runReflectiveTests(UniversalCachePartitionTest); | 33 runReflectiveTests(UniversalCachePartitionTest); |
28 runReflectiveTests(ResultDataTest); | 34 runReflectiveTests(ResultDataTest); |
29 } | 35 } |
30 | 36 |
31 AnalysisCache createCache({AnalysisContext context, | 37 AnalysisCache createCache({AnalysisContext context, |
32 RetentionPriority policy: RetentionPriority.LOW}) { | 38 RetentionPriority policy: RetentionPriority.LOW}) { |
33 CachePartition partition = new UniversalCachePartition( | 39 CachePartition partition = new UniversalCachePartition(context); |
34 context, 8, new TestCacheRetentionPolicy(policy)); | |
35 return new AnalysisCache(<CachePartition>[partition]); | 40 return new AnalysisCache(<CachePartition>[partition]); |
36 } | 41 } |
37 | 42 |
38 @reflectiveTest | 43 @reflectiveTest |
39 class AnalysisCacheTest extends EngineTestCase { | 44 class AnalysisCacheTest extends EngineTestCase { |
40 void test_astSize_empty() { | |
41 AnalysisCache cache = createCache(); | |
42 expect(cache.astSize, 0); | |
43 } | |
44 | |
45 void test_astSize_nonEmpty() { | |
46 ResultDescriptor result = new ResultDescriptor('test', null); | |
47 AstNode node = new NullLiteral(null); | |
48 AnalysisCache cache = createCache(); | |
49 AnalysisTarget target1 = new TestSource('/test1.dart'); | |
50 CacheEntry entry1 = new CacheEntry(); | |
51 entry1.setValue(result, node, TargetedResult.EMPTY_LIST, null); | |
52 AnalysisTarget target2 = new TestSource('/test2.dart'); | |
53 CacheEntry entry2 = new CacheEntry(); | |
54 entry2.setValue(result, node, TargetedResult.EMPTY_LIST, null); | |
55 cache.put(target1, entry1); | |
56 cache.accessedAst(target1); | |
57 cache.put(target2, entry2); | |
58 cache.accessedAst(target2); | |
59 expect(cache.astSize, 2); | |
60 } | |
61 | |
62 void test_creation() { | 45 void test_creation() { |
63 expect(createCache(), isNotNull); | 46 expect(createCache(), isNotNull); |
64 } | 47 } |
65 | 48 |
66 void test_get() { | 49 void test_get() { |
67 AnalysisCache cache = createCache(); | 50 AnalysisCache cache = createCache(); |
68 AnalysisTarget target = new TestSource(); | 51 AnalysisTarget target = new TestSource(); |
69 expect(cache.get(target), isNull); | 52 expect(cache.get(target), isNull); |
70 } | 53 } |
71 | 54 |
(...skipping 24 matching lines...) Expand all Loading... |
96 cache.put(target, entry); | 79 cache.put(target, entry); |
97 expect(cache.get(target), entry); | 80 expect(cache.get(target), entry); |
98 } | 81 } |
99 | 82 |
100 void test_remove() { | 83 void test_remove() { |
101 AnalysisCache cache = createCache(); | 84 AnalysisCache cache = createCache(); |
102 AnalysisTarget target = new TestSource(); | 85 AnalysisTarget target = new TestSource(); |
103 cache.remove(target); | 86 cache.remove(target); |
104 } | 87 } |
105 | 88 |
106 void test_setMaxCacheSize() { | |
107 CachePartition partition = new UniversalCachePartition( | |
108 null, 8, new TestCacheRetentionPolicy(RetentionPriority.MEDIUM)); | |
109 AnalysisCache cache = new AnalysisCache(<CachePartition>[partition]); | |
110 ResultDescriptor result = new ResultDescriptor('test', null); | |
111 AstNode node = new NullLiteral(null); | |
112 int size = 6; | |
113 for (int i = 0; i < size; i++) { | |
114 AnalysisTarget target = new TestSource("/test$i.dart"); | |
115 CacheEntry entry = new CacheEntry(); | |
116 entry.setValue(result, node, TargetedResult.EMPTY_LIST, null); | |
117 cache.put(target, entry); | |
118 cache.accessedAst(target); | |
119 } | |
120 | |
121 void _assertNonFlushedCount(int expectedCount, AnalysisCache cache) { | |
122 int nonFlushedCount = 0; | |
123 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); | |
124 while (iterator.moveNext()) { | |
125 if (iterator.value.getState(result) != CacheState.FLUSHED) { | |
126 nonFlushedCount++; | |
127 } | |
128 } | |
129 expect(nonFlushedCount, expectedCount); | |
130 } | |
131 | |
132 _assertNonFlushedCount(size, cache); | |
133 int newSize = size - 2; | |
134 partition.maxCacheSize = newSize; | |
135 _assertNonFlushedCount(newSize, cache); | |
136 } | |
137 | |
138 void test_size() { | 89 void test_size() { |
139 AnalysisCache cache = createCache(); | 90 AnalysisCache cache = createCache(); |
140 int size = 4; | 91 int size = 4; |
141 for (int i = 0; i < size; i++) { | 92 for (int i = 0; i < size; i++) { |
142 AnalysisTarget target = new TestSource("/test$i.dart"); | 93 AnalysisTarget target = new TestSource("/test$i.dart"); |
143 cache.put(target, new CacheEntry()); | 94 cache.put(target, new CacheEntry()); |
144 } | 95 } |
145 expect(cache.size(), size); | 96 expect(cache.size(), size); |
146 } | 97 } |
147 } | 98 } |
148 | 99 |
149 @reflectiveTest | 100 @reflectiveTest |
150 class CacheEntryTest extends EngineTestCase { | 101 class CacheEntryTest extends EngineTestCase { |
| 102 InternalAnalysisContext context; |
| 103 AnalysisCache cache; |
| 104 |
| 105 void setUp() { |
| 106 context = new _InternalAnalysisContextMock(); |
| 107 when(context.priorityTargets).thenReturn([]); |
| 108 cache = createCache(context: context); |
| 109 // when(context.analysisCache).thenReturn(cache); |
| 110 } |
| 111 |
151 test_explicitlyAdded() { | 112 test_explicitlyAdded() { |
152 CacheEntry entry = new CacheEntry(); | 113 CacheEntry entry = new CacheEntry(); |
153 expect(entry.explicitlyAdded, false); | 114 expect(entry.explicitlyAdded, false); |
154 entry.explicitlyAdded = true; | 115 entry.explicitlyAdded = true; |
155 expect(entry.explicitlyAdded, true); | 116 expect(entry.explicitlyAdded, true); |
156 } | 117 } |
157 | 118 |
158 test_fixExceptionState_error_exception() { | 119 test_fixExceptionState_error_exception() { |
159 ResultDescriptor result = new ResultDescriptor('test', null); | 120 ResultDescriptor result = new ResultDescriptor('test', null); |
160 CaughtException exception = new CaughtException(null, null); | 121 CaughtException exception = new CaughtException(null, null); |
(...skipping 18 matching lines...) Expand all Loading... |
179 } | 140 } |
180 | 141 |
181 test_fixExceptionState_noError_noException() { | 142 test_fixExceptionState_noError_noException() { |
182 ResultDescriptor result = new ResultDescriptor('test', null); | 143 ResultDescriptor result = new ResultDescriptor('test', null); |
183 CacheEntry entry = new CacheEntry(); | 144 CacheEntry entry = new CacheEntry(); |
184 entry.fixExceptionState(); | 145 entry.fixExceptionState(); |
185 expect(entry.getState(result), CacheState.INVALID); | 146 expect(entry.getState(result), CacheState.INVALID); |
186 expect(entry.exception, isNull); | 147 expect(entry.exception, isNull); |
187 } | 148 } |
188 | 149 |
189 test_flushAstStructures() { | 150 test_getMemento_noResult() { |
190 ResultDescriptor result = new ResultDescriptor('test', null); | 151 String defaultValue = 'value'; |
| 152 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
191 CacheEntry entry = new CacheEntry(); | 153 CacheEntry entry = new CacheEntry(); |
192 entry.setValue( | 154 expect(entry.getMemento(result), null); |
193 result, new NullLiteral(null), TargetedResult.EMPTY_LIST, null); | |
194 expect(entry.hasAstStructure, true); | |
195 entry.flushAstStructures(); | |
196 expect(entry.hasAstStructure, false); | |
197 } | 155 } |
198 | 156 |
199 test_getState() { | 157 test_getState() { |
200 ResultDescriptor result = new ResultDescriptor('test', null); | 158 ResultDescriptor result = new ResultDescriptor('test', null); |
201 CacheEntry entry = new CacheEntry(); | 159 CacheEntry entry = new CacheEntry(); |
202 expect(entry.getState(result), CacheState.INVALID); | 160 expect(entry.getState(result), CacheState.INVALID); |
203 } | 161 } |
204 | 162 |
205 test_getValue() { | 163 test_getValue() { |
206 String defaultValue = 'value'; | 164 String defaultValue = 'value'; |
207 ResultDescriptor result = new ResultDescriptor('test', defaultValue); | 165 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
208 CacheEntry entry = new CacheEntry(); | 166 CacheEntry entry = new CacheEntry(); |
209 expect(entry.getValue(result), defaultValue); | 167 expect(entry.getValue(result), defaultValue); |
210 } | 168 } |
211 | 169 |
212 test_getMemento_noResult() { | 170 test_getValue_flushResults() { |
213 String defaultValue = 'value'; | 171 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
214 ResultDescriptor result = new ResultDescriptor('test', defaultValue); | 172 ResultDescriptor descriptor1 = |
| 173 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 174 ResultDescriptor descriptor2 = |
| 175 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 176 ResultDescriptor descriptor3 = |
| 177 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
| 178 AnalysisTarget target = new TestSource(); |
215 CacheEntry entry = new CacheEntry(); | 179 CacheEntry entry = new CacheEntry(); |
216 expect(entry.getMemento(result), null); | 180 cache.put(target, entry); |
217 } | 181 { |
218 | 182 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST, null); |
219 test_hasAstStructure_false() { | 183 expect(entry.getState(descriptor1), CacheState.VALID); |
220 CacheEntry entry = new CacheEntry(); | 184 } |
221 expect(entry.hasAstStructure, false); | 185 { |
222 } | 186 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST, null); |
223 | 187 expect(entry.getState(descriptor1), CacheState.VALID); |
224 test_hasAstStructure_true() { | 188 expect(entry.getState(descriptor2), CacheState.VALID); |
225 ResultDescriptor result = new ResultDescriptor('test', null); | 189 } |
226 CacheEntry entry = new CacheEntry(); | 190 // get descriptor1, so that descriptor2 will be flushed |
227 entry.setValue( | 191 entry.getValue(descriptor1); |
228 result, new NullLiteral(null), TargetedResult.EMPTY_LIST, null); | 192 { |
229 expect(entry.hasAstStructure, true); | 193 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST, null); |
| 194 expect(entry.getState(descriptor1), CacheState.VALID); |
| 195 expect(entry.getState(descriptor2), CacheState.FLUSHED); |
| 196 expect(entry.getState(descriptor3), CacheState.VALID); |
| 197 } |
230 } | 198 } |
231 | 199 |
232 test_hasErrorState_false() { | 200 test_hasErrorState_false() { |
233 CacheEntry entry = new CacheEntry(); | 201 CacheEntry entry = new CacheEntry(); |
234 expect(entry.hasErrorState(), false); | 202 expect(entry.hasErrorState(), false); |
235 } | 203 } |
236 | 204 |
237 test_hasErrorState_true() { | 205 test_hasErrorState_true() { |
238 ResultDescriptor result = new ResultDescriptor('test', null); | 206 ResultDescriptor result = new ResultDescriptor('test', null); |
239 CaughtException exception = new CaughtException(null, null); | 207 CaughtException exception = new CaughtException(null, null); |
(...skipping 27 matching lines...) Expand all Loading... |
267 expect(entry.exception, exception); | 235 expect(entry.exception, exception); |
268 expect(entry.getState(result1), CacheState.ERROR); | 236 expect(entry.getState(result1), CacheState.ERROR); |
269 expect(entry.getState(result2), CacheState.ERROR); | 237 expect(entry.getState(result2), CacheState.ERROR); |
270 expect(entry.getState(result3), CacheState.VALID); | 238 expect(entry.getState(result3), CacheState.VALID); |
271 expect(entry.getValue(result1), 1); | 239 expect(entry.getValue(result1), 1); |
272 expect(entry.getValue(result2), 2); | 240 expect(entry.getValue(result2), 2); |
273 expect(entry.getValue(result3), 30); | 241 expect(entry.getValue(result3), 30); |
274 } | 242 } |
275 | 243 |
276 test_setErrorState_invalidateDependent() { | 244 test_setErrorState_invalidateDependent() { |
277 AnalysisCache cache = createCache(); | |
278 AnalysisTarget target = new TestSource(); | 245 AnalysisTarget target = new TestSource(); |
279 CacheEntry entry = new CacheEntry(); | 246 CacheEntry entry = new CacheEntry(); |
280 cache.put(target, entry); | 247 cache.put(target, entry); |
281 ResultDescriptor result1 = new ResultDescriptor('result1', -1); | 248 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
282 ResultDescriptor result2 = new ResultDescriptor('result2', -2); | 249 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
283 ResultDescriptor result3 = new ResultDescriptor('result3', -3); | 250 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
284 ResultDescriptor result4 = new ResultDescriptor('result4', -4); | 251 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
285 // set results, all of them are VALID | 252 // set results, all of them are VALID |
286 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); | 253 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); |
287 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); | 254 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
381 expect(entry.getState(result), CacheState.VALID); | 348 expect(entry.getState(result), CacheState.VALID); |
382 expect(entry.getValue(result), 10); | 349 expect(entry.getValue(result), 10); |
383 // set INVALID | 350 // set INVALID |
384 entry.setState(result, CacheState.INVALID); | 351 entry.setState(result, CacheState.INVALID); |
385 expect(entry.getState(result), CacheState.INVALID); | 352 expect(entry.getState(result), CacheState.INVALID); |
386 expect(entry.getValue(result), 1); | 353 expect(entry.getValue(result), 1); |
387 expect(entry.getMemento(result), memento); | 354 expect(entry.getMemento(result), memento); |
388 } | 355 } |
389 | 356 |
390 test_setState_invalid_invalidateDependent() { | 357 test_setState_invalid_invalidateDependent() { |
391 AnalysisCache cache = createCache(); | |
392 AnalysisTarget target = new TestSource(); | 358 AnalysisTarget target = new TestSource(); |
393 CacheEntry entry = new CacheEntry(); | 359 CacheEntry entry = new CacheEntry(); |
394 cache.put(target, entry); | 360 cache.put(target, entry); |
395 ResultDescriptor result1 = new ResultDescriptor('result1', -1); | 361 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
396 ResultDescriptor result2 = new ResultDescriptor('result2', -2); | 362 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
397 ResultDescriptor result3 = new ResultDescriptor('result3', -3); | 363 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
398 ResultDescriptor result4 = new ResultDescriptor('result4', -4); | 364 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
399 // set results, all of them are VALID | 365 // set results, all of them are VALID |
400 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); | 366 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); |
401 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); | 367 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); |
(...skipping 29 matching lines...) Expand all Loading... |
431 ResultDescriptor result = new ResultDescriptor('test', null); | 397 ResultDescriptor result = new ResultDescriptor('test', null); |
432 String value = 'value'; | 398 String value = 'value'; |
433 String memento = 'main() {}'; | 399 String memento = 'main() {}'; |
434 CacheEntry entry = new CacheEntry(); | 400 CacheEntry entry = new CacheEntry(); |
435 entry.setValue(result, value, TargetedResult.EMPTY_LIST, memento); | 401 entry.setValue(result, value, TargetedResult.EMPTY_LIST, memento); |
436 expect(entry.getState(result), CacheState.VALID); | 402 expect(entry.getState(result), CacheState.VALID); |
437 expect(entry.getValue(result), value); | 403 expect(entry.getValue(result), value); |
438 expect(entry.getMemento(result), memento); | 404 expect(entry.getMemento(result), memento); |
439 } | 405 } |
440 | 406 |
441 test_setValue_invalidateDependent() { | 407 test_setValue_flushResults() { |
442 AnalysisCache cache = createCache(); | 408 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
| 409 ResultDescriptor descriptor1 = |
| 410 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 411 ResultDescriptor descriptor2 = |
| 412 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 413 ResultDescriptor descriptor3 = |
| 414 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
443 AnalysisTarget target = new TestSource(); | 415 AnalysisTarget target = new TestSource(); |
444 CacheEntry entry = new CacheEntry(); | 416 CacheEntry entry = new CacheEntry(); |
445 cache.put(target, entry); | 417 cache.put(target, entry); |
| 418 { |
| 419 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST, null); |
| 420 expect(entry.getState(descriptor1), CacheState.VALID); |
| 421 } |
| 422 { |
| 423 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST, null); |
| 424 expect(entry.getState(descriptor1), CacheState.VALID); |
| 425 expect(entry.getState(descriptor2), CacheState.VALID); |
| 426 } |
| 427 { |
| 428 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST, null); |
| 429 expect(entry.getState(descriptor1), CacheState.FLUSHED); |
| 430 expect(entry.getState(descriptor2), CacheState.VALID); |
| 431 expect(entry.getState(descriptor3), CacheState.VALID); |
| 432 } |
| 433 } |
| 434 |
| 435 test_setValue_invalidateDependent() { |
| 436 AnalysisTarget target = new TestSource(); |
| 437 CacheEntry entry = new CacheEntry(); |
| 438 cache.put(target, entry); |
446 ResultDescriptor result1 = new ResultDescriptor('result1', -1); | 439 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
447 ResultDescriptor result2 = new ResultDescriptor('result2', -2); | 440 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
448 ResultDescriptor result3 = new ResultDescriptor('result3', -3); | 441 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
449 ResultDescriptor result4 = new ResultDescriptor('result4', -4); | 442 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
450 // set results, all of them are VALID | 443 // set results, all of them are VALID |
451 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); | 444 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST, null); |
452 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); | 445 entry.setValue(result2, 222, [new TargetedResult(target, result1)], null); |
453 entry.setValue(result3, 333, [new TargetedResult(target, result2)], null); | 446 entry.setValue(result3, 333, [new TargetedResult(target, result2)], null); |
454 entry.setValue(result4, 444, [], null); | 447 entry.setValue(result4, 444, [], null); |
455 expect(entry.getState(result1), CacheState.VALID); | 448 expect(entry.getState(result1), CacheState.VALID); |
(...skipping 10 matching lines...) Expand all Loading... |
466 expect(entry.getState(result2), CacheState.INVALID); | 459 expect(entry.getState(result2), CacheState.INVALID); |
467 expect(entry.getState(result3), CacheState.INVALID); | 460 expect(entry.getState(result3), CacheState.INVALID); |
468 expect(entry.getState(result4), CacheState.VALID); | 461 expect(entry.getState(result4), CacheState.VALID); |
469 expect(entry.getValue(result1), 1111); | 462 expect(entry.getValue(result1), 1111); |
470 expect(entry.getValue(result2), -2); | 463 expect(entry.getValue(result2), -2); |
471 expect(entry.getValue(result3), -3); | 464 expect(entry.getValue(result3), -3); |
472 expect(entry.getValue(result4), 444); | 465 expect(entry.getValue(result4), 444); |
473 } | 466 } |
474 | 467 |
475 test_setValue_invalidateDependent2() { | 468 test_setValue_invalidateDependent2() { |
476 AnalysisCache cache = createCache(); | |
477 AnalysisTarget target1 = new TestSource('a'); | 469 AnalysisTarget target1 = new TestSource('a'); |
478 AnalysisTarget target2 = new TestSource('b'); | 470 AnalysisTarget target2 = new TestSource('b'); |
479 CacheEntry entry1 = new CacheEntry(); | 471 CacheEntry entry1 = new CacheEntry(); |
480 CacheEntry entry2 = new CacheEntry(); | 472 CacheEntry entry2 = new CacheEntry(); |
481 cache.put(target1, entry1); | 473 cache.put(target1, entry1); |
482 cache.put(target2, entry2); | 474 cache.put(target2, entry2); |
483 ResultDescriptor result1 = new ResultDescriptor('result1', -1); | 475 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
484 ResultDescriptor result2 = new ResultDescriptor('result2', -2); | 476 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
485 ResultDescriptor result3 = new ResultDescriptor('result3', -3); | 477 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
486 // set results, all of them are VALID | 478 // set results, all of them are VALID |
(...skipping 23 matching lines...) Expand all Loading... |
510 | 502 |
511 test_toString_nonEmpty() { | 503 test_toString_nonEmpty() { |
512 String value = 'value'; | 504 String value = 'value'; |
513 ResultDescriptor result = new ResultDescriptor('test', null); | 505 ResultDescriptor result = new ResultDescriptor('test', null); |
514 CacheEntry entry = new CacheEntry(); | 506 CacheEntry entry = new CacheEntry(); |
515 entry.setValue(result, value, TargetedResult.EMPTY_LIST, null); | 507 entry.setValue(result, value, TargetedResult.EMPTY_LIST, null); |
516 expect(entry.toString(), isNotNull); | 508 expect(entry.toString(), isNotNull); |
517 } | 509 } |
518 } | 510 } |
519 | 511 |
| 512 @reflectiveTest |
| 513 class CacheFlushManagerTest { |
| 514 CacheFlushManager manager = new CacheFlushManager( |
| 515 new SimpleResultCachingPolicy(15, 3), (AnalysisTarget target) => false); |
| 516 |
| 517 test_madeActive() { |
| 518 manager.madeActive(); |
| 519 expect(manager.maxSize, 15); |
| 520 } |
| 521 |
| 522 test_madeIdle() { |
| 523 manager.madeActive(); |
| 524 AnalysisTarget target = new TestSource(); |
| 525 // prepare TargetedResult(s) |
| 526 List<TargetedResult> results = <TargetedResult>[]; |
| 527 for (int i = 0; i < 15; i++) { |
| 528 ResultDescriptor descriptor = new ResultDescriptor('result$i', null); |
| 529 results.add(new TargetedResult(target, descriptor)); |
| 530 } |
| 531 // notify about storing TargetedResult(s) |
| 532 for (TargetedResult result in results) { |
| 533 manager.resultStored(result, null); |
| 534 } |
| 535 expect(manager.recentlyUsed, results); |
| 536 expect(manager.currentSize, 15); |
| 537 // make idle |
| 538 List<TargetedResult> resultsToFlush = manager.madeIdle(); |
| 539 expect(manager.maxSize, 3); |
| 540 expect(manager.recentlyUsed, results.skip(15 - 3)); |
| 541 expect(resultsToFlush, results.take(15 - 3)); |
| 542 } |
| 543 |
| 544 test_new() { |
| 545 expect(manager.maxActiveSize, 15); |
| 546 expect(manager.maxIdleSize, 3); |
| 547 expect(manager.maxSize, 3); |
| 548 expect(manager.currentSize, 0); |
| 549 expect(manager.recentlyUsed, isEmpty); |
| 550 } |
| 551 |
| 552 test_resultAccessed() { |
| 553 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 554 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 555 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 556 AnalysisTarget target = new TestSource(); |
| 557 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 558 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 559 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 560 manager.resultStored(result1, null); |
| 561 manager.resultStored(result2, null); |
| 562 manager.resultStored(result3, null); |
| 563 expect(manager.currentSize, 3); |
| 564 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 565 // access result2 |
| 566 manager.resultAccessed(result2); |
| 567 expect(manager.currentSize, 3); |
| 568 expect(manager.recentlyUsed, orderedEquals([result1, result3, result2])); |
| 569 } |
| 570 |
| 571 test_resultAccessed_noSuchResult() { |
| 572 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 573 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 574 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 575 AnalysisTarget target = new TestSource(); |
| 576 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 577 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 578 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 579 manager.resultStored(result1, null); |
| 580 manager.resultStored(result2, null); |
| 581 expect(manager.currentSize, 2); |
| 582 expect(manager.recentlyUsed, orderedEquals([result1, result2])); |
| 583 // access result3, no-op |
| 584 manager.resultAccessed(result3); |
| 585 expect(manager.currentSize, 2); |
| 586 expect(manager.recentlyUsed, orderedEquals([result1, result2])); |
| 587 } |
| 588 |
| 589 test_resultStored() { |
| 590 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 591 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 592 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 593 ResultDescriptor descriptor4 = new ResultDescriptor('result4', null); |
| 594 AnalysisTarget target = new TestSource(); |
| 595 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 596 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 597 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 598 TargetedResult result4 = new TargetedResult(target, descriptor4); |
| 599 manager.resultStored(result1, null); |
| 600 manager.resultStored(result2, null); |
| 601 manager.resultStored(result3, null); |
| 602 expect(manager.currentSize, 3); |
| 603 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 604 // store result2 again |
| 605 { |
| 606 List<TargetedResult> resultsToFlush = manager.resultStored(result2, null); |
| 607 expect(resultsToFlush, isEmpty); |
| 608 expect(manager.currentSize, 3); |
| 609 expect(manager.recentlyUsed, orderedEquals([result1, result3, result2])); |
| 610 } |
| 611 // store result4 |
| 612 { |
| 613 List<TargetedResult> resultsToFlush = manager.resultStored(result4, null); |
| 614 expect(resultsToFlush, [result1]); |
| 615 expect(manager.currentSize, 3); |
| 616 expect(manager.recentlyUsed, orderedEquals([result3, result2, result4])); |
| 617 expect(manager.resultSizeMap, {result3: 1, result2: 1, result4: 1}); |
| 618 } |
| 619 } |
| 620 |
| 621 test_targetRemoved() { |
| 622 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 623 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 624 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 625 AnalysisTarget target1 = new TestSource('a.dart'); |
| 626 AnalysisTarget target2 = new TestSource('b.dart'); |
| 627 TargetedResult result1 = new TargetedResult(target1, descriptor1); |
| 628 TargetedResult result2 = new TargetedResult(target2, descriptor2); |
| 629 TargetedResult result3 = new TargetedResult(target1, descriptor3); |
| 630 manager.resultStored(result1, null); |
| 631 manager.resultStored(result2, null); |
| 632 manager.resultStored(result3, null); |
| 633 expect(manager.currentSize, 3); |
| 634 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 635 expect(manager.resultSizeMap, {result1: 1, result2: 1, result3: 1}); |
| 636 // remove target1 |
| 637 { |
| 638 manager.targetRemoved(target1); |
| 639 expect(manager.currentSize, 1); |
| 640 expect(manager.recentlyUsed, orderedEquals([result2])); |
| 641 expect(manager.resultSizeMap, {result2: 1}); |
| 642 } |
| 643 // remove target2 |
| 644 { |
| 645 manager.targetRemoved(target2); |
| 646 expect(manager.currentSize, 0); |
| 647 expect(manager.recentlyUsed, isEmpty); |
| 648 expect(manager.resultSizeMap, isEmpty); |
| 649 } |
| 650 } |
| 651 } |
| 652 |
520 abstract class CachePartitionTest extends EngineTestCase { | 653 abstract class CachePartitionTest extends EngineTestCase { |
521 CachePartition createPartition([CacheRetentionPolicy policy = null]); | 654 CachePartition createPartition(); |
522 | 655 |
523 void test_creation() { | 656 void test_creation() { |
524 expect(createPartition(), isNotNull); | 657 expect(createPartition(), isNotNull); |
525 } | 658 } |
526 | 659 |
527 void test_entrySet() { | 660 void test_entrySet() { |
528 CachePartition partition = createPartition(); | 661 CachePartition partition = createPartition(); |
529 AnalysisTarget target = new TestSource(); | 662 AnalysisTarget target = new TestSource(); |
530 CacheEntry entry = new CacheEntry(); | 663 CacheEntry entry = new CacheEntry(); |
531 partition.put(target, entry); | 664 partition.put(target, entry); |
(...skipping 11 matching lines...) Expand all Loading... |
543 } | 676 } |
544 | 677 |
545 void test_put_noFlush() { | 678 void test_put_noFlush() { |
546 CachePartition partition = createPartition(); | 679 CachePartition partition = createPartition(); |
547 AnalysisTarget target = new TestSource(); | 680 AnalysisTarget target = new TestSource(); |
548 CacheEntry entry = new CacheEntry(); | 681 CacheEntry entry = new CacheEntry(); |
549 partition.put(target, entry); | 682 partition.put(target, entry); |
550 expect(partition.get(target), entry); | 683 expect(partition.get(target), entry); |
551 } | 684 } |
552 | 685 |
| 686 void test_put_alreadyInPartition() { |
| 687 CachePartition partition1 = createPartition(); |
| 688 CachePartition partition2 = createPartition(); |
| 689 AnalysisTarget target = new TestSource(); |
| 690 CacheEntry entry = new CacheEntry(); |
| 691 partition1.put(target, entry); |
| 692 expect(() => partition2.put(target, entry), throwsStateError); |
| 693 } |
| 694 |
553 void test_remove() { | 695 void test_remove() { |
554 CachePartition partition = createPartition(); | 696 CachePartition partition = createPartition(); |
555 AnalysisTarget target = new TestSource(); | 697 AnalysisTarget target = new TestSource(); |
556 CacheEntry entry = new CacheEntry(); | 698 CacheEntry entry = new CacheEntry(); |
557 partition.put(target, entry); | 699 partition.put(target, entry); |
558 expect(partition.get(target), entry); | 700 expect(partition.get(target), entry); |
559 partition.remove(target); | 701 partition.remove(target); |
560 expect(partition.get(target), isNull); | 702 expect(partition.get(target), isNull); |
561 } | 703 } |
562 | |
563 void test_setMaxCacheSize() { | |
564 CachePartition partition = | |
565 createPartition(new TestCacheRetentionPolicy(RetentionPriority.LOW)); | |
566 ResultDescriptor result = new ResultDescriptor('result', null); | |
567 NullLiteral node = new NullLiteral(null); | |
568 int size = 6; // Must be <= partition.maxCacheSize | |
569 for (int i = 0; i < size; i++) { | |
570 AnalysisTarget target = new TestSource("/test$i.dart"); | |
571 CacheEntry entry = new CacheEntry(); | |
572 entry.setValue(result, node, TargetedResult.EMPTY_LIST, null); | |
573 partition.put(target, entry); | |
574 partition.accessedAst(target); | |
575 } | |
576 | |
577 void assertNonFlushedCount(int expectedCount, CachePartition partition) { | |
578 int nonFlushedCount = 0; | |
579 Map<AnalysisTarget, CacheEntry> entryMap = partition.map; | |
580 entryMap.values.forEach((CacheEntry entry) { | |
581 if (entry.getState(result) != CacheState.FLUSHED) { | |
582 nonFlushedCount++; | |
583 } | |
584 }); | |
585 expect(nonFlushedCount, expectedCount); | |
586 } | |
587 | |
588 assertNonFlushedCount(size, partition); | |
589 int newSize = size - 2; | |
590 partition.maxCacheSize = newSize; | |
591 assertNonFlushedCount(newSize, partition); | |
592 } | |
593 | |
594 void test_size() { | |
595 CachePartition partition = createPartition(); | |
596 int size = 4; | |
597 for (int i = 0; i < size; i++) { | |
598 AnalysisTarget target = new TestSource("/test$i.dart"); | |
599 partition.put(target, new CacheEntry()); | |
600 partition.accessedAst(target); | |
601 } | |
602 expect(partition.size(), size); | |
603 } | |
604 } | 704 } |
605 | 705 |
606 @reflectiveTest | 706 @reflectiveTest |
607 class ResultDataTest extends EngineTestCase { | 707 class ResultDataTest extends EngineTestCase { |
608 test_creation() { | 708 test_creation() { |
609 String value = 'value'; | 709 String value = 'value'; |
610 ResultData data = new ResultData(new ResultDescriptor('test', value)); | 710 ResultData data = new ResultData(new ResultDescriptor('test', value)); |
611 expect(data, isNotNull); | 711 expect(data, isNotNull); |
612 expect(data.state, CacheState.INVALID); | 712 expect(data.state, CacheState.INVALID); |
613 expect(data.value, value); | 713 expect(data.value, value); |
614 } | 714 } |
615 } | 715 } |
616 | 716 |
617 @reflectiveTest | 717 @reflectiveTest |
618 class SdkCachePartitionTest extends CachePartitionTest { | 718 class SdkCachePartitionTest extends CachePartitionTest { |
619 CachePartition createPartition([CacheRetentionPolicy policy = null]) { | 719 CachePartition createPartition() { |
620 return new SdkCachePartition(null, 8); | 720 return new SdkCachePartition(null); |
621 } | 721 } |
622 | 722 |
623 void test_contains_false() { | 723 void test_contains_false() { |
624 CachePartition partition = createPartition(); | 724 CachePartition partition = createPartition(); |
625 AnalysisTarget target = new TestSource(); | 725 AnalysisTarget target = new TestSource(); |
626 expect(partition.contains(target), isFalse); | 726 expect(partition.contains(target), isFalse); |
627 } | 727 } |
628 | 728 |
629 void test_contains_true() { | 729 void test_contains_true() { |
630 SdkCachePartition partition = new SdkCachePartition(null, 8); | 730 SdkCachePartition partition = new SdkCachePartition(null); |
631 SourceFactory factory = new SourceFactory( | 731 SourceFactory factory = new SourceFactory( |
632 [new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)]); | 732 [new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)]); |
633 AnalysisTarget target = factory.forUri("dart:core"); | 733 AnalysisTarget target = factory.forUri("dart:core"); |
634 expect(partition.contains(target), isTrue); | 734 expect(partition.contains(target), isTrue); |
635 } | 735 } |
636 } | 736 } |
637 | 737 |
638 @reflectiveTest | 738 @reflectiveTest |
639 class TestCacheRetentionPolicy extends CacheRetentionPolicy { | |
640 final RetentionPriority policy; | |
641 | |
642 TestCacheRetentionPolicy([this.policy = RetentionPriority.MEDIUM]); | |
643 | |
644 @override | |
645 RetentionPriority getAstPriority(AnalysisTarget target, CacheEntry entry) => | |
646 policy; | |
647 } | |
648 | |
649 @reflectiveTest | |
650 class UniversalCachePartitionTest extends CachePartitionTest { | 739 class UniversalCachePartitionTest extends CachePartitionTest { |
651 CachePartition createPartition([CacheRetentionPolicy policy = null]) { | 740 CachePartition createPartition() { |
652 return new UniversalCachePartition(null, 8, policy); | 741 return new UniversalCachePartition(null); |
653 } | 742 } |
654 | 743 |
655 void test_contains() { | 744 void test_contains() { |
656 UniversalCachePartition partition = | 745 UniversalCachePartition partition = new UniversalCachePartition(null); |
657 new UniversalCachePartition(null, 8, null); | |
658 TestSource source = new TestSource(); | 746 TestSource source = new TestSource(); |
659 expect(partition.contains(source), isTrue); | 747 expect(partition.contains(source), isTrue); |
660 } | 748 } |
661 } | 749 } |
| 750 |
| 751 class _InternalAnalysisContextMock extends TypedMock |
| 752 implements InternalAnalysisContext { |
| 753 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 754 } |
OLD | NEW |