Skip to content

Commit

Permalink
update unit tests per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
faec committed Jun 24, 2021
1 parent aac8300 commit 9f88e81
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions libbeat/publisher/queue/diskqueue/serialize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,42 @@ import (
)

// A test to make sure serialization works correctly on multi-byte characters.
func TestSerializeMultiByte(t *testing.T) {
asciiOnly := "{\"name\": \"Momotaro\"}"
multiBytes := "{\"name\": \"桃太郎\"}"

encoder := newEventEncoder()
event := publisher.Event{
Content: beat.Event{
Fields: common.MapStr{
"ascii_only": asciiOnly,
"multi_bytes": multiBytes,
},
},
}
serialized, err := encoder.encode(&event)
if err != nil {
t.Fatalf("Couldn't encode event: %v", err)
func TestSerialize(t *testing.T) {
testCases := []struct {
name string
value string
}{
{name: "Ascii only", value: "{\"name\": \"Momotaro\"}"},
{name: "Multi-byte", value: "{\"name\": \"桃太郎\"}"},
}

// Use decoder to decode the serialized bytes.
decoder := newEventDecoder()
buf := decoder.Buffer(len(serialized))
copy(buf, serialized)
decoded, err := decoder.Decode()
if err != nil {
t.Fatalf("Couldn't decode serialized data: %v", err)
}
for _, test := range testCases {
encoder := newEventEncoder()
event := publisher.Event{
Content: beat.Event{
Fields: common.MapStr{
"test_field": test.value,
},
},
}
serialized, err := encoder.encode(&event)
if err != nil {
t.Fatalf("[%v] Couldn't encode event: %v", test.name, err)
}

decodedAsciiOnly, _ := decoded.Content.Fields.GetValue("ascii_only")
assert.Equal(t, asciiOnly, decodedAsciiOnly)
// Use decoder to decode the serialized bytes.
decoder := newEventDecoder()
buf := decoder.Buffer(len(serialized))
copy(buf, serialized)
decoded, err := decoder.Decode()
if err != nil {
t.Fatalf("[%v] Couldn't decode serialized data: %v", test.name, err)
}

decodedMultiBytes, _ := decoded.Content.Fields.GetValue("multi_bytes")
assert.Equal(t, multiBytes, decodedMultiBytes)
decodedValue, err := decoded.Content.Fields.GetValue("test_field")
if err != nil {
t.Fatalf("[%v] Couldn't get field 'test_field': %v", test.name, err)
}
assert.Equal(t, test.value, decodedValue)
}
}

0 comments on commit 9f88e81

Please sign in to comment.