| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package status |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"sync" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component/componentstatus" |
| 14 |
) |
| 15 |
|
| 16 |
func TestStatusFSM(t *testing.T) { |
| 17 |
for _, tt := range []struct { |
| 18 |
name string |
| 19 |
reportedStatuses []componentstatus.Status |
| 20 |
expectedStatuses []componentstatus.Status |
| 21 |
expectedErrorCount int |
| 22 |
}{ |
| 23 |
{ |
| 24 |
name: "successful startup and shutdown", |
| 25 |
reportedStatuses: []componentstatus.Status{ |
| 26 |
componentstatus.StatusStarting, |
| 27 |
componentstatus.StatusOK, |
| 28 |
componentstatus.StatusStopping, |
| 29 |
componentstatus.StatusStopped, |
| 30 |
}, |
| 31 |
expectedStatuses: []componentstatus.Status{ |
| 32 |
componentstatus.StatusStarting, |
| 33 |
componentstatus.StatusOK, |
| 34 |
componentstatus.StatusStopping, |
| 35 |
componentstatus.StatusStopped, |
| 36 |
}, |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "component recovered", |
| 40 |
reportedStatuses: []componentstatus.Status{ |
| 41 |
componentstatus.StatusStarting, |
| 42 |
componentstatus.StatusRecoverableError, |
| 43 |
componentstatus.StatusOK, |
| 44 |
componentstatus.StatusStopping, |
| 45 |
componentstatus.StatusStopped, |
| 46 |
}, |
| 47 |
expectedStatuses: []componentstatus.Status{ |
| 48 |
componentstatus.StatusStarting, |
| 49 |
componentstatus.StatusRecoverableError, |
| 50 |
componentstatus.StatusOK, |
| 51 |
componentstatus.StatusStopping, |
| 52 |
componentstatus.StatusStopped, |
| 53 |
}, |
| 54 |
}, |
| 55 |
{ |
| 56 |
name: "repeated events are errors", |
| 57 |
reportedStatuses: []componentstatus.Status{ |
| 58 |
componentstatus.StatusStarting, |
| 59 |
componentstatus.StatusOK, |
| 60 |
componentstatus.StatusRecoverableError, |
| 61 |
componentstatus.StatusRecoverableError, |
| 62 |
componentstatus.StatusRecoverableError, |
| 63 |
componentstatus.StatusOK, |
| 64 |
componentstatus.StatusStopping, |
| 65 |
componentstatus.StatusStopped, |
| 66 |
}, |
| 67 |
expectedStatuses: []componentstatus.Status{ |
| 68 |
componentstatus.StatusStarting, |
| 69 |
componentstatus.StatusOK, |
| 70 |
componentstatus.StatusRecoverableError, |
| 71 |
componentstatus.StatusOK, |
| 72 |
componentstatus.StatusStopping, |
| 73 |
componentstatus.StatusStopped, |
| 74 |
}, |
| 75 |
expectedErrorCount: 2, |
| 76 |
}, |
| 77 |
{ |
| 78 |
name: "PermanentError is stoppable", |
| 79 |
reportedStatuses: []componentstatus.Status{ |
| 80 |
componentstatus.StatusStarting, |
| 81 |
componentstatus.StatusOK, |
| 82 |
componentstatus.StatusPermanentError, |
| 83 |
componentstatus.StatusOK, |
| 84 |
componentstatus.StatusStopping, |
| 85 |
}, |
| 86 |
expectedStatuses: []componentstatus.Status{ |
| 87 |
componentstatus.StatusStarting, |
| 88 |
componentstatus.StatusOK, |
| 89 |
componentstatus.StatusPermanentError, |
| 90 |
componentstatus.StatusStopping, |
| 91 |
}, |
| 92 |
expectedErrorCount: 1, |
| 93 |
}, |
| 94 |
{ |
| 95 |
name: "FatalError is terminal", |
| 96 |
reportedStatuses: []componentstatus.Status{ |
| 97 |
componentstatus.StatusStarting, |
| 98 |
componentstatus.StatusOK, |
| 99 |
componentstatus.StatusFatalError, |
| 100 |
componentstatus.StatusOK, |
| 101 |
}, |
| 102 |
expectedStatuses: []componentstatus.Status{ |
| 103 |
componentstatus.StatusStarting, |
| 104 |
componentstatus.StatusOK, |
| 105 |
componentstatus.StatusFatalError, |
| 106 |
}, |
| 107 |
expectedErrorCount: 1, |
| 108 |
}, |
| 109 |
{ |
| 110 |
name: "Stopped is terminal", |
| 111 |
reportedStatuses: []componentstatus.Status{ |
| 112 |
componentstatus.StatusStarting, |
| 113 |
componentstatus.StatusOK, |
| 114 |
componentstatus.StatusStopping, |
| 115 |
componentstatus.StatusStopped, |
| 116 |
componentstatus.StatusOK, |
| 117 |
}, |
| 118 |
expectedStatuses: []componentstatus.Status{ |
| 119 |
componentstatus.StatusStarting, |
| 120 |
componentstatus.StatusOK, |
| 121 |
componentstatus.StatusStopping, |
| 122 |
componentstatus.StatusStopped, |
| 123 |
}, |
| 124 |
expectedErrorCount: 1, |
| 125 |
}, |
| 126 |
} { |
| 127 |
t.Run(tt.name, func(t *testing.T) { |
| 128 |
var receivedStatuses []componentstatus.Status |
| 129 |
fsm := newFSM( |
| 130 |
func(ev *componentstatus.Event) { |
| 131 |
receivedStatuses = append(receivedStatuses, ev.Status()) |
| 132 |
}, |
| 133 |
) |
| 134 |
|
| 135 |
errorCount := 0 |
| 136 |
for _, status := range tt.reportedStatuses { |
| 137 |
if err := fsm.transition(componentstatus.NewEvent(status)); err != nil { |
| 138 |
errorCount++ |
| 139 |
require.ErrorIs(t, err, errInvalidStateTransition) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
require.Equal(t, tt.expectedErrorCount, errorCount) |
| 144 |
require.Equal(t, tt.expectedStatuses, receivedStatuses) |
| 145 |
}) |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
func TestValidSeqsToStopped(t *testing.T) { |
| 150 |
events := []*componentstatus.Event{ |
| 151 |
componentstatus.NewEvent(componentstatus.StatusStarting), |
| 152 |
componentstatus.NewEvent(componentstatus.StatusOK), |
| 153 |
componentstatus.NewEvent(componentstatus.StatusRecoverableError), |
| 154 |
componentstatus.NewEvent(componentstatus.StatusPermanentError), |
| 155 |
componentstatus.NewEvent(componentstatus.StatusFatalError), |
| 156 |
} |
| 157 |
|
| 158 |
for _, ev := range events { |
| 159 |
name := fmt.Sprintf("transition from: %s to: %s", ev.Status(), componentstatus.StatusStopped) |
| 160 |
t.Run(name, func(t *testing.T) { |
| 161 |
fsm := newFSM(func(*componentstatus.Event) {}) |
| 162 |
if ev.Status() != componentstatus.StatusStarting { |
| 163 |
require.NoError(t, fsm.transition(componentstatus.NewEvent(componentstatus.StatusStarting))) |
| 164 |
} |
| 165 |
require.NoError(t, fsm.transition(ev)) |
| 166 |
// skipping to stopped is not allowed |
| 167 |
err := fsm.transition(componentstatus.NewEvent(componentstatus.StatusStopped)) |
| 168 |
require.ErrorIs(t, err, errInvalidStateTransition) |
| 169 |
|
| 170 |
// stopping -> stopped is allowed for non-fatal errors |
| 171 |
err = fsm.transition(componentstatus.NewEvent(componentstatus.StatusStopping)) |
| 172 |
if ev.Status() == componentstatus.StatusFatalError { |
| 173 |
require.ErrorIs(t, err, errInvalidStateTransition) |
| 174 |
} else { |
| 175 |
require.NoError(t, err) |
| 176 |
require.NoError(t, fsm.transition(componentstatus.NewEvent(componentstatus.StatusStopped))) |
| 177 |
} |
| 178 |
}) |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
func TestStatusFuncs(t *testing.T) { |
| 183 |
id1 := &componentstatus.InstanceID{} |
| 184 |
id2 := &componentstatus.InstanceID{} |
| 185 |
|
| 186 |
actualStatuses := make(map[*componentstatus.InstanceID][]componentstatus.Status) |
| 187 |
statusFunc := func(id *componentstatus.InstanceID, ev *componentstatus.Event) { |
| 188 |
actualStatuses[id] = append(actualStatuses[id], ev.Status()) |
| 189 |
} |
| 190 |
|
| 191 |
statuses1 := []componentstatus.Status{ |
| 192 |
componentstatus.StatusStarting, |
| 193 |
componentstatus.StatusOK, |
| 194 |
componentstatus.StatusStopping, |
| 195 |
componentstatus.StatusStopped, |
| 196 |
} |
| 197 |
|
| 198 |
statuses2 := []componentstatus.Status{ |
| 199 |
componentstatus.StatusStarting, |
| 200 |
componentstatus.StatusOK, |
| 201 |
componentstatus.StatusRecoverableError, |
| 202 |
componentstatus.StatusOK, |
| 203 |
componentstatus.StatusStopping, |
| 204 |
componentstatus.StatusStopped, |
| 205 |
} |
| 206 |
|
| 207 |
expectedStatuses := map[*componentstatus.InstanceID][]componentstatus.Status{ |
| 208 |
id1: statuses1, |
| 209 |
id2: statuses2, |
| 210 |
} |
| 211 |
|
| 212 |
rep := NewReporter(statusFunc, |
| 213 |
func(err error) { |
| 214 |
require.NoError(t, err) |
| 215 |
}) |
| 216 |
comp1Func := NewReportStatusFunc(id1, rep.ReportStatus) |
| 217 |
comp2Func := NewReportStatusFunc(id2, rep.ReportStatus) |
| 218 |
|
| 219 |
for _, st := range statuses1 { |
| 220 |
comp1Func(componentstatus.NewEvent(st)) |
| 221 |
} |
| 222 |
|
| 223 |
for _, st := range statuses2 { |
| 224 |
comp2Func(componentstatus.NewEvent(st)) |
| 225 |
} |
| 226 |
|
| 227 |
require.Equal(t, expectedStatuses, actualStatuses) |
| 228 |
} |
| 229 |
|
| 230 |
func TestStatusFuncsConcurrent(t *testing.T) { |
| 231 |
ids := []*componentstatus.InstanceID{{}, {}, {}, {}} |
| 232 |
count := 0 |
| 233 |
statusFunc := func(*componentstatus.InstanceID, *componentstatus.Event) { |
| 234 |
count++ |
| 235 |
} |
| 236 |
rep := NewReporter(statusFunc, |
| 237 |
func(err error) { |
| 238 |
require.NoError(t, err) |
| 239 |
}) |
| 240 |
|
| 241 |
wg := sync.WaitGroup{} |
| 242 |
wg.Add(len(ids)) |
| 243 |
|
| 244 |
for _, id := range ids { |
| 245 |
go func() { |
| 246 |
compFn := NewReportStatusFunc(id, rep.ReportStatus) |
| 247 |
compFn(componentstatus.NewEvent(componentstatus.StatusStarting)) |
| 248 |
for range 1000 { |
| 249 |
compFn(componentstatus.NewEvent(componentstatus.StatusRecoverableError)) |
| 250 |
compFn(componentstatus.NewEvent(componentstatus.StatusOK)) |
| 251 |
} |
| 252 |
wg.Done() |
| 253 |
}() |
| 254 |
} |
| 255 |
|
| 256 |
wg.Wait() |
| 257 |
require.Equal(t, 8004, count) |
| 258 |
} |
| 259 |
|
| 260 |
func TestReportComponentOKIfStarting(t *testing.T) { |
| 261 |
for _, tt := range []struct { |
| 262 |
name string |
| 263 |
initialStatuses []componentstatus.Status |
| 264 |
expectedStatuses []componentstatus.Status |
| 265 |
}{ |
| 266 |
{ |
| 267 |
name: "matching condition: StatusStarting", |
| 268 |
initialStatuses: []componentstatus.Status{ |
| 269 |
componentstatus.StatusStarting, |
| 270 |
}, |
| 271 |
expectedStatuses: []componentstatus.Status{ |
| 272 |
componentstatus.StatusStarting, |
| 273 |
componentstatus.StatusOK, |
| 274 |
}, |
| 275 |
}, |
| 276 |
{ |
| 277 |
name: "non-matching condition StatusOK", |
| 278 |
initialStatuses: []componentstatus.Status{ |
| 279 |
componentstatus.StatusStarting, |
| 280 |
componentstatus.StatusOK, |
| 281 |
}, |
| 282 |
expectedStatuses: []componentstatus.Status{ |
| 283 |
componentstatus.StatusStarting, |
| 284 |
componentstatus.StatusOK, |
| 285 |
}, |
| 286 |
}, |
| 287 |
{ |
| 288 |
name: "non-matching condition RecoverableError", |
| 289 |
initialStatuses: []componentstatus.Status{ |
| 290 |
componentstatus.StatusStarting, |
| 291 |
componentstatus.StatusRecoverableError, |
| 292 |
}, |
| 293 |
expectedStatuses: []componentstatus.Status{ |
| 294 |
componentstatus.StatusStarting, |
| 295 |
componentstatus.StatusRecoverableError, |
| 296 |
}, |
| 297 |
}, |
| 298 |
{ |
| 299 |
name: "non-matching condition PermanentError", |
| 300 |
initialStatuses: []componentstatus.Status{ |
| 301 |
componentstatus.StatusStarting, |
| 302 |
componentstatus.StatusPermanentError, |
| 303 |
}, |
| 304 |
expectedStatuses: []componentstatus.Status{ |
| 305 |
componentstatus.StatusStarting, |
| 306 |
componentstatus.StatusPermanentError, |
| 307 |
}, |
| 308 |
}, |
| 309 |
} { |
| 310 |
t.Run(tt.name, func(t *testing.T) { |
| 311 |
var receivedStatuses []componentstatus.Status |
| 312 |
|
| 313 |
rep := NewReporter( |
| 314 |
func(_ *componentstatus.InstanceID, ev *componentstatus.Event) { |
| 315 |
receivedStatuses = append(receivedStatuses, ev.Status()) |
| 316 |
}, |
| 317 |
func(err error) { |
| 318 |
require.NoError(t, err) |
| 319 |
}, |
| 320 |
) |
| 321 |
|
| 322 |
id := &componentstatus.InstanceID{} |
| 323 |
for _, status := range tt.initialStatuses { |
| 324 |
rep.ReportStatus(id, componentstatus.NewEvent(status)) |
| 325 |
} |
| 326 |
|
| 327 |
rep.ReportOKIfStarting(id) |
| 328 |
|
| 329 |
require.Equal(t, tt.expectedStatuses, receivedStatuses) |
| 330 |
}) |
| 331 |
} |
| 332 |
} |