Repository navigation
Expand file tree
/
Copy pathservice_run_stop_test.go
More file actions
211 lines (190 loc) · 7.93 KB
/
Copy pathservice_run_stop_test.go
File metadata and controls
211 lines (190 loc) · 7.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package kata
import (
"context"
"errors"
"fmt"
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestServiceRunTreatsCallerDeadlineAsCleanShutdown(t *testing.T) {
service, err := New(t.Context(), Config{
DSN: filepath.Join(t.TempDir(), "service.db"),
Auth: AuthConfig{TrustCallerAuthentication: true},
})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
runCtx, cancel := context.WithTimeout(t.Context(), 10*time.Millisecond) //nolint:kennlint // the deadline is the expected result; Run serves until the caller deadline ends it
defer cancel()
require.NoError(t, service.Run(runCtx))
}
// TestRunResultAppliesShutdownPolicy pins decision D1. The first case is the
// one the old Run got wrong: a worker that failed for a real reason was
// silenced because a shutdown happened to be in flight, so the error reached
// neither the caller nor a log.
func TestRunResultAppliesShutdownPolicy(t *testing.T) {
workerFailure := errors.New("github sync worker exploded")
fenceRejection := errors.New("worker transaction rejected")
for _, testCase := range []struct {
name string
stop runStop
workerResults []error
callerStop error
check func(t *testing.T, err error)
}{
{
name: "worker failure during shutdown reaches the caller",
stop: runStop{reason: stopContextDone, err: context.Canceled},
workerResults: []error{nil, workerFailure, nil},
callerStop: context.Canceled,
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, workerFailure)
},
},
{
name: "cancellation during shutdown is a clean stop",
stop: runStop{reason: stopContextDone, err: context.Canceled},
workerResults: []error{nil, nil, nil},
callerStop: context.Canceled,
check: func(t *testing.T, err error) { assert.NoError(t, err) },
},
{
name: "worker failure with no shutdown reaches the caller",
stop: runStop{reason: stopWorkerExit, err: workerFailure},
workerResults: []error{workerFailure, nil, nil},
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, workerFailure)
},
},
{
name: "fence rejection is reported as a fence failure",
stop: runStop{reason: stopFenceFailure, err: fenceRejection},
workerResults: []error{nil, nil, nil},
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, fenceRejection)
assert.ErrorContains(t, err, "worker transaction fence")
},
},
{
name: "fence rejection outranks a worker failure",
stop: runStop{reason: stopFenceFailure, err: fenceRejection},
workerResults: []error{workerFailure, nil, nil},
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, fenceRejection)
},
},
{
name: "a fence canceled by the shutdown it unwinds is not a failure",
stop: runStop{reason: stopFenceFailure, err: context.Canceled},
workerResults: []error{nil, nil, nil},
callerStop: context.Canceled,
check: func(t *testing.T, err error) { assert.NoError(t, err) },
},
{
name: "only cancellation causes from a fence are a clean shutdown",
stop: runStop{reason: stopFenceFailure, err: errors.Join(
context.Canceled, fmt.Errorf("stopping: %w", context.Canceled))},
workerResults: []error{nil, nil, nil},
callerStop: context.Canceled,
check: func(t *testing.T, err error) { assert.NoError(t, err) },
},
{
name: "a fence cancellation joined with a rejection still surfaces during shutdown",
stop: runStop{reason: stopFenceFailure, err: errors.Join(
context.Canceled, fenceRejection)},
workerResults: []error{nil, nil, nil},
callerStop: context.Canceled,
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, fenceRejection)
assert.ErrorContains(t, err, "worker transaction fence")
},
},
{
name: "a fence deadline joined with a rejection still surfaces during shutdown",
stop: runStop{reason: stopFenceFailure, err: errors.Join(
context.DeadlineExceeded, fenceRejection)},
workerResults: []error{nil, nil, nil},
callerStop: context.DeadlineExceeded,
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, fenceRejection)
assert.ErrorContains(t, err, "worker transaction fence")
},
},
{
name: "a worker-initiated fence cancellation still surfaces",
stop: runStop{reason: stopFenceFailure, err: context.Canceled},
workerResults: []error{nil, nil, nil},
check: func(t *testing.T, err error) {
require.ErrorIs(t, err, context.Canceled)
assert.ErrorContains(t, err, "worker transaction fence")
},
},
} {
t.Run(testCase.name, func(t *testing.T) {
testCase.check(t, runResult(
testCase.stop, testCase.workerResults, testCase.callerStop))
})
}
}
// TestNamedWorkerResultAttributesOnlyRealFailures pins the other half of the
// policy: cancellation is how a worker reports a clean stop, and anything else
// is named after the worker that produced it.
func TestNamedWorkerResultAttributesOnlyRealFailures(t *testing.T) {
worker := namedWorker{name: "github-sync"}
assert.NoError(t, worker.result(nil, nil))
assert.NoError(t, worker.result(context.Canceled, context.Canceled))
assert.NoError(t, worker.result(
fmt.Errorf("stopping: %w", context.Canceled), context.Canceled))
assert.NoError(t, worker.result(errors.Join(
context.Canceled, fmt.Errorf("stopping: %w", context.Canceled)), context.Canceled))
assert.NoError(t, worker.result(context.DeadlineExceeded, context.DeadlineExceeded))
failure := errors.New("boom")
err := worker.result(failure, nil)
require.ErrorIs(t, err, failure)
assert.ErrorContains(t, err, "github-sync worker")
joinedErr := worker.result(errors.Join(context.DeadlineExceeded, failure), context.DeadlineExceeded)
require.ErrorIs(t, joinedErr, failure)
assert.ErrorContains(t, joinedErr, "github-sync worker")
}
// TestFenceRecorderPreservesStopPriorityAcrossInternalUnwind pins the boundary
// between a fence failure that stopped Run and cancellation noise caused by
// Run unwinding a stop it already recorded.
func TestFenceRecorderPreservesStopPriorityAcrossInternalUnwind(t *testing.T) {
workerFailure := errors.New("federation worker failed")
fenceRejection := errors.New("worker transaction rejected")
resultAfterDrain := func(fence *fenceRecorder, stop runStop, workerResults []error) error {
if fenceErr := fence.first(); fenceErr != nil {
stop = runStop{reason: stopFenceFailure, err: fenceErr}
}
return runResult(stop, workerResults, nil)
}
t.Run("cancellation caused by internal unwind preserves the worker failure", func(t *testing.T) {
fence := &fenceRecorder{cancel: func() {}}
stop := runStop{reason: stopWorkerExit, err: workerFailure}
fence.beginUnwind()
require.ErrorIs(t, fence.record(context.Canceled), context.Canceled)
err := resultAfterDrain(fence, stop, []error{workerFailure})
require.ErrorIs(t, err, workerFailure)
assert.NotErrorIs(t, err, context.Canceled)
})
t.Run("mixed fence failure during internal unwind still outranks the worker", func(t *testing.T) {
fence := &fenceRecorder{cancel: func() {}}
stop := runStop{reason: stopWorkerExit, err: workerFailure}
fence.beginUnwind()
require.Error(t, fence.record(errors.Join(context.Canceled, fenceRejection)))
err := resultAfterDrain(fence, stop, []error{workerFailure})
require.ErrorIs(t, err, fenceRejection)
assert.ErrorContains(t, err, "worker transaction fence")
})
t.Run("fence cancellation before internal unwind remains the stop reason", func(t *testing.T) {
fence := &fenceRecorder{cancel: func() {}}
require.ErrorIs(t, fence.record(context.Canceled), context.Canceled)
fence.beginUnwind()
err := resultAfterDrain(
fence, runStop{reason: stopContextDone, err: context.Canceled}, []error{nil})
require.ErrorIs(t, err, context.Canceled)
assert.ErrorContains(t, err, "worker transaction fence")
})
}