| 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/generated/engine.dart' |
| 8 show AnalysisContext, CacheState, RetentionPriority; |
| 9 import 'package:analyzer/src/task/driver.dart'; |
| 10 import 'package:analyzer/src/task/manager.dart'; |
| 11 import 'package:analyzer/task/model.dart'; |
| 12 import 'package:unittest/unittest.dart'; |
| 13 |
| 14 import '../../generated/resolver_test.dart'; |
| 15 import '../../generated/test_support.dart'; |
| 16 import '../../reflective_tests.dart'; |
| 17 import 'package:analyzer/src/generated/source.dart'; |
| 18 import 'package:analyzer/src/generated/html.dart'; |
| 19 import 'package:analyzer/src/generated/ast.dart'; |
| 20 import 'package:analyzer/src/generated/element.dart'; |
| 21 import 'package:analyzer/src/generated/resolver.dart'; |
| 22 import 'package:analyzer/src/cancelable_future.dart'; |
| 23 import 'package:analyzer/src/generated/constant.dart'; |
| 24 import 'package:analyzer/src/generated/error.dart'; |
| 25 import 'dart:collection'; |
| 26 import 'dart:async'; |
| 27 import 'package:analyzer/src/context/cache.dart'; |
| 28 import 'package:analyzer/src/generated/java_engine.dart'; |
| 29 import 'package:analyzer/src/generated/sdk_io.dart'; |
| 30 import 'package:analyzer/src/generated/utilities_collection.dart'; |
| 31 import '../../generated/engine_test.dart'; |
| 32 |
| 33 main() { |
| 34 groupSep = ' | '; |
| 35 runReflectiveTests(AnalysisCacheTest); |
| 36 runReflectiveTests(CacheEntryTest); |
| 37 runReflectiveTests(SdkCachePartitionTest); |
| 38 runReflectiveTests(UniversalCachePartitionTest); |
| 39 runReflectiveTests(ResultDataTest); |
| 40 } |
| 41 |
| 42 @reflectiveTest |
| 43 class AnalysisCacheTest extends EngineTestCase { |
| 44 void test_astSize_empty() { |
| 45 AnalysisCache cache = createCache(); |
| 46 expect(cache.astSize, 0); |
| 47 } |
| 48 |
| 49 void test_astSize_nonEmpty() { |
| 50 ResultDescriptor result = new ResultDescriptor('test', null); |
| 51 AstNode node = new NullLiteral(null); |
| 52 AnalysisCache cache = createCache(); |
| 53 AnalysisTarget target1 = new TestSource('/test1.dart'); |
| 54 CacheEntry entry1 = new CacheEntry(); |
| 55 entry1.setValue(result, node); |
| 56 AnalysisTarget target2 = new TestSource('/test2.dart'); |
| 57 CacheEntry entry2 = new CacheEntry(); |
| 58 entry2.setValue(result, node); |
| 59 cache.put(target1, entry1); |
| 60 cache.accessedAst(target1); |
| 61 cache.put(target2, entry2); |
| 62 cache.accessedAst(target2); |
| 63 expect(cache.astSize, 2); |
| 64 } |
| 65 |
| 66 void test_creation() { |
| 67 expect(createCache(), isNotNull); |
| 68 } |
| 69 |
| 70 void test_get() { |
| 71 AnalysisCache cache = createCache(); |
| 72 AnalysisTarget target = new TestSource(); |
| 73 expect(cache.get(target), isNull); |
| 74 } |
| 75 |
| 76 void test_getContextFor() { |
| 77 AnalysisContext context = new TestAnalysisContext(); |
| 78 AnalysisCache cache = createCache(context: context); |
| 79 AnalysisTarget target = new TestSource(); |
| 80 expect(cache.getContextFor(target), context); |
| 81 } |
| 82 |
| 83 void test_iterator() { |
| 84 AnalysisCache cache = createCache(); |
| 85 AnalysisTarget target = new TestSource(); |
| 86 CacheEntry entry = new CacheEntry(); |
| 87 cache.put(target, entry); |
| 88 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); |
| 89 expect(iterator.moveNext(), isTrue); |
| 90 expect(iterator.key, same(target)); |
| 91 expect(iterator.value, same(entry)); |
| 92 expect(iterator.moveNext(), isFalse); |
| 93 } |
| 94 |
| 95 void test_put() { |
| 96 AnalysisCache cache = createCache(); |
| 97 AnalysisTarget target = new TestSource(); |
| 98 CacheEntry entry = new CacheEntry(); |
| 99 expect(cache.get(target), isNull); |
| 100 cache.put(target, entry); |
| 101 expect(cache.get(target), entry); |
| 102 } |
| 103 |
| 104 AnalysisCache createCache({AnalysisContext context, RetentionPriority policy :
RetentionPriority.LOW}) { |
| 105 CachePartition partition = new UniversalCachePartition(context, 8, new TestC
acheRetentionPolicy(policy)); |
| 106 return new AnalysisCache(<CachePartition>[partition]); |
| 107 } |
| 108 |
| 109 void test_remove() { |
| 110 AnalysisCache cache = createCache(); |
| 111 AnalysisTarget target = new TestSource(); |
| 112 cache.remove(target); |
| 113 } |
| 114 |
| 115 void test_setMaxCacheSize() { |
| 116 CachePartition partition = new UniversalCachePartition(null, 8, new TestCach
eRetentionPolicy(RetentionPriority.MEDIUM)); |
| 117 AnalysisCache cache = new AnalysisCache(<CachePartition>[partition]); |
| 118 ResultDescriptor result = new ResultDescriptor('test', null); |
| 119 AstNode node = new NullLiteral(null); |
| 120 int size = 6; |
| 121 for (int i = 0; i < size; i++) { |
| 122 AnalysisTarget target = new TestSource("/test$i.dart"); |
| 123 CacheEntry entry = new CacheEntry(); |
| 124 entry.setValue(result, node); |
| 125 cache.put(target, entry); |
| 126 cache.accessedAst(target); |
| 127 } |
| 128 |
| 129 void _assertNonFlushedCount(int expectedCount, AnalysisCache cache) { |
| 130 int nonFlushedCount = 0; |
| 131 MapIterator<AnalysisTarget, CacheEntry> iterator = cache.iterator(); |
| 132 while (iterator.moveNext()) { |
| 133 if (iterator.value.getState(result) != CacheState.FLUSHED) { |
| 134 nonFlushedCount++; |
| 135 } |
| 136 } |
| 137 expect(nonFlushedCount, expectedCount); |
| 138 } |
| 139 |
| 140 _assertNonFlushedCount(size, cache); |
| 141 int newSize = size - 2; |
| 142 partition.maxCacheSize = newSize; |
| 143 _assertNonFlushedCount(newSize, cache); |
| 144 } |
| 145 |
| 146 void test_size() { |
| 147 AnalysisCache cache = createCache(); |
| 148 int size = 4; |
| 149 for (int i = 0; i < size; i++) { |
| 150 AnalysisTarget target = new TestSource("/test$i.dart"); |
| 151 cache.put(target, new CacheEntry()); |
| 152 } |
| 153 expect(cache.size(), size); |
| 154 } |
| 155 } |
| 156 |
| 157 @reflectiveTest |
| 158 class CacheEntryTest extends EngineTestCase { |
| 159 test_explicitlyAdded() { |
| 160 CacheEntry entry = new CacheEntry(); |
| 161 expect(entry.explicitlyAdded, false); |
| 162 entry.explicitlyAdded = true; |
| 163 expect(entry.explicitlyAdded, true); |
| 164 } |
| 165 |
| 166 test_hasAstStructure_false() { |
| 167 CacheEntry entry = new CacheEntry(); |
| 168 expect(entry.hasAstStructure, false); |
| 169 } |
| 170 |
| 171 test_hasAstStructure_true() { |
| 172 ResultDescriptor result = new ResultDescriptor('test', null); |
| 173 CacheEntry entry = new CacheEntry(); |
| 174 entry.setValue(result, new NullLiteral(null)); |
| 175 expect(entry.hasAstStructure, true); |
| 176 } |
| 177 |
| 178 test_fixExceptionState_error_exception() { |
| 179 ResultDescriptor result = new ResultDescriptor('test', null); |
| 180 CaughtException exception = new CaughtException(null, null); |
| 181 CacheEntry entry = new CacheEntry(); |
| 182 entry.setState(result, CacheState.ERROR); |
| 183 entry.exception = exception; |
| 184 entry.fixExceptionState(); |
| 185 expect(entry.getState(result), CacheState.ERROR); |
| 186 expect(entry.exception, exception); |
| 187 } |
| 188 |
| 189 test_fixExceptionState_error_noException() { |
| 190 ResultDescriptor result = new ResultDescriptor('test', null); |
| 191 CacheEntry entry = new CacheEntry(); |
| 192 entry.setState(result, CacheState.ERROR); |
| 193 entry.fixExceptionState(); |
| 194 expect(entry.getState(result), CacheState.ERROR); |
| 195 expect(entry.exception, isNotNull); |
| 196 } |
| 197 |
| 198 test_fixExceptionState_noError_exception() { |
| 199 ResultDescriptor result = new ResultDescriptor('test', null); |
| 200 CacheEntry entry = new CacheEntry(); |
| 201 entry.exception = new CaughtException(null, null); |
| 202 entry.fixExceptionState(); |
| 203 expect(entry.getState(result), CacheState.INVALID); |
| 204 expect(entry.exception, isNull); |
| 205 } |
| 206 |
| 207 test_fixExceptionState_noError_noException() { |
| 208 ResultDescriptor result = new ResultDescriptor('test', null); |
| 209 CacheEntry entry = new CacheEntry(); |
| 210 entry.fixExceptionState(); |
| 211 expect(entry.getState(result), CacheState.INVALID); |
| 212 expect(entry.exception, isNull); |
| 213 } |
| 214 |
| 215 test_flushAstStructures() { |
| 216 ResultDescriptor result = new ResultDescriptor('test', null); |
| 217 CacheEntry entry = new CacheEntry(); |
| 218 entry.setValue(result, new NullLiteral(null)); |
| 219 expect(entry.hasAstStructure, true); |
| 220 entry.flushAstStructures(); |
| 221 expect(entry.hasAstStructure, false); |
| 222 } |
| 223 |
| 224 test_getState() { |
| 225 ResultDescriptor result = new ResultDescriptor('test', null); |
| 226 CacheEntry entry = new CacheEntry(); |
| 227 expect(entry.getState(result), CacheState.INVALID); |
| 228 } |
| 229 |
| 230 test_getValue() { |
| 231 String defaultValue = 'value'; |
| 232 ResultDescriptor result = new ResultDescriptor('test', defaultValue); |
| 233 CacheEntry entry = new CacheEntry(); |
| 234 expect(entry.getValue(result), defaultValue); |
| 235 } |
| 236 |
| 237 test_hasErrorState_false() { |
| 238 CacheEntry entry = new CacheEntry(); |
| 239 expect(entry.hasErrorState(), false); |
| 240 } |
| 241 |
| 242 test_hasErrorState_true() { |
| 243 ResultDescriptor result = new ResultDescriptor('test', null); |
| 244 CacheEntry entry = new CacheEntry(); |
| 245 entry.setState(result, CacheState.ERROR); |
| 246 expect(entry.hasErrorState(), true); |
| 247 } |
| 248 |
| 249 test_invalidateAllInformation() { |
| 250 ResultDescriptor result = new ResultDescriptor('test', null); |
| 251 CacheEntry entry = new CacheEntry(); |
| 252 entry.setValue(result, 'value'); |
| 253 entry.invalidateAllInformation(); |
| 254 expect(entry.getState(result), CacheState.INVALID); |
| 255 expect(entry.getValue(result), isNull); |
| 256 } |
| 257 |
| 258 test_setState_error() { |
| 259 ResultDescriptor result = new ResultDescriptor('test', null); |
| 260 CacheEntry entry = new CacheEntry(); |
| 261 entry.setState(result, CacheState.ERROR); |
| 262 expect(entry.getState(result), CacheState.ERROR); |
| 263 expect(entry.getValue(result), isNull); |
| 264 } |
| 265 |
| 266 test_setState_invalid() { |
| 267 ResultDescriptor result = new ResultDescriptor('test', null); |
| 268 CacheEntry entry = new CacheEntry(); |
| 269 entry.setState(result, CacheState.INVALID); |
| 270 expect(entry.getState(result), CacheState.INVALID); |
| 271 expect(entry.getValue(result), isNull); |
| 272 } |
| 273 |
| 274 test_setState_valid() { |
| 275 ResultDescriptor result = new ResultDescriptor('test', null); |
| 276 CacheEntry entry = new CacheEntry(); |
| 277 expect(() => entry.setState(result, CacheState.VALID), throwsArgumentError); |
| 278 } |
| 279 |
| 280 test_setValue() { |
| 281 String value = 'value'; |
| 282 ResultDescriptor result = new ResultDescriptor('test', null); |
| 283 CacheEntry entry = new CacheEntry(); |
| 284 entry.setValue(result, value); |
| 285 expect(entry.getState(result), CacheState.VALID); |
| 286 expect(entry.getValue(result), value); |
| 287 } |
| 288 |
| 289 test_toString_empty() { |
| 290 CacheEntry entry = new CacheEntry(); |
| 291 expect(entry.toString(), isNotNull); |
| 292 } |
| 293 |
| 294 test_toString_nonEmpty() { |
| 295 String value = 'value'; |
| 296 ResultDescriptor result = new ResultDescriptor('test', null); |
| 297 CacheEntry entry = new CacheEntry(); |
| 298 entry.setValue(result, value); |
| 299 expect(entry.toString(), isNotNull); |
| 300 } |
| 301 } |
| 302 |
| 303 abstract class CachePartitionTest extends EngineTestCase { |
| 304 CachePartition createPartition([CacheRetentionPolicy policy = null]); |
| 305 |
| 306 void test_creation() { |
| 307 expect(createPartition(), isNotNull); |
| 308 } |
| 309 |
| 310 void test_entrySet() { |
| 311 CachePartition partition = createPartition(); |
| 312 AnalysisTarget target = new TestSource(); |
| 313 CacheEntry entry = new CacheEntry(); |
| 314 partition.put(target, entry); |
| 315 Map<AnalysisTarget, CacheEntry> entryMap = partition.map; |
| 316 expect(entryMap, hasLength(1)); |
| 317 AnalysisTarget entryKey = entryMap.keys.first; |
| 318 expect(entryKey, target); |
| 319 expect(entryMap[entryKey], entry); |
| 320 } |
| 321 |
| 322 void test_get() { |
| 323 CachePartition partition = createPartition(); |
| 324 AnalysisTarget target = new TestSource(); |
| 325 expect(partition.get(target), isNull); |
| 326 } |
| 327 |
| 328 void test_put_noFlush() { |
| 329 CachePartition partition = createPartition(); |
| 330 AnalysisTarget target = new TestSource(); |
| 331 CacheEntry entry = new CacheEntry(); |
| 332 partition.put(target, entry); |
| 333 expect(partition.get(target), entry); |
| 334 } |
| 335 |
| 336 void test_remove() { |
| 337 CachePartition partition = createPartition(); |
| 338 AnalysisTarget target = new TestSource(); |
| 339 CacheEntry entry = new CacheEntry(); |
| 340 partition.put(target, entry); |
| 341 expect(partition.get(target), entry); |
| 342 partition.remove(target); |
| 343 expect(partition.get(target), isNull); |
| 344 } |
| 345 |
| 346 void test_setMaxCacheSize() { |
| 347 CachePartition partition = |
| 348 createPartition(new TestCacheRetentionPolicy(RetentionPriority.LOW)); |
| 349 ResultDescriptor result = new ResultDescriptor('result', null); |
| 350 NullLiteral node = new NullLiteral(null); |
| 351 int size = 6; // Must be <= partition.maxCacheSize |
| 352 for (int i = 0; i < size; i++) { |
| 353 AnalysisTarget target = new TestSource("/test$i.dart"); |
| 354 CacheEntry entry = new CacheEntry(); |
| 355 entry.setValue(result, node); |
| 356 partition.put(target, entry); |
| 357 partition.accessedAst(target); |
| 358 } |
| 359 |
| 360 void assertNonFlushedCount(int expectedCount, CachePartition partition) { |
| 361 int nonFlushedCount = 0; |
| 362 Map<AnalysisTarget, CacheEntry> entryMap = partition.map; |
| 363 entryMap.values.forEach((CacheEntry entry) { |
| 364 if (entry.getState(result) != CacheState.FLUSHED) { |
| 365 nonFlushedCount++; |
| 366 } |
| 367 }); |
| 368 expect(nonFlushedCount, expectedCount); |
| 369 } |
| 370 |
| 371 assertNonFlushedCount(size, partition); |
| 372 int newSize = size - 2; |
| 373 partition.maxCacheSize = newSize; |
| 374 assertNonFlushedCount(newSize, partition); |
| 375 } |
| 376 |
| 377 void test_size() { |
| 378 CachePartition partition = createPartition(); |
| 379 int size = 4; |
| 380 for (int i = 0; i < size; i++) { |
| 381 AnalysisTarget target = new TestSource("/test$i.dart"); |
| 382 partition.put(target, new CacheEntry()); |
| 383 partition.accessedAst(target); |
| 384 } |
| 385 expect(partition.size(), size); |
| 386 } |
| 387 } |
| 388 |
| 389 @reflectiveTest |
| 390 class SdkCachePartitionTest extends CachePartitionTest { |
| 391 CachePartition createPartition([CacheRetentionPolicy policy = null]) { |
| 392 return new SdkCachePartition(null, 8); |
| 393 } |
| 394 |
| 395 void test_contains_false() { |
| 396 CachePartition partition = createPartition(); |
| 397 AnalysisTarget target = new TestSource(); |
| 398 expect(partition.contains(target), isFalse); |
| 399 } |
| 400 |
| 401 void test_contains_true() { |
| 402 SdkCachePartition partition = new SdkCachePartition(null, 8); |
| 403 SourceFactory factory = |
| 404 new SourceFactory([new DartUriResolver(DirectoryBasedDartSdk.defaultSdk)
]); |
| 405 AnalysisTarget target = factory.forUri("dart:core"); |
| 406 expect(partition.contains(target), isTrue); |
| 407 } |
| 408 } |
| 409 |
| 410 @reflectiveTest |
| 411 class UniversalCachePartitionTest extends CachePartitionTest { |
| 412 CachePartition createPartition([CacheRetentionPolicy policy = null]) { |
| 413 return new UniversalCachePartition(null, 8, policy); |
| 414 } |
| 415 |
| 416 void test_contains() { |
| 417 UniversalCachePartition partition = |
| 418 new UniversalCachePartition(null, 8, null); |
| 419 TestSource source = new TestSource(); |
| 420 expect(partition.contains(source), isTrue); |
| 421 } |
| 422 } |
| 423 |
| 424 @reflectiveTest |
| 425 class ResultDataTest extends EngineTestCase { |
| 426 test_creation() { |
| 427 String value = 'value'; |
| 428 ResultData data = new ResultData(new ResultDescriptor('test', value)); |
| 429 expect(data, isNotNull); |
| 430 expect(data.state, CacheState.INVALID); |
| 431 expect(data.value, value); |
| 432 } |
| 433 } |
| 434 |
| 435 @reflectiveTest |
| 436 class TestCacheRetentionPolicy extends CacheRetentionPolicy { |
| 437 final RetentionPriority policy; |
| 438 |
| 439 TestCacheRetentionPolicy([this.policy = RetentionPriority.MEDIUM]); |
| 440 |
| 441 @override |
| 442 RetentionPriority getAstPriority(AnalysisTarget target, CacheEntry entry) => |
| 443 policy; |
| 444 } |
| OLD | NEW |