2020-04-12 20:40:52 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestBook(t *testing.T) {
|
|
|
|
b := NewBook()
|
|
|
|
|
|
|
|
buf1 := bytes.NewBufferString("buf1")
|
|
|
|
b.Join("moo", buf1)
|
|
|
|
b.loop()
|
|
|
|
if len(b.entries) != 1 {
|
|
|
|
t.Error("Wrong number of entries")
|
|
|
|
}
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 20:40:52 -06:00
|
|
|
// Send to an empty channel
|
|
|
|
b.Send("merf", []byte("goober"))
|
|
|
|
b.loop()
|
|
|
|
if buf1.String() != "buf1" {
|
|
|
|
t.Error("Sending to empty channel sent to non-empty channel")
|
|
|
|
}
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 20:40:52 -06:00
|
|
|
// Send to a non-empty channel!
|
|
|
|
b.Send("moo", []byte("goober"))
|
|
|
|
b.loop()
|
|
|
|
if buf1.String() != "buf1goober" {
|
|
|
|
t.Error("Sending didn't work")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Join another client
|
|
|
|
buf2 := bytes.NewBufferString("buf2")
|
|
|
|
b.Join("moo", buf2)
|
|
|
|
b.loop()
|
|
|
|
|
|
|
|
// Send to both
|
|
|
|
b.Send("moo", []byte("snerk"))
|
|
|
|
b.loop()
|
|
|
|
if buf1.String() != "buf1goobersnerk" {
|
|
|
|
t.Error("Send to 2-member channel busted", buf1)
|
|
|
|
}
|
|
|
|
if buf2.String() != "buf2snerk" {
|
|
|
|
t.Error("Send to 2-member channel busted", buf2)
|
|
|
|
}
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 20:40:52 -06:00
|
|
|
// Part a client
|
|
|
|
b.Part("moo", buf1)
|
|
|
|
b.loop()
|
2020-05-30 22:23:53 -06:00
|
|
|
|
2020-04-12 20:40:52 -06:00
|
|
|
b.Send("moo", []byte("peanut"))
|
|
|
|
b.loop()
|
|
|
|
if buf1.String() != "buf1goobersnerk" {
|
|
|
|
t.Error("Parted channel but still getting messages", buf1)
|
|
|
|
}
|
|
|
|
if buf2.String() != "buf2snerkpeanut" {
|
|
|
|
t.Error("Someone else parting somehow messed up sends", buf2)
|
|
|
|
}
|
|
|
|
}
|