Skip to content

Fixing Flaky AM test #4865

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions pkg/alertmanager/distributor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/weaveworks/common/httpgrpc"
"github.com/weaveworks/common/user"
"go.uber.org/atomic"
"google.golang.org/grpc"
"google.golang.org/grpc/health/grpc_health_v1"

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

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

// Use a real ring with a mock KV store to test ring RF logic.
Expand Down Expand Up @@ -399,15 +398,15 @@ type mockAlertmanager struct {
receivedRequests map[string]map[int]int
mtx sync.Mutex
myAddr string
happy bool
remainingError *atomic.Int32
responseBody []byte
}

func newMockAlertmanager(idx int, happy bool, responseBody []byte) *mockAlertmanager {
func newMockAlertmanager(idx int, remainingError *atomic.Int32, responseBody []byte) *mockAlertmanager {
return &mockAlertmanager{
receivedRequests: make(map[string]map[int]int),
myAddr: fmt.Sprintf("127.0.0.1:%05d", 10000+idx),
happy: happy,
remainingError: remainingError,
responseBody: responseBody,
}
}
Expand All @@ -427,7 +426,7 @@ func (am *mockAlertmanager) HandleRequest(_ context.Context, in *httpgrpc.HTTPRe
am.receivedRequests[path] = m
}

if am.happy {
if am.remainingError.Dec() < 0 {
m[http.StatusOK]++
return &httpgrpc.HTTPResponse{
Code: http.StatusOK,
Expand Down