|
| 1 | +// Copyright (c) 2025, 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 | +import 'dart:math'; |
| 6 | + |
| 7 | +import 'package:build/build.dart' show AssetId; |
| 8 | +import 'package:test/test.dart'; |
| 9 | + |
| 10 | +import 'invalidation_tester.dart'; |
| 11 | + |
| 12 | +/// Tests correctness of invalidation over many randomly generated scenarios. |
| 13 | +/// |
| 14 | +/// The test builders write output that is a list of files read/resolved and their |
| 15 | +/// content hashes. This does two things: it ensures that if any input changes, |
| 16 | +/// the output changes; and it makes it possible to determine what will |
| 17 | +/// invalidate that output. |
| 18 | +/// |
| 19 | +/// In this way the test can know what output changes to assert due to a |
| 20 | +/// particular input change. |
| 21 | +Future<void> main() async { |
| 22 | + for (var iteration = 0; iteration != 500; ++iteration) { |
| 23 | + test('invalidation stress test $iteration', () async { |
| 24 | + final tester = InvalidationTester()..logSetup(); |
| 25 | + final random = Random(iteration); |
| 26 | + |
| 27 | + // Whether to change the imports in source files between builds. |
| 28 | + final changeImports = iteration % 10 == 0; |
| 29 | + |
| 30 | + // How many checked in sources and how many builders; the build is |
| 31 | + // a rectangle of size numberOfSources x numberOfBuilders. |
| 32 | + final numberOfSources = random.nextInt(8) + 1; |
| 33 | + final numberOfBuilders = random.nextInt(4) + 1; |
| 34 | + |
| 35 | + final sources = [ |
| 36 | + for (var i = 0; i != numberOfSources; ++i) 'a${i + 1}.1', |
| 37 | + ]; |
| 38 | + |
| 39 | + // Inputs that can be randomly read/resolved. |
| 40 | + // `numberOfBuilders + 2` to add some reads/resolves of files that |
| 41 | + // don't exist. |
| 42 | + final pickableInputs = <String>[]; |
| 43 | + for (var i = 1; i != (numberOfBuilders + 2); ++i) { |
| 44 | + for (final source in sources) { |
| 45 | + pickableInputs.add(source.replaceAll('.1', '.$i')); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // All outputs. |
| 50 | + final outputs = <String>[]; |
| 51 | + for (var i = 2; i != (numberOfBuilders + 1); ++i) { |
| 52 | + for (final source in sources) { |
| 53 | + outputs.add(source.replaceAll('.1', '.$i')); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + // Picks a list of files to import from `pickableInputs`. |
| 58 | + List<String> randomImportList() { |
| 59 | + final result = <String>[]; |
| 60 | + final length = random.nextInt(numberOfSources); |
| 61 | + for (var i = 0; i != length; ++i) { |
| 62 | + result.add(pickableInputs[random.nextInt(pickableInputs.length)]); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + tester |
| 68 | + ..sources(sources) |
| 69 | + ..pickableInputs(pickableInputs); |
| 70 | + |
| 71 | + // Set up builders. |
| 72 | + for (var i = 1; i != numberOfBuilders; ++i) { |
| 73 | + tester.builder( |
| 74 | + from: '.$i', |
| 75 | + to: '.${i + 1}', |
| 76 | + // Cover optional+required builders. |
| 77 | + isOptional: random.nextBool(), |
| 78 | + // Cover builders with hidden+visible output. |
| 79 | + outputIsVisible: random.nextBool(), |
| 80 | + ) |
| 81 | + // Use the input as the seed for what additional reads to do, |
| 82 | + // so reads won't change between identical runs. |
| 83 | + ..readsForSeedThenReadsRandomly('.$i') |
| 84 | + ..writes('.${i + 1}'); |
| 85 | + } |
| 86 | + |
| 87 | + // Initial random import graph. |
| 88 | + final importGraph = { |
| 89 | + for (var source in pickableInputs) source: randomImportList(), |
| 90 | + }; |
| 91 | + tester.importGraph(importGraph); |
| 92 | + |
| 93 | + // Initial build should succeed. |
| 94 | + expect((await tester.build()).succeeded, true); |
| 95 | + |
| 96 | + // Do five additional builds making changes and checking what was written. |
| 97 | + for (var build = 0; build != 5; ++build) { |
| 98 | + // Pick which source to change, compute expected outputs. |
| 99 | + final sourceToChange = sources[random.nextInt(sources.length)]; |
| 100 | + final expectedOutputs = <String>{}; |
| 101 | + |
| 102 | + Future<void> addExpectedOutputs(String invalidatedInput) async { |
| 103 | + for (final output in outputs) { |
| 104 | + final assetId = AssetId('pkg', 'lib/$output.dart'); |
| 105 | + final hiddenAssetId = AssetId( |
| 106 | + 'pkg', |
| 107 | + '.dart_tool/build/generated/pkg/lib/$output.dart', |
| 108 | + ); |
| 109 | + final outputContents = |
| 110 | + tester.readerWriter!.testing.exists(assetId) |
| 111 | + ? tester.readerWriter!.testing.readString(assetId) |
| 112 | + : tester.readerWriter!.testing.exists(hiddenAssetId) |
| 113 | + ? tester.readerWriter!.testing.readString(hiddenAssetId) |
| 114 | + : ''; |
| 115 | + // The test builder output is a list of "$name,$hash" for each input |
| 116 | + // that was read, including transitively resolved sources. Check it |
| 117 | + // for [input]. If found, this output is invalidated: recursively |
| 118 | + // add its invalidated outputs. |
| 119 | + if (outputContents.contains('$invalidatedInput.dart,')) { |
| 120 | + if (expectedOutputs.add(output)) { |
| 121 | + await addExpectedOutputs(output); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + await addExpectedOutputs(sourceToChange); |
| 128 | + |
| 129 | + // If [changeImports] then change some imports; it's only possible to |
| 130 | + // change imports for files that will be output. |
| 131 | + if (changeImports && expectedOutputs.isNotEmpty) { |
| 132 | + final sourceToChangeImports = |
| 133 | + expectedOutputs.toList()[random.nextInt(expectedOutputs.length)]; |
| 134 | + importGraph[sourceToChangeImports] = randomImportList(); |
| 135 | + tester.importGraph(importGraph); |
| 136 | + } |
| 137 | + |
| 138 | + // Build and check exactly the expected outputs change. |
| 139 | + expect( |
| 140 | + await tester.build(change: sourceToChange), |
| 141 | + Result(written: expectedOutputs), |
| 142 | + ); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | +} |
0 commit comments