OLD | NEW |
(Empty) | |
| 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 |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library test.src.task.driver_test; |
| 6 |
| 7 import 'package:analyzer/src/context/cache.dart'; |
| 8 import 'package:analyzer/src/generated/engine.dart' |
| 9 show |
| 10 AnalysisContext, |
| 11 CacheState, |
| 12 InternalAnalysisContext, |
| 13 RetentionPriority; |
| 14 import 'package:analyzer/src/generated/java_engine.dart'; |
| 15 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 16 import 'package:analyzer/src/generated/source.dart'; |
| 17 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 18 import 'package:analyzer/src/task/model.dart'; |
| 19 import 'package:analyzer/task/model.dart'; |
| 20 import 'package:typed_mock/typed_mock.dart'; |
| 21 import 'package:unittest/unittest.dart'; |
| 22 |
| 23 import '../../generated/test_support.dart'; |
| 24 import '../../reflective_tests.dart'; |
| 25 import '../../utils.dart'; |
| 26 |
| 27 main() { |
| 28 initializeTestEnvironment(); |
| 29 runReflectiveTests(AnalysisCacheTest); |
| 30 runReflectiveTests(CacheEntryTest); |
| 31 runReflectiveTests(CacheFlushManagerTest); |
| 32 runReflectiveTests(SdkCachePartitionTest); |
| 33 runReflectiveTests(UniversalCachePartitionTest); |
| 34 runReflectiveTests(ResultDataTest); |
| 35 } |
| 36 |
| 37 AnalysisCache createCache( |
| 38 {AnalysisContext context, |
| 39 RetentionPriority policy: RetentionPriority.LOW}) { |
| 40 CachePartition partition = new UniversalCachePartition(context); |
| 41 return new AnalysisCache(<CachePartition>[partition]); |
| 42 } |
| 43 |
| 44 class AbstractCacheTest { |
| 45 InternalAnalysisContext context; |
| 46 AnalysisCache cache; |
| 47 |
| 48 void setUp() { |
| 49 context = new _InternalAnalysisContextMock(); |
| 50 when(context.priorityTargets).thenReturn([]); |
| 51 cache = createCache(context: context); |
| 52 when(context.analysisCache).thenReturn(cache); |
| 53 } |
| 54 } |
| 55 |
| 56 @reflectiveTest |
| 57 class AnalysisCacheTest extends AbstractCacheTest { |
| 58 void test_creation() { |
| 59 expect(cache, isNotNull); |
| 60 } |
| 61 |
| 62 void test_get() { |
| 63 AnalysisTarget target = new TestSource(); |
| 64 expect(cache.get(target), isNull); |
| 65 } |
| 66 |
| 67 void test_getContextFor() { |
| 68 AnalysisTarget target = new TestSource(); |
| 69 expect(cache.getContextFor(target), context); |
| 70 } |
| 71 |
| 72 void test_getSourcesWithFullName() { |
| 73 String filePath = '/foo/lib/file.dart'; |
| 74 // no sources |
| 75 expect(cache.getSourcesWithFullName(filePath), isEmpty); |
| 76 // add source1 |
| 77 TestSourceWithUri source1 = |
| 78 new TestSourceWithUri(filePath, Uri.parse('file://$filePath')); |
| 79 cache.put(new CacheEntry(source1)); |
| 80 expect(cache.getSourcesWithFullName(filePath), unorderedEquals([source1])); |
| 81 // add source2 |
| 82 TestSourceWithUri source2 = |
| 83 new TestSourceWithUri(filePath, Uri.parse('package:foo/file.dart')); |
| 84 cache.put(new CacheEntry(source2)); |
| 85 expect(cache.getSourcesWithFullName(filePath), |
| 86 unorderedEquals([source1, source2])); |
| 87 // remove source1 |
| 88 cache.remove(source1); |
| 89 expect(cache.getSourcesWithFullName(filePath), unorderedEquals([source2])); |
| 90 // remove source2 |
| 91 cache.remove(source2); |
| 92 expect(cache.getSourcesWithFullName(filePath), isEmpty); |
| 93 // ignored |
| 94 cache.remove(source1); |
| 95 cache.remove(source2); |
| 96 expect(cache.getSourcesWithFullName(filePath), isEmpty); |
| 97 } |
| 98 |
| 99 void test_getState_hasEntry_flushed() { |
| 100 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 101 AnalysisTarget target = new TestSource(); |
| 102 CacheEntry entry = new CacheEntry(target); |
| 103 cache.put(entry); |
| 104 entry.setState(result, CacheState.FLUSHED); |
| 105 expect(cache.getState(target, result), CacheState.FLUSHED); |
| 106 } |
| 107 |
| 108 void test_getState_hasEntry_valid() { |
| 109 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 110 AnalysisTarget target = new TestSource(); |
| 111 CacheEntry entry = new CacheEntry(target); |
| 112 cache.put(entry); |
| 113 entry.setValue(result, '', []); |
| 114 expect(cache.getState(target, result), CacheState.VALID); |
| 115 } |
| 116 |
| 117 void test_getState_noEntry() { |
| 118 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 119 AnalysisTarget target = new TestSource(); |
| 120 expect(cache.getState(target, result), CacheState.INVALID); |
| 121 } |
| 122 |
| 123 void test_getValue_hasEntry_valid() { |
| 124 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 125 AnalysisTarget target = new TestSource(); |
| 126 CacheEntry entry = new CacheEntry(target); |
| 127 cache.put(entry); |
| 128 entry.setValue(result, 111, []); |
| 129 expect(cache.getValue(target, result), 111); |
| 130 } |
| 131 |
| 132 void test_getValue_noEntry() { |
| 133 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 134 AnalysisTarget target = new TestSource(); |
| 135 expect(cache.getValue(target, result), -1); |
| 136 } |
| 137 |
| 138 void test_iterator() { |
| 139 AnalysisTarget target = new TestSource(); |
| 140 CacheEntry entry = new CacheEntry(target); |
| 141 cache.put(entry); |
| 142 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); |
| 143 expect(iterator.moveNext(), isTrue); |
| 144 expect(iterator.key, same(target)); |
| 145 expect(iterator.value, same(entry)); |
| 146 expect(iterator.moveNext(), isFalse); |
| 147 } |
| 148 |
| 149 void test_put() { |
| 150 AnalysisTarget target = new TestSource(); |
| 151 CacheEntry entry = new CacheEntry(target); |
| 152 expect(cache.get(target), isNull); |
| 153 cache.put(entry); |
| 154 expect(cache.get(target), entry); |
| 155 } |
| 156 |
| 157 void test_remove() { |
| 158 AnalysisTarget target1 = new TestSource('/a.dart'); |
| 159 AnalysisTarget target2 = new TestSource('/b.dart'); |
| 160 AnalysisTarget target3 = new TestSource('/c.dart'); |
| 161 CacheEntry entry1 = new CacheEntry(target1); |
| 162 CacheEntry entry2 = new CacheEntry(target2); |
| 163 CacheEntry entry3 = new CacheEntry(target3); |
| 164 cache.put(entry1); |
| 165 cache.put(entry2); |
| 166 cache.put(entry3); |
| 167 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 168 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 169 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 170 // set results, all of them are VALID |
| 171 entry1.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 172 entry2.setValue(result2, 222, [new TargetedResult(target1, result1)]); |
| 173 entry3.setValue(result3, 333, []); |
| 174 expect(entry1.getState(result1), CacheState.VALID); |
| 175 expect(entry2.getState(result2), CacheState.VALID); |
| 176 expect(entry3.getState(result3), CacheState.VALID); |
| 177 expect(entry1.getValue(result1), 111); |
| 178 expect(entry2.getValue(result2), 222); |
| 179 expect(entry3.getValue(result3), 333); |
| 180 // remove entry1, invalidate result2 and remove empty entry2 |
| 181 expect(cache.remove(target1), entry1); |
| 182 expect(cache.get(target1), isNull); |
| 183 expect(cache.get(target2), isNull); |
| 184 expect(cache.get(target3), entry3); |
| 185 expect(entry3.getState(result3), CacheState.VALID); |
| 186 } |
| 187 |
| 188 void test_remove_invalidateResults_sameTarget() { |
| 189 AnalysisTarget target = new TestSource('/a.dart'); |
| 190 CacheEntry entry = new CacheEntry(target); |
| 191 cache.put(entry); |
| 192 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 193 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 194 // set results, all of them are VALID |
| 195 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 196 entry.setValue(result2, 222, [new TargetedResult(target, result1)]); |
| 197 expect(entry.getState(result1), CacheState.VALID); |
| 198 expect(entry.getState(result2), CacheState.VALID); |
| 199 expect(entry.getValue(result1), 111); |
| 200 expect(entry.getValue(result2), 222); |
| 201 // remove target, invalidate result2 |
| 202 expect(cache.remove(target), entry); |
| 203 expect(cache.get(target), isNull); |
| 204 expect(entry.getState(result2), CacheState.INVALID); |
| 205 } |
| 206 |
| 207 void test_size() { |
| 208 int size = 4; |
| 209 for (int i = 0; i < size; i++) { |
| 210 AnalysisTarget target = new TestSource("/test$i.dart"); |
| 211 cache.put(new CacheEntry(target)); |
| 212 } |
| 213 expect(cache.size(), size); |
| 214 } |
| 215 |
| 216 void test_sources() { |
| 217 AnalysisTarget source1 = new TestSource('1.dart'); |
| 218 AnalysisTarget source2 = new TestSource('2.dart'); |
| 219 AnalysisTarget target1 = new _TestAnalysisTarget(); |
| 220 // no entries |
| 221 expect(cache.sources, isEmpty); |
| 222 // add source1 |
| 223 cache.put(new CacheEntry(source1)); |
| 224 expect(cache.sources, unorderedEquals([source1])); |
| 225 // add target1 |
| 226 cache.put(new CacheEntry(target1)); |
| 227 expect(cache.sources, unorderedEquals([source1])); |
| 228 // add source2 |
| 229 cache.put(new CacheEntry(source2)); |
| 230 expect(cache.sources, unorderedEquals([source1, source2])); |
| 231 // remove source1 |
| 232 cache.remove(source1); |
| 233 expect(cache.sources, unorderedEquals([source2])); |
| 234 // remove source2 |
| 235 cache.remove(source2); |
| 236 expect(cache.sources, isEmpty); |
| 237 } |
| 238 } |
| 239 |
| 240 @reflectiveTest |
| 241 class CacheEntryTest extends AbstractCacheTest { |
| 242 test_dispose() { |
| 243 ResultDescriptor descriptor1 = new ResultDescriptor('result1', -1); |
| 244 ResultDescriptor descriptor2 = new ResultDescriptor('result2', -2); |
| 245 AnalysisTarget target1 = new TestSource('1.dart'); |
| 246 AnalysisTarget target2 = new TestSource('2.dart'); |
| 247 TargetedResult result1 = new TargetedResult(target1, descriptor1); |
| 248 TargetedResult result2 = new TargetedResult(target2, descriptor2); |
| 249 CacheEntry entry1 = new CacheEntry(target1); |
| 250 CacheEntry entry2 = new CacheEntry(target2); |
| 251 cache.put(entry1); |
| 252 cache.put(entry2); |
| 253 entry1.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST); |
| 254 entry2.setValue(descriptor2, 2, <TargetedResult>[result1]); |
| 255 // target2 is listed as dependent in target1 |
| 256 expect( |
| 257 entry1.getResultData(descriptor1).dependentResults, contains(result2)); |
| 258 // dispose entry2, result2 is removed from result1 |
| 259 entry2.dispose(); |
| 260 expect(entry1.getResultData(descriptor1).dependentResults, isEmpty); |
| 261 } |
| 262 |
| 263 test_explicitlyAdded() { |
| 264 AnalysisTarget target = new TestSource(); |
| 265 CacheEntry entry = new CacheEntry(target); |
| 266 expect(entry.explicitlyAdded, false); |
| 267 entry.explicitlyAdded = true; |
| 268 expect(entry.explicitlyAdded, true); |
| 269 } |
| 270 |
| 271 test_fixExceptionState_error_exception() { |
| 272 AnalysisTarget target = new TestSource(); |
| 273 ResultDescriptor result = new ResultDescriptor('test', null); |
| 274 CaughtException exception = new CaughtException(null, null); |
| 275 CacheEntry entry = new CacheEntry(target); |
| 276 entry.setErrorState(exception, <ResultDescriptor>[result]); |
| 277 entry.fixExceptionState(); |
| 278 expect(entry.getState(result), CacheState.ERROR); |
| 279 expect(entry.exception, exception); |
| 280 } |
| 281 |
| 282 test_fixExceptionState_noError_exception() { |
| 283 AnalysisTarget target = new TestSource(); |
| 284 ResultDescriptor result = new ResultDescriptor('test', null); |
| 285 CacheEntry entry = new CacheEntry(target); |
| 286 cache.put(entry); |
| 287 // set one result to ERROR |
| 288 CaughtException exception = new CaughtException(null, null); |
| 289 entry.setErrorState(exception, <ResultDescriptor>[result]); |
| 290 // set the same result to VALID |
| 291 entry.setValue(result, 1, TargetedResult.EMPTY_LIST); |
| 292 // fix the exception state |
| 293 entry.fixExceptionState(); |
| 294 expect(entry.exception, isNull); |
| 295 } |
| 296 |
| 297 test_fixExceptionState_noError_noException() { |
| 298 AnalysisTarget target = new TestSource(); |
| 299 ResultDescriptor result = new ResultDescriptor('test', null); |
| 300 CacheEntry entry = new CacheEntry(target); |
| 301 entry.fixExceptionState(); |
| 302 expect(entry.getState(result), CacheState.INVALID); |
| 303 expect(entry.exception, isNull); |
| 304 } |
| 305 |
| 306 test_getState() { |
| 307 AnalysisTarget target = new TestSource(); |
| 308 ResultDescriptor result = new ResultDescriptor('test', null); |
| 309 CacheEntry entry = new CacheEntry(target); |
| 310 expect(entry.getState(result), CacheState.INVALID); |
| 311 } |
| 312 |
| 313 test_getValue_default() { |
| 314 AnalysisTarget target = new TestSource(); |
| 315 String defaultValue = 'value'; |
| 316 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
| 317 CacheEntry entry = new CacheEntry(target); |
| 318 expect(entry.getValue(result), defaultValue); |
| 319 } |
| 320 |
| 321 test_getValue_flushResults() { |
| 322 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
| 323 ResultDescriptor descriptor1 = |
| 324 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 325 ResultDescriptor descriptor2 = |
| 326 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 327 ResultDescriptor descriptor3 = |
| 328 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
| 329 AnalysisTarget target = new TestSource(); |
| 330 CacheEntry entry = new CacheEntry(target); |
| 331 cache.put(entry); |
| 332 { |
| 333 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST); |
| 334 expect(entry.getState(descriptor1), CacheState.VALID); |
| 335 } |
| 336 { |
| 337 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST); |
| 338 expect(entry.getState(descriptor1), CacheState.VALID); |
| 339 expect(entry.getState(descriptor2), CacheState.VALID); |
| 340 } |
| 341 // get descriptor1, so that descriptor2 will be flushed |
| 342 entry.getValue(descriptor1); |
| 343 { |
| 344 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST); |
| 345 expect(entry.getState(descriptor1), CacheState.VALID); |
| 346 expect(entry.getState(descriptor2), CacheState.FLUSHED); |
| 347 expect(entry.getState(descriptor3), CacheState.VALID); |
| 348 } |
| 349 } |
| 350 |
| 351 test_hasErrorState_false() { |
| 352 AnalysisTarget target = new TestSource(); |
| 353 CacheEntry entry = new CacheEntry(target); |
| 354 expect(entry.hasErrorState(), false); |
| 355 } |
| 356 |
| 357 test_hasErrorState_true() { |
| 358 AnalysisTarget target = new TestSource(); |
| 359 ResultDescriptor result = new ResultDescriptor('test', null); |
| 360 CaughtException exception = new CaughtException(null, null); |
| 361 CacheEntry entry = new CacheEntry(target); |
| 362 entry.setErrorState(exception, <ResultDescriptor>[result]); |
| 363 expect(entry.hasErrorState(), true); |
| 364 } |
| 365 |
| 366 test_invalidateAllInformation() { |
| 367 AnalysisTarget target = new TestSource(); |
| 368 ResultDescriptor result = new ResultDescriptor('test', null); |
| 369 CacheEntry entry = new CacheEntry(target); |
| 370 entry.setValue(result, 'value', TargetedResult.EMPTY_LIST); |
| 371 entry.invalidateAllInformation(); |
| 372 expect(entry.getState(result), CacheState.INVALID); |
| 373 expect(entry.getValue(result), isNull); |
| 374 } |
| 375 |
| 376 test_setErrorState() { |
| 377 AnalysisTarget target = new TestSource(); |
| 378 ResultDescriptor result1 = new ResultDescriptor('res1', 1); |
| 379 ResultDescriptor result2 = new ResultDescriptor('res2', 2); |
| 380 ResultDescriptor result3 = new ResultDescriptor('res3', 3); |
| 381 // prepare some good state |
| 382 CacheEntry entry = new CacheEntry(target); |
| 383 entry.setValue(result1, 10, TargetedResult.EMPTY_LIST); |
| 384 entry.setValue(result2, 20, TargetedResult.EMPTY_LIST); |
| 385 entry.setValue(result3, 30, TargetedResult.EMPTY_LIST); |
| 386 // set error state |
| 387 CaughtException exception = new CaughtException(null, null); |
| 388 entry.setErrorState(exception, <ResultDescriptor>[result1, result2]); |
| 389 // verify |
| 390 expect(entry.exception, exception); |
| 391 expect(entry.getState(result1), CacheState.ERROR); |
| 392 expect(entry.getState(result2), CacheState.ERROR); |
| 393 expect(entry.getState(result3), CacheState.VALID); |
| 394 expect(entry.getValue(result1), 1); |
| 395 expect(entry.getValue(result2), 2); |
| 396 expect(entry.getValue(result3), 30); |
| 397 } |
| 398 |
| 399 test_setErrorState_invalidateDependent() { |
| 400 AnalysisTarget target1 = new TestSource('/a.dart'); |
| 401 AnalysisTarget target2 = new TestSource('/b.dart'); |
| 402 CacheEntry entry1 = new CacheEntry(target1); |
| 403 CacheEntry entry2 = new CacheEntry(target2); |
| 404 cache.put(entry1); |
| 405 cache.put(entry2); |
| 406 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 407 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 408 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 409 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
| 410 // set results, all of them are VALID |
| 411 entry1.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 412 entry2.setValue(result2, 222, [new TargetedResult(target1, result1)]); |
| 413 entry2.setValue(result3, 333, [new TargetedResult(target2, result2)]); |
| 414 entry2.setValue(result4, 444, []); |
| 415 expect(entry1.getState(result1), CacheState.VALID); |
| 416 expect(entry2.getState(result2), CacheState.VALID); |
| 417 expect(entry2.getState(result3), CacheState.VALID); |
| 418 expect(entry2.getState(result4), CacheState.VALID); |
| 419 expect(entry1.getValue(result1), 111); |
| 420 expect(entry2.getValue(result2), 222); |
| 421 expect(entry2.getValue(result3), 333); |
| 422 expect(entry2.getValue(result4), 444); |
| 423 // set error state |
| 424 CaughtException exception = new CaughtException(null, null); |
| 425 entry1.setErrorState(exception, <ResultDescriptor>[result1]); |
| 426 // result2 and result3 are invalidated, result4 is intact |
| 427 expect(entry1.getState(result1), CacheState.ERROR); |
| 428 expect(entry2.getState(result2), CacheState.ERROR); |
| 429 expect(entry2.getState(result3), CacheState.ERROR); |
| 430 expect(entry2.getState(result4), CacheState.VALID); |
| 431 expect(entry1.getValue(result1), -1); |
| 432 expect(entry2.getValue(result2), -2); |
| 433 expect(entry2.getValue(result3), -3); |
| 434 expect(entry2.getValue(result4), 444); |
| 435 expect(entry1.exception, exception); |
| 436 expect(entry2.exception, exception); |
| 437 } |
| 438 |
| 439 test_setErrorState_noDescriptors() { |
| 440 AnalysisTarget target = new TestSource(); |
| 441 CaughtException exception = new CaughtException(null, null); |
| 442 CacheEntry entry = new CacheEntry(target); |
| 443 expect(() { |
| 444 entry.setErrorState(exception, <ResultDescriptor>[]); |
| 445 }, throwsArgumentError); |
| 446 } |
| 447 |
| 448 test_setErrorState_noException() { |
| 449 AnalysisTarget target = new TestSource(); |
| 450 ResultDescriptor result = new ResultDescriptor('test', null); |
| 451 CacheEntry entry = new CacheEntry(target); |
| 452 expect(() { |
| 453 entry.setErrorState(null, <ResultDescriptor>[result]); |
| 454 }, throwsArgumentError); |
| 455 } |
| 456 |
| 457 test_setErrorState_nullDescriptors() { |
| 458 AnalysisTarget target = new TestSource(); |
| 459 CaughtException exception = new CaughtException(null, null); |
| 460 CacheEntry entry = new CacheEntry(target); |
| 461 expect(() { |
| 462 entry.setErrorState(exception, null); |
| 463 }, throwsArgumentError); |
| 464 } |
| 465 |
| 466 test_setState_error() { |
| 467 AnalysisTarget target = new TestSource(); |
| 468 ResultDescriptor result = new ResultDescriptor('test', null); |
| 469 CacheEntry entry = new CacheEntry(target); |
| 470 entry.setValue(result, 42, TargetedResult.EMPTY_LIST); |
| 471 // an invalid state change |
| 472 expect(() { |
| 473 entry.setState(result, CacheState.ERROR); |
| 474 }, throwsArgumentError); |
| 475 // no changes |
| 476 expect(entry.getState(result), CacheState.VALID); |
| 477 expect(entry.getValue(result), 42); |
| 478 } |
| 479 |
| 480 test_setState_flushed() { |
| 481 AnalysisTarget target = new TestSource(); |
| 482 ResultDescriptor result = new ResultDescriptor('test', 1); |
| 483 CacheEntry entry = new CacheEntry(target); |
| 484 // set VALID |
| 485 entry.setValue(result, 10, TargetedResult.EMPTY_LIST); |
| 486 expect(entry.getState(result), CacheState.VALID); |
| 487 expect(entry.getValue(result), 10); |
| 488 // set FLUSHED |
| 489 entry.setState(result, CacheState.FLUSHED); |
| 490 expect(entry.getState(result), CacheState.FLUSHED); |
| 491 expect(entry.getValue(result), 1); |
| 492 } |
| 493 |
| 494 test_setState_inProcess() { |
| 495 AnalysisTarget target = new TestSource(); |
| 496 ResultDescriptor result = new ResultDescriptor('test', 1); |
| 497 CacheEntry entry = new CacheEntry(target); |
| 498 // set VALID |
| 499 entry.setValue(result, 10, TargetedResult.EMPTY_LIST); |
| 500 expect(entry.getState(result), CacheState.VALID); |
| 501 expect(entry.getValue(result), 10); |
| 502 // set IN_PROCESS |
| 503 entry.setState(result, CacheState.IN_PROCESS); |
| 504 expect(entry.getState(result), CacheState.IN_PROCESS); |
| 505 expect(entry.getValue(result), 10); |
| 506 } |
| 507 |
| 508 test_setState_invalid() { |
| 509 AnalysisTarget target = new TestSource(); |
| 510 ResultDescriptor result = new ResultDescriptor('test', 1); |
| 511 CacheEntry entry = new CacheEntry(target); |
| 512 cache.put(entry); |
| 513 // set VALID |
| 514 entry.setValue(result, 10, TargetedResult.EMPTY_LIST); |
| 515 expect(entry.getState(result), CacheState.VALID); |
| 516 expect(entry.getValue(result), 10); |
| 517 // listen, expect "result" invalidation event |
| 518 int numberOfEvents = 0; |
| 519 cache.onResultInvalidated.listen((event) { |
| 520 numberOfEvents++; |
| 521 expect(event.entry, same(entry)); |
| 522 expect(event.descriptor, same(result)); |
| 523 }); |
| 524 // set INVALID |
| 525 entry.setState(result, CacheState.INVALID); |
| 526 expect(entry.getState(result), CacheState.INVALID); |
| 527 expect(entry.getValue(result), 1); |
| 528 expect(numberOfEvents, 1); |
| 529 } |
| 530 |
| 531 test_setState_invalid_dependencyCycle() { |
| 532 AnalysisTarget target1 = new TestSource('/a.dart'); |
| 533 AnalysisTarget target2 = new TestSource('/b.dart'); |
| 534 CacheEntry entry1 = new CacheEntry(target1); |
| 535 CacheEntry entry2 = new CacheEntry(target2); |
| 536 cache.put(entry1); |
| 537 cache.put(entry2); |
| 538 ResultDescriptor result = new ResultDescriptor('result', -1); |
| 539 // Set each result as VALID with a dependency on on the other. |
| 540 entry1.setValue(result, 100, [new TargetedResult(target2, result)]); |
| 541 entry2.setValue(result, 200, [new TargetedResult(target1, result)]); |
| 542 expect(entry1.getState(result), CacheState.VALID); |
| 543 expect(entry2.getState(result), CacheState.VALID); |
| 544 // Listen, expect entry1.result and entry2.result invalidation events. |
| 545 int numberOfEvents = 0; |
| 546 bool wasEntry1 = false; |
| 547 bool wasEntry2 = false; |
| 548 cache.onResultInvalidated.listen((event) { |
| 549 numberOfEvents++; |
| 550 if (event.entry == entry1) wasEntry1 = true; |
| 551 if (event.entry == entry2) wasEntry2 = true; |
| 552 expect(event.descriptor, same(result)); |
| 553 }); |
| 554 // Invalidate entry1.result; this should cause entry2 to be also |
| 555 // cleared without going into an infinite regress. |
| 556 entry1.setState(result, CacheState.INVALID); |
| 557 expect(cache.get(target1), isNull); |
| 558 expect(cache.get(target2), isNull); |
| 559 expect(numberOfEvents, 2); |
| 560 expect(wasEntry1, isTrue); |
| 561 expect(wasEntry2, isTrue); |
| 562 } |
| 563 |
| 564 test_setState_invalid_invalidateDependent() { |
| 565 AnalysisTarget target = new TestSource(); |
| 566 CacheEntry entry = new CacheEntry(target); |
| 567 cache.put(entry); |
| 568 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 569 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 570 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 571 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
| 572 // set results, all of them are VALID |
| 573 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 574 entry.setValue(result2, 222, [new TargetedResult(target, result1)]); |
| 575 entry.setValue(result3, 333, [new TargetedResult(target, result2)]); |
| 576 entry.setValue(result4, 444, []); |
| 577 expect(entry.getState(result1), CacheState.VALID); |
| 578 expect(entry.getState(result2), CacheState.VALID); |
| 579 expect(entry.getState(result3), CacheState.VALID); |
| 580 expect(entry.getState(result4), CacheState.VALID); |
| 581 expect(entry.getValue(result1), 111); |
| 582 expect(entry.getValue(result2), 222); |
| 583 expect(entry.getValue(result3), 333); |
| 584 expect(entry.getValue(result4), 444); |
| 585 // invalidate result1, invalidates result2 and result3, result4 is intact |
| 586 entry.setState(result1, CacheState.INVALID); |
| 587 expect(entry.getState(result1), CacheState.INVALID); |
| 588 expect(entry.getState(result2), CacheState.INVALID); |
| 589 expect(entry.getState(result3), CacheState.INVALID); |
| 590 expect(entry.getState(result4), CacheState.VALID); |
| 591 expect(entry.getValue(result1), -1); |
| 592 expect(entry.getValue(result2), -2); |
| 593 expect(entry.getValue(result3), -3); |
| 594 expect(entry.getValue(result4), 444); |
| 595 // result4 is still valid, so the entry is still in the cache |
| 596 expect(cache.get(target), entry); |
| 597 } |
| 598 |
| 599 test_setState_invalid_removeEmptyEntry() { |
| 600 AnalysisTarget target1 = new TestSource('/a.dart'); |
| 601 AnalysisTarget target2 = new TestSource('/b.dart'); |
| 602 CacheEntry entry1 = new CacheEntry(target1); |
| 603 CacheEntry entry2 = new CacheEntry(target2); |
| 604 cache.put(entry1); |
| 605 cache.put(entry2); |
| 606 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 607 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 608 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 609 // set results, all of them are VALID |
| 610 entry1.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 611 entry2.setValue(result2, 222, [new TargetedResult(target1, result1)]); |
| 612 entry2.setValue(result3, 333, [new TargetedResult(target2, result2)]); |
| 613 expect(entry1.getState(result1), CacheState.VALID); |
| 614 expect(entry2.getState(result2), CacheState.VALID); |
| 615 expect(entry2.getState(result3), CacheState.VALID); |
| 616 expect(entry1.getValue(result1), 111); |
| 617 expect(entry2.getValue(result2), 222); |
| 618 expect(entry2.getValue(result3), 333); |
| 619 // invalidate result1, remove entry1 & entry2 |
| 620 entry1.setState(result1, CacheState.INVALID); |
| 621 expect(cache.get(target1), isNull); |
| 622 expect(cache.get(target2), isNull); |
| 623 } |
| 624 |
| 625 test_setState_valid() { |
| 626 AnalysisTarget target = new TestSource(); |
| 627 ResultDescriptor result = new ResultDescriptor('test', null); |
| 628 CacheEntry entry = new CacheEntry(target); |
| 629 expect(() => entry.setState(result, CacheState.VALID), throwsArgumentError); |
| 630 } |
| 631 |
| 632 test_setValue() { |
| 633 AnalysisTarget target = new TestSource(); |
| 634 ResultDescriptor result = new ResultDescriptor('test', null); |
| 635 String value = 'value'; |
| 636 CacheEntry entry = new CacheEntry(target); |
| 637 entry.setValue(result, value, TargetedResult.EMPTY_LIST); |
| 638 expect(entry.getState(result), CacheState.VALID); |
| 639 expect(entry.getValue(result), value); |
| 640 } |
| 641 |
| 642 test_setValue_flushResults() { |
| 643 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
| 644 ResultDescriptor descriptor1 = |
| 645 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 646 ResultDescriptor descriptor2 = |
| 647 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 648 ResultDescriptor descriptor3 = |
| 649 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
| 650 AnalysisTarget target = new TestSource(); |
| 651 CacheEntry entry = new CacheEntry(target); |
| 652 cache.put(entry); |
| 653 { |
| 654 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST); |
| 655 expect(entry.getState(descriptor1), CacheState.VALID); |
| 656 } |
| 657 { |
| 658 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST); |
| 659 expect(entry.getState(descriptor1), CacheState.VALID); |
| 660 expect(entry.getState(descriptor2), CacheState.VALID); |
| 661 } |
| 662 { |
| 663 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST); |
| 664 expect(entry.getState(descriptor1), CacheState.FLUSHED); |
| 665 expect(entry.getState(descriptor2), CacheState.VALID); |
| 666 expect(entry.getState(descriptor3), CacheState.VALID); |
| 667 } |
| 668 } |
| 669 |
| 670 test_setValue_keepDependent() { |
| 671 AnalysisTarget target = new TestSource(); |
| 672 CacheEntry entry = new CacheEntry(target); |
| 673 cache.put(entry); |
| 674 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 675 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 676 // set results, all of them are VALID |
| 677 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 678 entry.setValue(result2, 222, [new TargetedResult(target, result1)]); |
| 679 expect(entry.getState(result1), CacheState.VALID); |
| 680 expect(entry.getState(result2), CacheState.VALID); |
| 681 expect(entry.getValue(result1), 111); |
| 682 expect(entry.getValue(result2), 222); |
| 683 // set result1; result2 is intact |
| 684 entry.setValue(result1, 1111, TargetedResult.EMPTY_LIST); |
| 685 expect(entry.getState(result1), CacheState.VALID); |
| 686 expect(entry.getState(result2), CacheState.VALID); |
| 687 expect(entry.getValue(result1), 1111); |
| 688 expect(entry.getValue(result2), 222); |
| 689 } |
| 690 |
| 691 test_setValueIncremental() { |
| 692 AnalysisTarget target = new TestSource(); |
| 693 CacheEntry entry = new CacheEntry(target); |
| 694 cache.put(entry); |
| 695 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 696 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 697 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 698 // set results, all of them are VALID |
| 699 entry.setValue(result1, 111, TargetedResult.EMPTY_LIST); |
| 700 entry.setValue(result2, 222, [new TargetedResult(target, result1)]); |
| 701 entry.setValue(result3, 333, [new TargetedResult(target, result2)]); |
| 702 expect(entry.getState(result1), CacheState.VALID); |
| 703 expect(entry.getState(result2), CacheState.VALID); |
| 704 expect(entry.getState(result3), CacheState.VALID); |
| 705 expect(entry.getValue(result1), 111); |
| 706 expect(entry.getValue(result2), 222); |
| 707 expect(entry.getValue(result3), 333); |
| 708 // replace result1, keep "dependedOn", invalidate result3 |
| 709 entry.setValueIncremental(result2, 2222); |
| 710 expect(entry.getState(result1), CacheState.VALID); |
| 711 expect(entry.getState(result2), CacheState.VALID); |
| 712 expect(entry.getState(result3), CacheState.INVALID); |
| 713 expect(entry.getValue(result1), 111); |
| 714 expect(entry.getValue(result2), 2222); |
| 715 expect(entry.getValue(result3), -3); |
| 716 expect(entry.getResultData(result2).dependedOnResults, |
| 717 unorderedEquals([new TargetedResult(target, result1)])); |
| 718 } |
| 719 |
| 720 test_toString_empty() { |
| 721 AnalysisTarget target = new TestSource(); |
| 722 CacheEntry entry = new CacheEntry(target); |
| 723 expect(entry.toString(), isNotNull); |
| 724 } |
| 725 |
| 726 test_toString_nonEmpty() { |
| 727 AnalysisTarget target = new TestSource(); |
| 728 ResultDescriptor result = new ResultDescriptor('test', null); |
| 729 CacheEntry entry = new CacheEntry(target); |
| 730 entry.setValue(result, 42, TargetedResult.EMPTY_LIST); |
| 731 expect(entry.toString(), isNotNull); |
| 732 } |
| 733 } |
| 734 |
| 735 @reflectiveTest |
| 736 class CacheFlushManagerTest { |
| 737 CacheFlushManager manager = new CacheFlushManager( |
| 738 new SimpleResultCachingPolicy(15, 3), (AnalysisTarget target) => false); |
| 739 |
| 740 test_madeActive() { |
| 741 manager.madeActive(); |
| 742 expect(manager.maxSize, 15); |
| 743 } |
| 744 |
| 745 test_madeIdle() { |
| 746 manager.madeActive(); |
| 747 AnalysisTarget target = new TestSource(); |
| 748 // prepare TargetedResult(s) |
| 749 List<TargetedResult> results = <TargetedResult>[]; |
| 750 for (int i = 0; i < 15; i++) { |
| 751 ResultDescriptor descriptor = new ResultDescriptor('result$i', null); |
| 752 results.add(new TargetedResult(target, descriptor)); |
| 753 } |
| 754 // notify about storing TargetedResult(s) |
| 755 for (TargetedResult result in results) { |
| 756 manager.resultStored(result, null); |
| 757 } |
| 758 expect(manager.recentlyUsed, results); |
| 759 expect(manager.currentSize, 15); |
| 760 // make idle |
| 761 List<TargetedResult> resultsToFlush = manager.madeIdle(); |
| 762 expect(manager.maxSize, 3); |
| 763 expect(manager.recentlyUsed, results.skip(15 - 3)); |
| 764 expect(resultsToFlush, results.take(15 - 3)); |
| 765 } |
| 766 |
| 767 test_new() { |
| 768 expect(manager.maxActiveSize, 15); |
| 769 expect(manager.maxIdleSize, 3); |
| 770 expect(manager.maxSize, 3); |
| 771 expect(manager.currentSize, 0); |
| 772 expect(manager.recentlyUsed, isEmpty); |
| 773 } |
| 774 |
| 775 test_resultAccessed() { |
| 776 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 777 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 778 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 779 AnalysisTarget target = new TestSource(); |
| 780 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 781 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 782 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 783 manager.resultStored(result1, null); |
| 784 manager.resultStored(result2, null); |
| 785 manager.resultStored(result3, null); |
| 786 expect(manager.currentSize, 3); |
| 787 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 788 // access result2 |
| 789 manager.resultAccessed(result2); |
| 790 expect(manager.currentSize, 3); |
| 791 expect(manager.recentlyUsed, orderedEquals([result1, result3, result2])); |
| 792 } |
| 793 |
| 794 test_resultAccessed_negativeMaxSize() { |
| 795 manager = new CacheFlushManager(new SimpleResultCachingPolicy(-1, -1), |
| 796 (AnalysisTarget target) => false); |
| 797 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 798 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 799 AnalysisTarget target = new TestSource(); |
| 800 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 801 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 802 manager.resultStored(result1, null); |
| 803 manager.resultStored(result2, null); |
| 804 expect(manager.currentSize, 0); |
| 805 expect(manager.recentlyUsed, isEmpty); |
| 806 // access result2 |
| 807 manager.resultAccessed(result2); |
| 808 expect(manager.currentSize, 0); |
| 809 expect(manager.recentlyUsed, isEmpty); |
| 810 } |
| 811 |
| 812 test_resultAccessed_noSuchResult() { |
| 813 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 814 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 815 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 816 AnalysisTarget target = new TestSource(); |
| 817 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 818 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 819 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 820 manager.resultStored(result1, null); |
| 821 manager.resultStored(result2, null); |
| 822 expect(manager.currentSize, 2); |
| 823 expect(manager.recentlyUsed, orderedEquals([result1, result2])); |
| 824 // access result3, no-op |
| 825 manager.resultAccessed(result3); |
| 826 expect(manager.currentSize, 2); |
| 827 expect(manager.recentlyUsed, orderedEquals([result1, result2])); |
| 828 } |
| 829 |
| 830 test_resultStored() { |
| 831 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 832 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 833 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 834 ResultDescriptor descriptor4 = new ResultDescriptor('result4', null); |
| 835 AnalysisTarget target = new TestSource(); |
| 836 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 837 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 838 TargetedResult result3 = new TargetedResult(target, descriptor3); |
| 839 TargetedResult result4 = new TargetedResult(target, descriptor4); |
| 840 manager.resultStored(result1, null); |
| 841 manager.resultStored(result2, null); |
| 842 manager.resultStored(result3, null); |
| 843 expect(manager.currentSize, 3); |
| 844 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 845 // store result2 again |
| 846 { |
| 847 List<TargetedResult> resultsToFlush = manager.resultStored(result2, null); |
| 848 expect(resultsToFlush, isEmpty); |
| 849 expect(manager.currentSize, 3); |
| 850 expect(manager.recentlyUsed, orderedEquals([result1, result3, result2])); |
| 851 } |
| 852 // store result4 |
| 853 { |
| 854 List<TargetedResult> resultsToFlush = manager.resultStored(result4, null); |
| 855 expect(resultsToFlush, [result1]); |
| 856 expect(manager.currentSize, 3); |
| 857 expect(manager.recentlyUsed, orderedEquals([result3, result2, result4])); |
| 858 expect(manager.resultSizeMap, {result3: 1, result2: 1, result4: 1}); |
| 859 } |
| 860 } |
| 861 |
| 862 test_resultStored_negativeMaxSize() { |
| 863 manager = new CacheFlushManager(new SimpleResultCachingPolicy(-1, -1), |
| 864 (AnalysisTarget target) => false); |
| 865 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 866 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 867 AnalysisTarget target = new TestSource(); |
| 868 TargetedResult result1 = new TargetedResult(target, descriptor1); |
| 869 TargetedResult result2 = new TargetedResult(target, descriptor2); |
| 870 manager.resultStored(result1, null); |
| 871 manager.resultStored(result2, null); |
| 872 expect(manager.currentSize, 0); |
| 873 expect(manager.recentlyUsed, isEmpty); |
| 874 } |
| 875 |
| 876 test_targetRemoved() { |
| 877 ResultDescriptor descriptor1 = new ResultDescriptor('result1', null); |
| 878 ResultDescriptor descriptor2 = new ResultDescriptor('result2', null); |
| 879 ResultDescriptor descriptor3 = new ResultDescriptor('result3', null); |
| 880 AnalysisTarget target1 = new TestSource('a.dart'); |
| 881 AnalysisTarget target2 = new TestSource('b.dart'); |
| 882 TargetedResult result1 = new TargetedResult(target1, descriptor1); |
| 883 TargetedResult result2 = new TargetedResult(target2, descriptor2); |
| 884 TargetedResult result3 = new TargetedResult(target1, descriptor3); |
| 885 manager.resultStored(result1, null); |
| 886 manager.resultStored(result2, null); |
| 887 manager.resultStored(result3, null); |
| 888 expect(manager.currentSize, 3); |
| 889 expect(manager.recentlyUsed, orderedEquals([result1, result2, result3])); |
| 890 expect(manager.resultSizeMap, {result1: 1, result2: 1, result3: 1}); |
| 891 // remove target1 |
| 892 { |
| 893 manager.targetRemoved(target1); |
| 894 expect(manager.currentSize, 1); |
| 895 expect(manager.recentlyUsed, orderedEquals([result2])); |
| 896 expect(manager.resultSizeMap, {result2: 1}); |
| 897 } |
| 898 // remove target2 |
| 899 { |
| 900 manager.targetRemoved(target2); |
| 901 expect(manager.currentSize, 0); |
| 902 expect(manager.recentlyUsed, isEmpty); |
| 903 expect(manager.resultSizeMap, isEmpty); |
| 904 } |
| 905 } |
| 906 } |
| 907 |
| 908 abstract class CachePartitionTest extends EngineTestCase { |
| 909 CachePartition createPartition(); |
| 910 |
| 911 void test_creation() { |
| 912 expect(createPartition(), isNotNull); |
| 913 } |
| 914 |
| 915 void test_entrySet() { |
| 916 CachePartition partition = createPartition(); |
| 917 AnalysisTarget target = new TestSource(); |
| 918 CacheEntry entry = new CacheEntry(target); |
| 919 partition.put(entry); |
| 920 Map<AnalysisTarget, CacheEntry> entryMap = partition.map; |
| 921 expect(entryMap, hasLength(1)); |
| 922 AnalysisTarget entryKey = entryMap.keys.first; |
| 923 expect(entryKey, target); |
| 924 expect(entryMap[entryKey], entry); |
| 925 } |
| 926 |
| 927 void test_get() { |
| 928 CachePartition partition = createPartition(); |
| 929 AnalysisTarget target = new TestSource(); |
| 930 expect(partition.get(target), isNull); |
| 931 } |
| 932 |
| 933 void test_put_alreadyInPartition() { |
| 934 CachePartition partition1 = createPartition(); |
| 935 CachePartition partition2 = createPartition(); |
| 936 AnalysisTarget target = new TestSource(); |
| 937 CacheEntry entry = new CacheEntry(target); |
| 938 partition1.put(entry); |
| 939 expect(() => partition2.put(entry), throwsStateError); |
| 940 } |
| 941 |
| 942 void test_put_noFlush() { |
| 943 CachePartition partition = createPartition(); |
| 944 AnalysisTarget target = new TestSource(); |
| 945 CacheEntry entry = new CacheEntry(target); |
| 946 partition.put(entry); |
| 947 expect(partition.get(target), entry); |
| 948 } |
| 949 |
| 950 void test_remove_absent() { |
| 951 CachePartition partition = createPartition(); |
| 952 AnalysisTarget target = new TestSource(); |
| 953 expect(partition.get(target), isNull); |
| 954 expect(partition.remove(target), isNull); |
| 955 expect(partition.get(target), isNull); |
| 956 } |
| 957 |
| 958 void test_remove_present() { |
| 959 CachePartition partition = createPartition(); |
| 960 AnalysisTarget target = new TestSource(); |
| 961 CacheEntry entry = new CacheEntry(target); |
| 962 partition.put(entry); |
| 963 expect(partition.get(target), entry); |
| 964 expect(partition.remove(target), entry); |
| 965 expect(partition.get(target), isNull); |
| 966 } |
| 967 } |
| 968 |
| 969 @reflectiveTest |
| 970 class ResultDataTest extends EngineTestCase { |
| 971 test_creation() { |
| 972 String value = 'value'; |
| 973 ResultData data = new ResultData(new ResultDescriptor('test', value)); |
| 974 expect(data, isNotNull); |
| 975 expect(data.state, CacheState.INVALID); |
| 976 expect(data.value, value); |
| 977 } |
| 978 |
| 979 test_flush() { |
| 980 ResultDescriptor result = new ResultDescriptor('test', -1); |
| 981 ResultData data = new ResultData(result); |
| 982 data.state = CacheState.VALID; |
| 983 data.value = 123; |
| 984 data.flush(); |
| 985 expect(data.state, CacheState.FLUSHED); |
| 986 expect(data.value, -1); |
| 987 } |
| 988 } |
| 989 |
| 990 @reflectiveTest |
| 991 class SdkCachePartitionTest extends CachePartitionTest { |
| 992 CachePartition createPartition() { |
| 993 return new SdkCachePartition(null); |
| 994 } |
| 995 |
| 996 void test_contains_false() { |
| 997 CachePartition partition = createPartition(); |
| 998 AnalysisTarget target = new TestSource(); |
| 999 expect(partition.isResponsibleFor(target), isFalse); |
| 1000 } |
| 1001 |
| 1002 void test_contains_true() { |
| 1003 SdkCachePartition partition = new SdkCachePartition(null); |
| 1004 SourceFactory factory = new SourceFactory( |
| 1005 [new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)]); |
| 1006 AnalysisTarget target = factory.forUri("dart:core"); |
| 1007 expect(partition.isResponsibleFor(target), isTrue); |
| 1008 } |
| 1009 } |
| 1010 |
| 1011 @reflectiveTest |
| 1012 class UniversalCachePartitionTest extends CachePartitionTest { |
| 1013 CachePartition createPartition() { |
| 1014 return new UniversalCachePartition(null); |
| 1015 } |
| 1016 |
| 1017 void test_contains() { |
| 1018 UniversalCachePartition partition = new UniversalCachePartition(null); |
| 1019 TestSource source = new TestSource(); |
| 1020 expect(partition.isResponsibleFor(source), isTrue); |
| 1021 } |
| 1022 |
| 1023 test_dispose() { |
| 1024 InternalAnalysisContext context = new _InternalAnalysisContextMock(); |
| 1025 CachePartition partition1 = new UniversalCachePartition(context); |
| 1026 CachePartition partition2 = new UniversalCachePartition(context); |
| 1027 AnalysisCache cache = new AnalysisCache([partition1, partition2]); |
| 1028 when(context.analysisCache).thenReturn(cache); |
| 1029 // configure |
| 1030 // prepare entries |
| 1031 ResultDescriptor descriptor1 = new ResultDescriptor('result1', -1); |
| 1032 ResultDescriptor descriptor2 = new ResultDescriptor('result2', -2); |
| 1033 AnalysisTarget target1 = new TestSource('1.dart'); |
| 1034 AnalysisTarget target2 = new TestSource('2.dart'); |
| 1035 TargetedResult result1 = new TargetedResult(target1, descriptor1); |
| 1036 TargetedResult result2 = new TargetedResult(target2, descriptor2); |
| 1037 CacheEntry entry1 = new CacheEntry(target1); |
| 1038 CacheEntry entry2 = new CacheEntry(target2); |
| 1039 partition1.put(entry1); |
| 1040 partition2.put(entry2); |
| 1041 entry1.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST); |
| 1042 entry2.setValue(descriptor2, 2, <TargetedResult>[result1]); |
| 1043 // target2 is listed as dependent in target1 |
| 1044 expect( |
| 1045 entry1.getResultData(descriptor1).dependentResults, contains(result2)); |
| 1046 // dispose |
| 1047 partition2.dispose(); |
| 1048 expect(partition1.get(target1), same(entry1)); |
| 1049 expect(partition2.get(target2), isNull); |
| 1050 // result2 is removed from result1 |
| 1051 expect(entry1.getResultData(descriptor1).dependentResults, isEmpty); |
| 1052 } |
| 1053 } |
| 1054 |
| 1055 class _InternalAnalysisContextMock extends TypedMock |
| 1056 implements InternalAnalysisContext { |
| 1057 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 1058 } |
| 1059 |
| 1060 class _TestAnalysisTarget implements AnalysisTarget { |
| 1061 @override |
| 1062 Source get source => null; |
| 1063 } |
OLD | NEW |