Skip to content

Commit

Permalink
fix flaky tests
Browse files Browse the repository at this point in the history
  • Loading branch information
roblaszczak committed Sep 25, 2023
1 parent 976f3c2 commit d2879fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
7 changes: 4 additions & 3 deletions components/cqrs/command_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cqrs_test

import (
"context"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -366,7 +367,7 @@ func TestNewCommandProcessor_OnHandle(t *testing.T) {
return nil
})

onHandleCalled := 0
onHandleCalled := int64(0)

config := cqrs.CommandProcessorConfig{
GenerateSubscribeTopic: func(params cqrs.CommandProcessorGenerateSubscribeTopicParams) (string, error) {
Expand All @@ -376,7 +377,7 @@ func TestNewCommandProcessor_OnHandle(t *testing.T) {
return mockSub, nil
},
OnHandle: func(params cqrs.CommandProcessorOnHandleParams) error {
onHandleCalled++
atomic.AddInt64(&onHandleCalled, 1)

assert.IsType(t, &TestCommand{}, params.Command)
assert.Equal(t, "cqrs_test.TestCommand", params.CommandName)
Expand Down Expand Up @@ -422,7 +423,7 @@ func TestNewCommandProcessor_OnHandle(t *testing.T) {
// nack received
}

assert.Equal(t, 2, onHandleCalled)
assert.EqualValues(t, 2, onHandleCalled)
}

func TestCommandProcessor_AddHandlersToRouter_without_disableRouterAutoAddHandlers(t *testing.T) {
Expand Down
20 changes: 13 additions & 7 deletions components/requestreply/requestreply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,20 +370,22 @@ func TestRequestReply_without_result_multiple_replies(t *testing.T) {
DoNotAckOnCommandErrors: true,
})

i := 0
type toSend struct {
ID string
Err error
}

toSendCh := make(chan toSend, 1)
toSendCh <- toSend{ID: "1", Err: fmt.Errorf("error 1")}

err := ts.CommandProcessor.AddHandlers(
requestreply.NewCommandHandlerWithResult[TestCommand, TestCommandResult](
"test_handler",
ts.RequestReplyBackend,
func(ctx context.Context, cmd *TestCommand) (TestCommandResult, error) {
i++

if i == 3 {
return TestCommandResult{ID: fmt.Sprintf("%d", i)}, nil
}
toSend := <-toSendCh

return TestCommandResult{ID: fmt.Sprintf("%d", i)}, fmt.Errorf("error %d", i)
return TestCommandResult{ID: toSend.ID}, toSend.Err
},
),
)
Expand Down Expand Up @@ -413,6 +415,8 @@ func TestRequestReply_without_result_multiple_replies(t *testing.T) {
t.Fatal("timeout")
}

toSendCh <- toSend{ID: "2", Err: fmt.Errorf("error 2")}

select {
case reply := <-replyCh:
assert.EqualValues(t, TestCommandResult{ID: "2"}, reply.HandlerResult)
Expand All @@ -425,6 +429,8 @@ func TestRequestReply_without_result_multiple_replies(t *testing.T) {
t.Fatal("timeout")
}

toSendCh <- toSend{ID: "3", Err: nil}

select {
case reply := <-replyCh:
assert.EqualValues(t, TestCommandResult{ID: "3"}, reply.HandlerResult)
Expand Down

0 comments on commit d2879fb

Please sign in to comment.