Skip to content

Commit 81def48

Browse files
authored
Fixing Flaky AM test (#4865)
Signed-off-by: Alan Protasio <[email protected]> Signed-off-by: Alan Protasio <[email protected]>
1 parent 6c2d013 commit 81def48

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

pkg/alertmanager/distributor_test.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/stretchr/testify/require"
2020
"github.com/weaveworks/common/httpgrpc"
2121
"github.com/weaveworks/common/user"
22+
"go.uber.org/atomic"
2223
"google.golang.org/grpc"
2324
"google.golang.org/grpc/health/grpc_health_v1"
2425

@@ -333,11 +334,9 @@ func TestDistributor_IsPathSupported(t *testing.T) {
333334

334335
func prepare(t *testing.T, numAM, numHappyAM, replicationFactor int, responseBody []byte) (*Distributor, []*mockAlertmanager, func()) {
335336
ams := []*mockAlertmanager{}
336-
for i := 0; i < numHappyAM; i++ {
337-
ams = append(ams, newMockAlertmanager(i, true, responseBody))
338-
}
339-
for i := numHappyAM; i < numAM; i++ {
340-
ams = append(ams, newMockAlertmanager(i, false, responseBody))
337+
remainingFailure := atomic.NewInt32(int32(numAM - numHappyAM))
338+
for i := 0; i < numAM; i++ {
339+
ams = append(ams, newMockAlertmanager(i, remainingFailure, responseBody))
341340
}
342341

343342
// Use a real ring with a mock KV store to test ring RF logic.
@@ -399,15 +398,15 @@ type mockAlertmanager struct {
399398
receivedRequests map[string]map[int]int
400399
mtx sync.Mutex
401400
myAddr string
402-
happy bool
401+
remainingError *atomic.Int32
403402
responseBody []byte
404403
}
405404

406-
func newMockAlertmanager(idx int, happy bool, responseBody []byte) *mockAlertmanager {
405+
func newMockAlertmanager(idx int, remainingError *atomic.Int32, responseBody []byte) *mockAlertmanager {
407406
return &mockAlertmanager{
408407
receivedRequests: make(map[string]map[int]int),
409408
myAddr: fmt.Sprintf("127.0.0.1:%05d", 10000+idx),
410-
happy: happy,
409+
remainingError: remainingError,
411410
responseBody: responseBody,
412411
}
413412
}
@@ -427,7 +426,7 @@ func (am *mockAlertmanager) HandleRequest(_ context.Context, in *httpgrpc.HTTPRe
427426
am.receivedRequests[path] = m
428427
}
429428

430-
if am.happy {
429+
if am.remainingError.Dec() < 0 {
431430
m[http.StatusOK]++
432431
return &httpgrpc.HTTPResponse{
433432
Code: http.StatusOK,

0 commit comments

Comments
 (0)