Skip to content

Commit

Permalink
fix(test): use copy in mockbroker SetHandlersByMap
Browse files Browse the repository at this point in the history
Clone the given handlerMap (to prevent race conditions on modification),
and require further calls to SetHandlerByMap if the handlers need
updating mid test.
  • Loading branch information
dnwe committed Sep 22, 2021
1 parent 01c7c43 commit 8a2c8ba
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
3 changes: 3 additions & 0 deletions admin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1357,20 +1357,23 @@ 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)
}

// 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)
}

// 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)
Expand Down
6 changes: 5 additions & 1 deletion mockbroker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 8a2c8ba

Please sign in to comment.