125 lines
2.4 KiB
Go
125 lines
2.4 KiB
Go
package journal
|
|
|
|
import (
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"src.lwithers.me.uk/go/journal/testsink"
|
|
)
|
|
|
|
// TestConn opens a connection to a test sink and writes one message,
|
|
// ensuring it is received.
|
|
func TestConn(t *testing.T) {
|
|
sockpath := filepath.Join(t.TempDir(), "socket")
|
|
sink, err := testsink.New(sockpath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer sink.Stop()
|
|
|
|
conn, err := Connect(sockpath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
conn.Entry(PriInfo, "hello, world", nil)
|
|
|
|
msg, err := sink.Message(0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msgText, attrs, err := msg.Decode()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if msgText != "hello, world" {
|
|
t.Errorf("unexpected message text %q", msgText)
|
|
}
|
|
val, ok := testsink.GetAttr(attrs, "PRIORITY")
|
|
switch {
|
|
case !ok:
|
|
t.Error("missing PRIORITY attribute")
|
|
case val != "6":
|
|
t.Error("unexpected PRIORITY value")
|
|
}
|
|
|
|
if t.Failed() {
|
|
for i := range attrs {
|
|
t.Errorf("attr %q=%q", attrs[i].Key, attrs[i].Val)
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestEntryBinary ensures that we can write a message with an attribute encoded
|
|
// as a binary field.
|
|
func TestEntryBinary(t *testing.T) {
|
|
sockpath := filepath.Join(t.TempDir(), "socket")
|
|
sink, err := testsink.New(sockpath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer sink.Stop()
|
|
|
|
conn, err := Connect(sockpath)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer conn.Close()
|
|
|
|
expAttrs := []Attr{
|
|
Attr{
|
|
Key: MustAttrKey("SHORT"),
|
|
Value: []byte("short val"),
|
|
},
|
|
Attr{
|
|
Key: MustAttrKey("BINARY"),
|
|
Value: []byte("string with\n=embedded newline\nrequires binary protocol\n"),
|
|
},
|
|
Attr{
|
|
Key: MustAttrKey("LAST"),
|
|
Value: []byte("last\n"),
|
|
},
|
|
}
|
|
conn.Entry(PriDebug, "hello, binary world", expAttrs)
|
|
|
|
msg, err := sink.Message(0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
msgText, attrs, err := msg.Decode()
|
|
if err != nil {
|
|
t.Error(err)
|
|
}
|
|
if msgText != "hello, binary world" {
|
|
t.Errorf("unexpected message text %q", msgText)
|
|
}
|
|
|
|
val, ok := testsink.GetAttr(attrs, "PRIORITY")
|
|
switch {
|
|
case !ok:
|
|
t.Error("missing PRIORITY attribute")
|
|
case val != "7":
|
|
t.Error("unexpected PRIORITY value")
|
|
}
|
|
|
|
for i := range expAttrs {
|
|
key, expVal := expAttrs[i].Key.Key(), string(expAttrs[i].Value)
|
|
val, ok = testsink.GetAttr(attrs, key)
|
|
switch {
|
|
case !ok:
|
|
t.Errorf("missing %s attribute", key)
|
|
case val != expVal:
|
|
t.Errorf("unexpected %s value", key)
|
|
}
|
|
}
|
|
|
|
if t.Failed() {
|
|
for i := range attrs {
|
|
t.Errorf("attr %q=%q", attrs[i].Key, attrs[i].Val)
|
|
}
|
|
}
|
|
}
|