ghostream/stream/messaging_test.go

43 lines
893 B
Go
Raw Normal View History

2020-10-17 08:02:38 +00:00
package stream
import (
"testing"
)
func TestWithoutOutputs(t *testing.T) {
stream := New()
defer stream.Close()
2020-10-17 08:21:40 +00:00
stream.Broadcast <- []byte("hello world")
2020-10-17 08:02:38 +00:00
}
func TestWithOneOutput(t *testing.T) {
stream := New()
defer stream.Close()
// Register one output
2020-10-17 08:21:40 +00:00
output := make(chan []byte, 64)
stream.Register(output)
stream.IncrementClientCount()
2020-10-17 08:02:38 +00:00
// Try to pass one message
2020-10-17 08:21:40 +00:00
stream.Broadcast <- []byte("hello world")
2020-10-17 08:02:38 +00:00
msg := <-output
2020-10-17 08:21:40 +00:00
if string(msg) != "hello world" {
t.Errorf("Message has wrong content: %s != hello world", msg)
2020-10-17 08:02:38 +00:00
}
2020-10-17 11:45:52 +00:00
// Check client count
if count := stream.ClientCount(); count != 1 {
2020-10-17 11:45:52 +00:00
t.Errorf("Client counter returned %d, expected 1", count)
}
2020-10-17 08:02:38 +00:00
// Unregister
stream.Unregister(output)
stream.DecrementClientCount()
2020-10-17 11:45:52 +00:00
// Check client count
if count := stream.ClientCount(); count != 0 {
2020-10-17 11:45:52 +00:00
t.Errorf("Client counter returned %d, expected 0", count)
}
2020-10-17 08:02:38 +00:00
}