Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
Added unit tests for canary message (#75)
Browse files Browse the repository at this point in the history
Signed-off-by: Paolo Patierno <[email protected]>
  • Loading branch information
ppatierno authored Aug 26, 2021
1 parent 3979a2c commit b2269f2
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions internal/services/canary_message_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Copyright Strimzi authors.
// License: Apache License 2.0 (see the file LICENSE or http://apache.org/licenses/LICENSE-2.0.html).
//

// Package services defines an interface for canary services and related implementations
package services

import (
"testing"

"github.com/Shopify/sarama"
)

func TestCanaryMessage(t *testing.T) {
cm := CanaryMessage{
ProducerID: "producer-id",
MessageID: 0,
Timestamp: 12345,
}
want := "{\"producerId\":\"producer-id\",\"messageId\":0,\"timestamp\":12345}"
if cm.Json() != want {
t.Errorf("JSON got = %s, want = %s", cm.Json(), want)
}
}

func TestCanaryMessageEncode(t *testing.T) {
cm := CanaryMessage{
ProducerID: "producer-id",
MessageID: 0,
Timestamp: 12345,
}
encoder := sarama.StringEncoder(cm.Json())
bytes, _ := encoder.Encode()

decodedCm := NewCanaryMessage(bytes)

if cm != decodedCm {
t.Errorf("got = %v, want = %v", decodedCm, cm)
}

wrong := "{\"producerId\":\"producer-id\",\"messageId\":1,\"timestamp\":67890}"

encoder = sarama.StringEncoder(wrong)
bytes, _ = encoder.Encode()

decodedCm = NewCanaryMessage(bytes)

if cm == decodedCm {
t.Errorf("got %v should be different from %v", decodedCm, cm)
}
}

0 comments on commit b2269f2

Please sign in to comment.