You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
735 B
44 lines
735 B
package main
|
|
|
|
import (
|
|
"sync"
|
|
"testing"
|
|
)
|
|
|
|
func TestBroadcast(t *testing.T) {
|
|
bs := NewBroadcastString()
|
|
bs.WriteMessage("hello")
|
|
c1 := bs.AddListener(nil)
|
|
go func() {
|
|
bs.WriteMessage("world")
|
|
}()
|
|
message := <-c1
|
|
if message != "world" {
|
|
t.Fatalf("expect message world, but got %s", message)
|
|
}
|
|
c2 := bs.AddListener(nil)
|
|
go func() {
|
|
bs.WriteMessage("tab")
|
|
}()
|
|
|
|
// test write multi
|
|
wg := sync.WaitGroup{}
|
|
wg.Add(2)
|
|
go func() {
|
|
message = <-c2
|
|
if message != "tab" {
|
|
t.Errorf("expect tab, but got %s", message)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
|
|
go func() {
|
|
message = <-c1
|
|
if message != "tab" {
|
|
t.Errorf("expect tab, but got %s", message)
|
|
}
|
|
wg.Done()
|
|
}()
|
|
wg.Wait()
|
|
}
|