From 8a2c8bae2c5f49d915fdb8121c3ad73e83c0938a Mon Sep 17 00:00:00 2001 From: Dominic Evans Date: Wed, 22 Sep 2021 14:46:16 +0100 Subject: [PATCH] fix(test): use copy in mockbroker SetHandlersByMap Clone the given handlerMap (to prevent race conditions on modification), and require further calls to SetHandlerByMap if the handlers need updating mid test. --- admin_test.go | 3 +++ mockbroker.go | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/admin_test.go b/admin_test.go index 791b30cd4..f6e06fd62 100644 --- a/admin_test.go +++ b/admin_test.go @@ -1357,6 +1357,7 @@ func TestDeleteOffset(t *testing.T) { // Test NoError handlerMap["DeleteOffsetsRequest"] = NewMockDeleteOffsetRequest(t).SetDeletedOffset(ErrNoError, topic, partition, ErrNoError) + seedBroker.SetHandlerByMap(handlerMap) err = admin.DeleteConsumerGroupOffset(group, topic, partition) if err != nil { t.Fatalf("DeleteConsumerGroupOffset failed with error %v", err) @@ -1364,6 +1365,7 @@ func TestDeleteOffset(t *testing.T) { // Test Error handlerMap["DeleteOffsetsRequest"] = NewMockDeleteOffsetRequest(t).SetDeletedOffset(ErrNotCoordinatorForConsumer, topic, partition, ErrNoError) + seedBroker.SetHandlerByMap(handlerMap) err = admin.DeleteConsumerGroupOffset(group, topic, partition) if err != ErrNotCoordinatorForConsumer { t.Fatalf("DeleteConsumerGroupOffset should have failed with error %v", ErrNotCoordinatorForConsumer) @@ -1371,6 +1373,7 @@ func TestDeleteOffset(t *testing.T) { // Test Error for partition handlerMap["DeleteOffsetsRequest"] = NewMockDeleteOffsetRequest(t).SetDeletedOffset(ErrNoError, topic, partition, ErrGroupSubscribedToTopic) + seedBroker.SetHandlerByMap(handlerMap) err = admin.DeleteConsumerGroupOffset(group, topic, partition) if err != ErrGroupSubscribedToTopic { t.Fatalf("DeleteConsumerGroupOffset should have failed with error %v", ErrGroupSubscribedToTopic) diff --git a/mockbroker.go b/mockbroker.go index f064c45b3..9166f6efb 100644 --- a/mockbroker.go +++ b/mockbroker.go @@ -83,9 +83,13 @@ func (b *MockBroker) SetLatency(latency time.Duration) { // and uses the found MockResponse instance to generate an appropriate reply. // If the request type is not found in the map then nothing is sent. func (b *MockBroker) SetHandlerByMap(handlerMap map[string]MockResponse) { + fnMap := make(map[string]MockResponse) + for k, v := range handlerMap { + fnMap[k] = v + } b.setHandler(func(req *request) (res encoderWithHeader) { reqTypeName := reflect.TypeOf(req.body).Elem().Name() - mockResponse := handlerMap[reqTypeName] + mockResponse := fnMap[reqTypeName] if mockResponse == nil { return nil }