This repository has been archived by the owner on Nov 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added unit tests for canary message (#75)
Signed-off-by: Paolo Patierno <[email protected]>
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |