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)
|
2020-10-17 14:17:19 +00:00
|
|
|
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
|
2020-10-17 14:17:19 +00:00
|
|
|
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
|
2020-10-17 14:17:19 +00:00
|
|
|
stream.Unregister(output)
|
|
|
|
stream.DecrementClientCount()
|
2020-10-17 11:45:52 +00:00
|
|
|
|
|
|
|
// Check client count
|
2020-10-17 14:17:19 +00:00
|
|
|
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
|
|
|
}
|