|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +// Tests that concurrent describe calls do not race on the describer's flow-map |
| 4 | +// state. The describer used to install package-level currentFlowsByOrigin / |
| 5 | +// currentFlowsByDest maps for the duration of a describe; two goroutines |
| 6 | +// running formatMicroflowActivities in parallel would clobber each other's |
| 7 | +// maps mid-traversal. |
| 8 | +// |
| 9 | +// This test drives formatMicroflowActivities from many goroutines at once. |
| 10 | +// It is primarily useful under `go test -race`, where the previous global- |
| 11 | +// state implementation reported a data race on currentFlowsByOrigin / |
| 12 | +// currentFlowsByDest. After threading the maps as explicit parameters the |
| 13 | +// race disappears; the test also asserts per-goroutine output integrity. |
| 14 | +package executor |
| 15 | + |
| 16 | +import ( |
| 17 | + "strings" |
| 18 | + "sync" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "github.com/mendixlabs/mxcli/model" |
| 22 | + "github.com/mendixlabs/mxcli/sdk/microflows" |
| 23 | +) |
| 24 | + |
| 25 | +func TestFormatMicroflowActivities_Concurrent_NoRace(t *testing.T) { |
| 26 | + // Build two distinct microflows whose activities are anchored to |
| 27 | + // different sides. If the two describe calls share state, one will |
| 28 | + // emit the other's anchor keyword. |
| 29 | + mfA := mkRaceMicroflow("mfa-start", "mfa-log", "mfa-end", AnchorRight) |
| 30 | + mfB := mkRaceMicroflow("mfb-start", "mfb-log", "mfb-end", AnchorBottom) |
| 31 | + |
| 32 | + e := newTestExecutor() |
| 33 | + ctx := e.newExecContext(t.Context()) |
| 34 | + |
| 35 | + const workersPerFlow = 12 |
| 36 | + |
| 37 | + var wg sync.WaitGroup |
| 38 | + resultsA := make([]string, workersPerFlow) |
| 39 | + resultsB := make([]string, workersPerFlow) |
| 40 | + for i := 0; i < workersPerFlow; i++ { |
| 41 | + wg.Add(2) |
| 42 | + go func(i int) { |
| 43 | + defer wg.Done() |
| 44 | + resultsA[i] = strings.Join(formatMicroflowActivities(ctx, mfA, nil, nil), "\n") |
| 45 | + }(i) |
| 46 | + go func(i int) { |
| 47 | + defer wg.Done() |
| 48 | + resultsB[i] = strings.Join(formatMicroflowActivities(ctx, mfB, nil, nil), "\n") |
| 49 | + }(i) |
| 50 | + } |
| 51 | + wg.Wait() |
| 52 | + |
| 53 | + wantA := "@anchor(from: right, to: left)" |
| 54 | + wantB := "@anchor(from: bottom, to: left)" |
| 55 | + for i, got := range resultsA { |
| 56 | + if !strings.Contains(got, wantA) { |
| 57 | + t.Errorf("worker %d (A) missing %q in output:\n%s", i, wantA, got) |
| 58 | + } |
| 59 | + if strings.Contains(got, wantB) { |
| 60 | + t.Errorf("worker %d (A) unexpectedly contains %q from flow B:\n%s", i, wantB, got) |
| 61 | + } |
| 62 | + } |
| 63 | + for i, got := range resultsB { |
| 64 | + if !strings.Contains(got, wantB) { |
| 65 | + t.Errorf("worker %d (B) missing %q in output:\n%s", i, wantB, got) |
| 66 | + } |
| 67 | + if strings.Contains(got, wantA) { |
| 68 | + t.Errorf("worker %d (B) unexpectedly contains %q from flow A:\n%s", i, wantA, got) |
| 69 | + } |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// mkRaceMicroflow builds a minimal microflow (Start → Log → End) whose |
| 74 | +// sequence-flow origin index matches the given anchorSide, so concurrent |
| 75 | +// describe outputs are distinguishable by their @anchor line. |
| 76 | +func mkRaceMicroflow(startID, logID, endID string, originSide int) *microflows.Microflow { |
| 77 | + start := µflows.StartEvent{BaseMicroflowObject: mkObj(startID)} |
| 78 | + log := µflows.ActionActivity{ |
| 79 | + BaseActivity: microflows.BaseActivity{BaseMicroflowObject: mkObj(logID)}, |
| 80 | + Action: µflows.LogMessageAction{ |
| 81 | + LogLevel: "Info", |
| 82 | + LogNodeName: "'App'", |
| 83 | + MessageTemplate: &model.Text{ |
| 84 | + Translations: map[string]string{"en_US": "hi"}, |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + end := µflows.EndEvent{BaseMicroflowObject: mkObj(endID)} |
| 89 | + |
| 90 | + // Flow start→log carries the distinguishing originSide on the LOG side |
| 91 | + // (where @anchor(from: ...) is emitted). |
| 92 | + flowSL := µflows.SequenceFlow{ |
| 93 | + OriginID: mkID(startID), |
| 94 | + DestinationID: mkID(logID), |
| 95 | + OriginConnectionIndex: AnchorRight, |
| 96 | + DestinationConnectionIndex: AnchorLeft, |
| 97 | + } |
| 98 | + flowLE := µflows.SequenceFlow{ |
| 99 | + OriginID: mkID(logID), |
| 100 | + DestinationID: mkID(endID), |
| 101 | + OriginConnectionIndex: originSide, |
| 102 | + DestinationConnectionIndex: AnchorLeft, |
| 103 | + } |
| 104 | + |
| 105 | + return µflows.Microflow{ |
| 106 | + Name: "MF_" + logID, |
| 107 | + ObjectCollection: µflows.MicroflowObjectCollection{ |
| 108 | + Objects: []microflows.MicroflowObject{start, log, end}, |
| 109 | + Flows: []*microflows.SequenceFlow{flowSL, flowLE}, |
| 110 | + }, |
| 111 | + } |
| 112 | +} |
0 commit comments