package pubsub import ( "fmt" "sync" "testing" ) func TestDupePublisher(t *testing.T) { pt := NewProgressTracker() if _, err := pt.Publish("foo"); err != nil { t.Fatalf("First publisher should not give error, err:%v", err) } if _, err := pt.Publish("foo"); err == nil { t.Fatal("Dupe publisher should give error but got nil") } if _, err := pt.Publish("bar"); err != nil { t.Fatalf("Different publisher should not give error, err:%v", err) } } func TestSubSub(t *testing.T) { pt := NewProgressTracker() c1 := pt.Subscribe("foo") select { //Not Working: expected 1 expression //case _, ok <- c1: case <-c1: default: } if c1 == nil { t.Fatal("Subscriber should not get a closed channel") } c2 := pt.Subscribe("foo") if c2 == nil { t.Fatal("Subscriber should not get a closed channel") } } func TestPubSub(t *testing.T) { pt := NewProgressTracker() pc, _ := pt.Publish("foo") if pc == nil { t.Fatal("Should not get nil channel") } sc := pt.Subscribe("foo") fmt.Printf("From test: chan is %+v and %#v\n", sc, sc) if sc == nil { t.Fatal("Should not get nil channel") } wg := sync.WaitGroup{} wg.Add(1) go func() { fmt.Println("Subscriber start") c := 0 for range sc { fmt.Println("Subscriber got msg") c++ } fmt.Println("Subscriber received close") if c == 0 { fmt.Println("Should have gotten update") } wg.Done() fmt.Println("Subscriber Done") }() fmt.Println("Producer Start") for i := 0; i < 10; i++ { fmt.Println("Producer Sent") pc <- "blah" } close(pc) fmt.Println("Producer close") t.Log("Now waiting") fmt.Println("Now waiting") wg.Wait() }