|
| 1 | +// Copyright (c) 2017-2021 Uber Technologies Inc. |
| 2 | +// Portions of the Software are attributed to Copyright (c) 2020 Temporal Technologies Inc. |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +package internal |
| 23 | + |
| 24 | +import ( |
| 25 | + "testing" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/stretchr/testify/assert" |
| 29 | + "go.uber.org/zap/zaptest" |
| 30 | +) |
| 31 | + |
| 32 | +func TestContext_RaceRegression(t *testing.T) { |
| 33 | + /* |
| 34 | + A race condition existed due to concurrently ending goroutines on shutdown (i.e. closing their chan without waiting |
| 35 | + on them to finish shutdown), which executed... quite a lot of non-concurrency-safe code in a concurrent way. All |
| 36 | + decision-sensitive code is assumed to be run strictly sequentially. |
| 37 | +
|
| 38 | + Context cancellation was one identified by a customer, and it's fairly easy to test. |
| 39 | + In principle this must be safe to do - contexts are supposed to be concurrency-safe. Even if ours are not actually |
| 40 | + safe (for valid reasons), our execution model needs to ensure they *act* like it's safe. |
| 41 | + */ |
| 42 | + s := WorkflowTestSuite{} |
| 43 | + s.SetLogger(zaptest.NewLogger(t)) |
| 44 | + env := s.NewTestWorkflowEnvironment() |
| 45 | + wf := func(ctx Context) error { |
| 46 | + ctx, cancel := WithCancel(ctx) |
| 47 | + racyCancel := func(ctx Context) { |
| 48 | + defer cancel() // defer is necessary as Sleep will never return due to Goexit |
| 49 | + _ = Sleep(ctx, time.Hour) |
| 50 | + } |
| 51 | + // start a handful to increase odds of a race being detected |
| 52 | + for i := 0; i < 10; i++ { |
| 53 | + Go(ctx, racyCancel) |
| 54 | + } |
| 55 | + |
| 56 | + _ = Sleep(ctx, time.Minute) // die early |
| 57 | + return nil |
| 58 | + } |
| 59 | + env.RegisterWorkflow(wf) |
| 60 | + env.ExecuteWorkflow(wf) |
| 61 | + assert.NoError(t, env.GetWorkflowError()) |
| 62 | +} |
0 commit comments