|
22 | 22 | package internal
|
23 | 23 |
|
24 | 24 | import (
|
| 25 | + "context" |
25 | 26 | "testing"
|
26 | 27 | "time"
|
27 | 28 |
|
28 | 29 | "github.com/stretchr/testify/assert"
|
| 30 | + "github.com/stretchr/testify/mock" |
| 31 | + "github.com/stretchr/testify/require" |
29 | 32 | )
|
30 | 33 |
|
31 | 34 | func TestContextChildParentCancelRace(t *testing.T) {
|
@@ -128,3 +131,132 @@ func TestContextAddChildCancelParentRace(t *testing.T) {
|
128 | 131 | env.ExecuteWorkflow(wf)
|
129 | 132 | assert.NoError(t, env.GetWorkflowError())
|
130 | 133 | }
|
| 134 | + |
| 135 | +func TestContextCancellationOrderDeterminism(t *testing.T) { |
| 136 | + /* |
| 137 | + Previously, child-contexts were stored in a map, preventing deterministic order when propagating cancellation. |
| 138 | + The order of branches being selected in this test was random, both for the first event and in following ones. |
| 139 | +
|
| 140 | + In principle this should be fine, but it's possible for the effects of cancellation to trigger a selector's |
| 141 | + future-done callback, which currently records the *real-time*-first event as the branch to unblock, rather than |
| 142 | + doing something more safe by design (e.g. choosing based on state when the selector's goroutine is unblocked). |
| 143 | +
|
| 144 | + Unfortunately, we cannot change the selector's behavior without introducing non-backwards-compatible changes to |
| 145 | + currently-working workflows. |
| 146 | +
|
| 147 | + So the workaround for now is to maintain child-context order, so they are canceled in a consistent order. |
| 148 | + As this order was not controlled before, and Go does a pretty good job at randomizing map iteration order, |
| 149 | + converting non-determinism to determinism should be strictly no worse for backwards compatibility, and it |
| 150 | + fixes the issue for future executions. |
| 151 | + */ |
| 152 | + check := func(t *testing.T, separateStart, separateSelect bool) { |
| 153 | + env := newTestWorkflowEnv(t) |
| 154 | + act := func(ctx context.Context) error { |
| 155 | + return nil // will be mocked |
| 156 | + } |
| 157 | + wf := func(ctx Context) ([]int, error) { |
| 158 | + ctx, cancel := WithCancel(ctx) |
| 159 | + Go(ctx, func(ctx Context) { |
| 160 | + _ = Sleep(ctx, time.Minute) |
| 161 | + cancel() |
| 162 | + }) |
| 163 | + |
| 164 | + // start some activities, which will not complete before the timeout cancels them |
| 165 | + ctx = WithActivityOptions(ctx, ActivityOptions{ |
| 166 | + TaskList: "", |
| 167 | + ScheduleToCloseTimeout: time.Hour, |
| 168 | + ScheduleToStartTimeout: time.Hour, |
| 169 | + StartToCloseTimeout: time.Hour, |
| 170 | + }) |
| 171 | + s := NewSelector(ctx) |
| 172 | + var result []int |
| 173 | + for i := 0; i < 10; i++ { |
| 174 | + i := i |
| 175 | + // need a child context, a future alone is not enough as it does not become a child |
| 176 | + cctx, ccancel := WithCancel(ctx) |
| 177 | + |
| 178 | + s.AddFuture(ExecuteActivity(cctx, act), func(f Future) { |
| 179 | + ccancel() // TODO: is this necessary to prevent leaks? if it is, how can we make it not? |
| 180 | + err := f.Get(ctx, nil) |
| 181 | + if err == nil || !IsCanceledError(err) { |
| 182 | + // fail the test, this should not happen - activities must be canceled or it's not valid. |
| 183 | + t.Errorf("activity completion or failure for some reason other than cancel: %v", err) |
| 184 | + } |
| 185 | + result = append(result, i) |
| 186 | + }) |
| 187 | + |
| 188 | + if separateStart { |
| 189 | + // yield so they are submitted one at a time, in case that matters |
| 190 | + _ = Sleep(ctx, time.Second) |
| 191 | + } |
| 192 | + } |
| 193 | + for i := 0; i < 10; i++ { |
| 194 | + if separateSelect { |
| 195 | + // yield so they are selected one at a time, in case that matters |
| 196 | + _ = Sleep(ctx, time.Second) |
| 197 | + } |
| 198 | + s.Select(ctx) |
| 199 | + } |
| 200 | + |
| 201 | + return result, nil |
| 202 | + } |
| 203 | + env.RegisterWorkflow(wf) |
| 204 | + env.RegisterActivity(act) |
| 205 | + |
| 206 | + // activities must not complete in time |
| 207 | + env.OnActivity(act, mock.Anything).After(5 * time.Minute).Return(nil) |
| 208 | + |
| 209 | + env.ExecuteWorkflow(wf) |
| 210 | + require.NoError(t, env.GetWorkflowError()) |
| 211 | + var result []int |
| 212 | + require.NoError(t, env.GetWorkflowResult(&result)) |
| 213 | + require.NotEmpty(t, result) |
| 214 | + assert.Equal(t, 0, result[0], "first activity to be created should be the first one canceled") |
| 215 | + assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7, 8, 9}, result[1:], "other activities should finish in a consistent (but undefined) order") |
| 216 | + } |
| 217 | + |
| 218 | + type variant struct { |
| 219 | + name string |
| 220 | + separateStart bool |
| 221 | + separateSelect bool |
| 222 | + } |
| 223 | + // all variants expose this behavior, but being a bit more exhaustive in the face |
| 224 | + // of decision-scheduling differences seems good. |
| 225 | + for _, test := range []variant{ |
| 226 | + {"many in one decision", false, false}, |
| 227 | + {"many started at once, selected slowly", false, true}, |
| 228 | + {"started slowly, selected quickly", true, false}, |
| 229 | + {"started and selected slowly", true, true}, |
| 230 | + } { |
| 231 | + t.Run(test.name, func(t *testing.T) { |
| 232 | + check(t, test.separateStart, test.separateSelect) |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +func BenchmarkSliceMaintenance(b *testing.B) { |
| 238 | + // all essentially identical |
| 239 | + b.Run("append", func(b *testing.B) { |
| 240 | + data := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 241 | + for i := 0; i < b.N; i++ { |
| 242 | + data = append(data[:5], data[6:]...) |
| 243 | + data = append(data, i) // keep the slice the same size for all iterations |
| 244 | + } |
| 245 | + }) |
| 246 | + b.Run("copy", func(b *testing.B) { |
| 247 | + data := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 248 | + for i := 0; i < b.N; i++ { |
| 249 | + copy(data[5:], data[6:]) |
| 250 | + data = data[:9] // trim to actual size, as the last value is now duplicated. capacity is still 10. |
| 251 | + data = append(data, i) // keep the slice the same size for all iterations |
| 252 | + } |
| 253 | + }) |
| 254 | + b.Run("copy explicit capacity", func(b *testing.B) { |
| 255 | + data := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} |
| 256 | + for i := 0; i < b.N; i++ { |
| 257 | + copy(data[5:], data[6:]) |
| 258 | + data = data[:9:10] // trim to actual size, as the last value is now duplicated. explicitly reserve 10 cap. |
| 259 | + data = append(data, i) // keep the slice the same size for all iterations |
| 260 | + } |
| 261 | + }) |
| 262 | +} |
0 commit comments