Skip to content
This repository has been archived by the owner on Oct 9, 2023. It is now read-only.

Commit

Permalink
improve test code coverage by adding test function in sandbox process…
Browse files Browse the repository at this point in the history
…or adn publisher

Signed-off-by: Future Outlier <eric901201@gmai.com>
  • Loading branch information
Future Outlier committed Aug 9, 2023
1 parent c2a9301 commit 22d84e7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ func TestSandboxProcessor_StartProcessing(t *testing.T) {
assert.Nil(t, testSandboxProcessor.(*SandboxProcessor).run())
}

func TestSandboxProcessor_StartProcessingMessageError(t *testing.T) {
msgChan := make(chan []byte, 1)
invalidProtoMessage := []byte("invalid message")
msgChan <- invalidProtoMessage
testSandboxProcessor := NewSandboxProcessor(msgChan, &mockSandboxEmailer)

assert.NotNil(t, testSandboxProcessor.(*SandboxProcessor).run())
}

func TestSandboxProcessor_StartProcessingEmailError(t *testing.T) {
msgChan := make(chan []byte, 1)
msgChan <- msg
Expand Down
18 changes: 18 additions & 0 deletions pkg/async/notifications/implementations/sandbox_publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ import (
"context"
"testing"

"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)

// mockMessage is a dummy proto message that will always fail to marshal
type mockMessage struct{}

func (m *mockMessage) Reset() {}
func (m *mockMessage) String() string { return "mockMessage" }
func (m *mockMessage) ProtoMessage() {}
func (m *mockMessage) Marshal() ([]byte, error) { return nil, errors.New("forced marshal error") }

func TestSandboxPublisher_Publish(t *testing.T) {
msgChan := make(chan []byte, 1)
publisher := NewSandboxPublisher(msgChan)
Expand All @@ -16,3 +25,12 @@ func TestSandboxPublisher_Publish(t *testing.T) {
assert.NotZero(t, len(msgChan))
assert.Nil(t, err)
}

func TestSandboxPublisher_PublishMarshalError(t *testing.T) {
msgChan := make(chan []byte, 1)
publisher := NewSandboxPublisher(msgChan)

err := publisher.Publish(context.Background(), "testMarshallError", &mockMessage{})
assert.Error(t, err)
assert.Equal(t, "forced marshal error", err.Error())
}

0 comments on commit 22d84e7

Please sign in to comment.