|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | +) |
| 7 | + |
| 8 | +// statefulCreatorMethods contains methods that create stateful objects. |
| 9 | +// Refs created by these methods are tracked as stateful, meaning tests |
| 10 | +// using these refs depend on mutable state. |
| 11 | +var statefulCreatorMethods = map[string]bool{ |
| 12 | + "btck_context_create": true, |
| 13 | + "btck_chainstate_manager_create": true, |
| 14 | +} |
| 15 | + |
| 16 | +// stateMutatingMethods contains methods that mutate internal state. |
| 17 | +// Tests using these methods are assumed to affect all subsequent tests |
| 18 | +// and are included in dependency chains printed in verbose mode. |
| 19 | +var stateMutatingMethods = map[string]bool{ |
| 20 | + "btck_chainstate_manager_process_block": true, |
| 21 | +} |
| 22 | + |
| 23 | +// DependencyTracker manages test dependencies and builds request chains for verbose output. |
| 24 | +// It tracks both explicit ref dependencies and implicit state dependencies. |
| 25 | +type DependencyTracker struct { |
| 26 | + // refCreators maps reference names to the test index that created them |
| 27 | + refCreators map[string]int |
| 28 | + |
| 29 | + // statefulRefs tracks refs created by stateful methods. |
| 30 | + // Tests using these refs depend on mutable state. |
| 31 | + statefulRefs map[string]bool |
| 32 | + |
| 33 | + // depChains maps test index to its dependency chain (tests it depends on via ref usage) |
| 34 | + depChains map[int][]int |
| 35 | + |
| 36 | + // stateDependencies is a cumulative list of all tests affecting state (state-mutating |
| 37 | + // tests and their complete dependency chains) |
| 38 | + stateDependencies []int |
| 39 | +} |
| 40 | + |
| 41 | +// NewDependencyTracker creates a new dependency tracker |
| 42 | +func NewDependencyTracker() *DependencyTracker { |
| 43 | + return &DependencyTracker{ |
| 44 | + refCreators: make(map[string]int), |
| 45 | + statefulRefs: make(map[string]bool), |
| 46 | + depChains: make(map[int][]int), |
| 47 | + stateDependencies: []int{}, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +// BuildDependenciesForTest analyzes a test's parameters to build its complete transitive |
| 52 | +// dependency chain. When a test uses refs created by earlier tests, this records all direct |
| 53 | +// dependencies (tests that created those refs) and indirect dependencies (their dependencies). |
| 54 | +// Must be called after all previous tests have been processed. |
| 55 | +func (dt *DependencyTracker) BuildDependenciesForTest(testIndex int, test *TestCase) { |
| 56 | + // Build dependency chain for current test based on refs it uses |
| 57 | + var parentChains [][]int |
| 58 | + for _, ref := range extractRefsFromParams(test.Request.Params) { |
| 59 | + if creatorIdx, exists := dt.refCreators[ref]; exists { |
| 60 | + // Add the creator as a direct dependency |
| 61 | + parentChains = append(parentChains, []int{creatorIdx}) |
| 62 | + // Add transitive dependencies (creator's dependencies) |
| 63 | + if chain, hasChain := dt.depChains[creatorIdx]; hasChain { |
| 64 | + parentChains = append(parentChains, chain) |
| 65 | + } |
| 66 | + } else { |
| 67 | + panic(fmt.Sprintf("test %d (%s) uses undefined reference %s - no prior test created this ref", |
| 68 | + testIndex, test.Request.ID, ref)) |
| 69 | + } |
| 70 | + } |
| 71 | + dt.depChains[testIndex] = mergeSortedUnique(parentChains...) |
| 72 | +} |
| 73 | + |
| 74 | +// OnTestExecuted is called after a test executes successfully. It tracks the ref |
| 75 | +// created by the test, marks it as stateful if needed, and updates state dependencies |
| 76 | +// for state-mutating methods. |
| 77 | +func (dt *DependencyTracker) OnTestExecuted(testIndex int, test *TestCase) { |
| 78 | + // Track ref creation using the request's ref field |
| 79 | + if test.Request.Ref != "" { |
| 80 | + dt.refCreators[test.Request.Ref] = testIndex |
| 81 | + |
| 82 | + // Mark refs from stateful methods |
| 83 | + if statefulCreatorMethods[test.Request.Method] { |
| 84 | + dt.statefulRefs[test.Request.Ref] = true |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + // Track state-mutating tests and their dependencies |
| 89 | + if stateMutatingMethods[test.Request.Method] { |
| 90 | + mutatorChain := append(dt.depChains[testIndex], testIndex) |
| 91 | + dt.stateDependencies = mergeSortedUnique(dt.stateDependencies, mutatorChain) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// BuildRequestChain builds the complete dependency chain for a test |
| 96 | +func (dt *DependencyTracker) BuildRequestChain(testIndex int, allTests []TestCase) []int { |
| 97 | + refDepChain := dt.depChains[testIndex] |
| 98 | + |
| 99 | + // Only include state dependencies if the test's dep chain contains any stateful refs |
| 100 | + if dt.testUsesStatefulRefs(testIndex, allTests) { |
| 101 | + return mergeSortedUnique(refDepChain, dt.stateDependencies) |
| 102 | + } |
| 103 | + |
| 104 | + return refDepChain |
| 105 | +} |
| 106 | + |
| 107 | +// testUsesStatefulRefs checks if a test's dependency chain includes any stateful refs |
| 108 | +func (dt *DependencyTracker) testUsesStatefulRefs(testIndex int, allTests []TestCase) bool { |
| 109 | + // Check all tests in the dependency chain |
| 110 | + for _, depIdx := range dt.depChains[testIndex] { |
| 111 | + for _, ref := range extractRefsFromParams(allTests[depIdx].Request.Params) { |
| 112 | + if dt.statefulRefs[ref] { |
| 113 | + return true |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + // Check the test itself |
| 119 | + for _, ref := range extractRefsFromParams(allTests[testIndex].Request.Params) { |
| 120 | + if dt.statefulRefs[ref] { |
| 121 | + return true |
| 122 | + } |
| 123 | + } |
| 124 | + return false |
| 125 | +} |
| 126 | + |
| 127 | +// extractRefsFromParams extracts all reference names from params JSON. |
| 128 | +// Searches for ref objects with structure {"ref": "..."} at the first level of params. |
| 129 | +func extractRefsFromParams(params json.RawMessage) []string { |
| 130 | + var refs []string |
| 131 | + |
| 132 | + var paramsMap map[string]json.RawMessage |
| 133 | + if err := json.Unmarshal(params, ¶msMap); err != nil { |
| 134 | + return refs |
| 135 | + } |
| 136 | + |
| 137 | + for _, value := range paramsMap { |
| 138 | + if ref, ok := ParseRefObject(value); ok { |
| 139 | + refs = append(refs, ref) |
| 140 | + } |
| 141 | + } |
| 142 | + return refs |
| 143 | +} |
0 commit comments