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 11:45:52 +00:00
|
|
|
stream.Register(output, false)
|
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.Count(); count != 1 {
|
|
|
|
t.Errorf("Client counter returned %d, expected 1", count)
|
|
|
|
}
|
|
|
|
|
2020-10-17 08:02:38 +00:00
|
|
|
// Unregister
|
2020-10-17 11:45:52 +00:00
|
|
|
stream.Unregister(output, false)
|
|
|
|
|
|
|
|
// Check client count
|
|
|
|
if count := stream.Count(); count != 0 {
|
|
|
|
t.Errorf("Client counter returned %d, expected 0", count)
|
|
|
|
}
|
2020-10-17 08:02:38 +00:00
|
|
|
}
|