| 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 } |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 179 } | 130 } |
| 180 | 131 |
| 181 test_fixExceptionState_noError_noException() { | 132 test_fixExceptionState_noError_noException() { |
| 182 ResultDescriptor result = new ResultDescriptor('test', null); | 133 ResultDescriptor result = new ResultDescriptor('test', null); |
| 183 CacheEntry entry = new CacheEntry(); | 134 CacheEntry entry = new CacheEntry(); |
| 184 entry.fixExceptionState(); | 135 entry.fixExceptionState(); |
| 185 expect(entry.getState(result), CacheState.INVALID); | 136 expect(entry.getState(result), CacheState.INVALID); |
| 186 expect(entry.exception, isNull); | 137 expect(entry.exception, isNull); |
| 187 } | 138 } |
| 188 | 139 |
| 189 test_flushAstStructures() { | 140 test_getMemento_noResult() { |
| 190 ResultDescriptor result = new ResultDescriptor('test', null); | 141 String defaultValue = 'value'; |
| 142 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
| 191 CacheEntry entry = new CacheEntry(); | 143 CacheEntry entry = new CacheEntry(); |
| 192 entry.setValue( | 144 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 } | 145 } |
| 198 | 146 |
| 199 test_getState() { | 147 test_getState() { |
| 200 ResultDescriptor result = new ResultDescriptor('test', null); | 148 ResultDescriptor result = new ResultDescriptor('test', null); |
| 201 CacheEntry entry = new CacheEntry(); | 149 CacheEntry entry = new CacheEntry(); |
| 202 expect(entry.getState(result), CacheState.INVALID); | 150 expect(entry.getState(result), CacheState.INVALID); |
| 203 } | 151 } |
| 204 | 152 |
| 205 test_getValue() { | 153 test_getValue() { |
| 206 String defaultValue = 'value'; | 154 String defaultValue = 'value'; |
| 207 ResultDescriptor result = new ResultDescriptor('test', defaultValue); | 155 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
| 208 CacheEntry entry = new CacheEntry(); | 156 CacheEntry entry = new CacheEntry(); |
| 209 expect(entry.getValue(result), defaultValue); | 157 expect(entry.getValue(result), defaultValue); |
| 210 } | 158 } |
| 211 | 159 |
| 212 test_getMemento_noResult() { | 160 test_getValue_flushResults() { |
| 213 String defaultValue = 'value'; | 161 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
| 214 ResultDescriptor result = new ResultDescriptor('test', defaultValue); | 162 ResultDescriptor descriptor1 = |
| 163 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 164 ResultDescriptor descriptor2 = |
| 165 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 166 ResultDescriptor descriptor3 = |
| 167 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
| 168 AnalysisTarget target = new TestSource(); |
| 169 InternalAnalysisContext context = new _InternalAnalysisContextMock(); |
| 170 when(context.priorityTargets).thenReturn([]); |
| 171 AnalysisCache cache = createCache(context: context); |
| 215 CacheEntry entry = new CacheEntry(); | 172 CacheEntry entry = new CacheEntry(); |
| 216 expect(entry.getMemento(result), null); | 173 cache.put(target, entry); |
| 217 } | 174 { |
| 218 | 175 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST, null); |
| 219 test_hasAstStructure_false() { | 176 expect(entry.getState(descriptor1), CacheState.VALID); |
| 220 CacheEntry entry = new CacheEntry(); | 177 } |
| 221 expect(entry.hasAstStructure, false); | 178 { |
| 222 } | 179 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST, null); |
| 223 | 180 expect(entry.getState(descriptor1), CacheState.VALID); |
| 224 test_hasAstStructure_true() { | 181 expect(entry.getState(descriptor2), CacheState.VALID); |
| 225 ResultDescriptor result = new ResultDescriptor('test', null); | 182 } |
| 226 CacheEntry entry = new CacheEntry(); | 183 // get descriptor1, so that descriptor2 will be flushed |
| 227 entry.setValue( | 184 entry.getValue(descriptor1); |
| 228 result, new NullLiteral(null), TargetedResult.EMPTY_LIST, null); | 185 { |
| 229 expect(entry.hasAstStructure, true); | 186 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST, null); |
| 187 expect(entry.getState(descriptor1), CacheState.VALID); |
| 188 expect(entry.getState(descriptor2), CacheState.FLUSHED); |
| 189 expect(entry.getState(descriptor3), CacheState.VALID); |
| 190 } |
| 230 } | 191 } |
| 231 | 192 |
| 232 test_hasErrorState_false() { | 193 test_hasErrorState_false() { |
| 233 CacheEntry entry = new CacheEntry(); | 194 CacheEntry entry = new CacheEntry(); |
| 234 expect(entry.hasErrorState(), false); | 195 expect(entry.hasErrorState(), false); |
| 235 } | 196 } |
| 236 | 197 |
| 237 test_hasErrorState_true() { | 198 test_hasErrorState_true() { |
| 238 ResultDescriptor result = new ResultDescriptor('test', null); | 199 ResultDescriptor result = new ResultDescriptor('test', null); |
| 239 CaughtException exception = new CaughtException(null, null); | 200 CaughtException exception = new CaughtException(null, null); |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 ResultDescriptor result = new ResultDescriptor('test', null); | 392 ResultDescriptor result = new ResultDescriptor('test', null); |
| 432 String value = 'value'; | 393 String value = 'value'; |
| 433 String memento = 'main() {}'; | 394 String memento = 'main() {}'; |
| 434 CacheEntry entry = new CacheEntry(); | 395 CacheEntry entry = new CacheEntry(); |
| 435 entry.setValue(result, value, TargetedResult.EMPTY_LIST, memento); | 396 entry.setValue(result, value, TargetedResult.EMPTY_LIST, memento); |
| 436 expect(entry.getState(result), CacheState.VALID); | 397 expect(entry.getState(result), CacheState.VALID); |
| 437 expect(entry.getValue(result), value); | 398 expect(entry.getValue(result), value); |
| 438 expect(entry.getMemento(result), memento); | 399 expect(entry.getMemento(result), memento); |
| 439 } | 400 } |
| 440 | 401 |
| 402 test_setValue_flushResults() { |
| 403 ResultCachingPolicy cachingPolicy = new SimpleResultCachingPolicy(2, 2); |
| 404 ResultDescriptor descriptor1 = |
| 405 new ResultDescriptor('result1', null, cachingPolicy: cachingPolicy); |
| 406 ResultDescriptor descriptor2 = |
| 407 new ResultDescriptor('result2', null, cachingPolicy: cachingPolicy); |
| 408 ResultDescriptor descriptor3 = |
| 409 new ResultDescriptor('result3', null, cachingPolicy: cachingPolicy); |
| 410 AnalysisTarget target = new TestSource(); |
| 411 InternalAnalysisContext context = new _InternalAnalysisContextMock(); |
| 412 when(context.priorityTargets).thenReturn([]); |
| 413 AnalysisCache cache = createCache(context: context); |
| 414 CacheEntry entry = new CacheEntry(); |
| 415 cache.put(target, entry); |
| 416 { |
| 417 entry.setValue(descriptor1, 1, TargetedResult.EMPTY_LIST, null); |
| 418 expect(entry.getState(descriptor1), CacheState.VALID); |
| 419 } |
| 420 { |
| 421 entry.setValue(descriptor2, 2, TargetedResult.EMPTY_LIST, null); |
| 422 expect(entry.getState(descriptor1), CacheState.VALID); |
| 423 expect(entry.getState(descriptor2), CacheState.VALID); |
| 424 } |
| 425 { |
| 426 entry.setValue(descriptor3, 3, TargetedResult.EMPTY_LIST, null); |
| 427 expect(entry.getState(descriptor1), CacheState.FLUSHED); |
| 428 expect(entry.getState(descriptor2), CacheState.VALID); |
| 429 expect(entry.getState(descriptor3), CacheState.VALID); |
| 430 } |
| 431 } |
| 432 |
| 441 test_setValue_invalidateDependent() { | 433 test_setValue_invalidateDependent() { |
| 442 AnalysisCache cache = createCache(); | 434 AnalysisCache cache = createCache(); |
| 443 AnalysisTarget target = new TestSource(); | 435 AnalysisTarget target = new TestSource(); |
| 444 CacheEntry entry = new CacheEntry(); | 436 CacheEntry entry = new CacheEntry(); |
| 445 cache.put(target, entry); | 437 cache.put(target, entry); |
| 446 ResultDescriptor result1 = new ResultDescriptor('result1', -1); | 438 ResultDescriptor result1 = new ResultDescriptor('result1', -1); |
| 447 ResultDescriptor result2 = new ResultDescriptor('result2', -2); | 439 ResultDescriptor result2 = new ResultDescriptor('result2', -2); |
| 448 ResultDescriptor result3 = new ResultDescriptor('result3', -3); | 440 ResultDescriptor result3 = new ResultDescriptor('result3', -3); |
| 449 ResultDescriptor result4 = new ResultDescriptor('result4', -4); | 441 ResultDescriptor result4 = new ResultDescriptor('result4', -4); |
| 450 // set results, all of them are VALID | 442 // set results, all of them are VALID |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after 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 20 matching lines...) Expand all Loading... |
| 552 | 685 |
| 553 void test_remove() { | 686 void test_remove() { |
| 554 CachePartition partition = createPartition(); | 687 CachePartition partition = createPartition(); |
| 555 AnalysisTarget target = new TestSource(); | 688 AnalysisTarget target = new TestSource(); |
| 556 CacheEntry entry = new CacheEntry(); | 689 CacheEntry entry = new CacheEntry(); |
| 557 partition.put(target, entry); | 690 partition.put(target, entry); |
| 558 expect(partition.get(target), entry); | 691 expect(partition.get(target), entry); |
| 559 partition.remove(target); | 692 partition.remove(target); |
| 560 expect(partition.get(target), isNull); | 693 expect(partition.get(target), isNull); |
| 561 } | 694 } |
| 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 } | 695 } |
| 605 | 696 |
| 606 @reflectiveTest | 697 @reflectiveTest |
| 607 class ResultDataTest extends EngineTestCase { | 698 class ResultDataTest extends EngineTestCase { |
| 608 test_creation() { | 699 test_creation() { |
| 609 String value = 'value'; | 700 String value = 'value'; |
| 610 ResultData data = new ResultData(new ResultDescriptor('test', value)); | 701 ResultData data = new ResultData(new ResultDescriptor('test', value)); |
| 611 expect(data, isNotNull); | 702 expect(data, isNotNull); |
| 612 expect(data.state, CacheState.INVALID); | 703 expect(data.state, CacheState.INVALID); |
| 613 expect(data.value, value); | 704 expect(data.value, value); |
| 614 } | 705 } |
| 615 } | 706 } |
| 616 | 707 |
| 617 @reflectiveTest | 708 @reflectiveTest |
| 618 class SdkCachePartitionTest extends CachePartitionTest { | 709 class SdkCachePartitionTest extends CachePartitionTest { |
| 619 CachePartition createPartition([CacheRetentionPolicy policy = null]) { | 710 CachePartition createPartition() { |
| 620 return new SdkCachePartition(null, 8); | 711 return new SdkCachePartition(null); |
| 621 } | 712 } |
| 622 | 713 |
| 623 void test_contains_false() { | 714 void test_contains_false() { |
| 624 CachePartition partition = createPartition(); | 715 CachePartition partition = createPartition(); |
| 625 AnalysisTarget target = new TestSource(); | 716 AnalysisTarget target = new TestSource(); |
| 626 expect(partition.contains(target), isFalse); | 717 expect(partition.contains(target), isFalse); |
| 627 } | 718 } |
| 628 | 719 |
| 629 void test_contains_true() { | 720 void test_contains_true() { |
| 630 SdkCachePartition partition = new SdkCachePartition(null, 8); | 721 SdkCachePartition partition = new SdkCachePartition(null); |
| 631 SourceFactory factory = new SourceFactory( | 722 SourceFactory factory = new SourceFactory( |
| 632 [new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)]); | 723 [new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)]); |
| 633 AnalysisTarget target = factory.forUri("dart:core"); | 724 AnalysisTarget target = factory.forUri("dart:core"); |
| 634 expect(partition.contains(target), isTrue); | 725 expect(partition.contains(target), isTrue); |
| 635 } | 726 } |
| 636 } | 727 } |
| 637 | 728 |
| 638 @reflectiveTest | 729 @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 { | 730 class UniversalCachePartitionTest extends CachePartitionTest { |
| 651 CachePartition createPartition([CacheRetentionPolicy policy = null]) { | 731 CachePartition createPartition() { |
| 652 return new UniversalCachePartition(null, 8, policy); | 732 return new UniversalCachePartition(null); |
| 653 } | 733 } |
| 654 | 734 |
| 655 void test_contains() { | 735 void test_contains() { |
| 656 UniversalCachePartition partition = | 736 UniversalCachePartition partition = new UniversalCachePartition(null); |
| 657 new UniversalCachePartition(null, 8, null); | |
| 658 TestSource source = new TestSource(); | 737 TestSource source = new TestSource(); |
| 659 expect(partition.contains(source), isTrue); | 738 expect(partition.contains(source), isTrue); |
| 660 } | 739 } |
| 661 } | 740 } |
| 741 |
| 742 class _InternalAnalysisContextMock extends TypedMock |
| 743 implements InternalAnalysisContext { |
| 744 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); |
| 745 } |
| OLD | NEW |