github.com/nalind/docker@v1.5.0/pkg/pubsub/publisher_test.go (about)

     1  package pubsub
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestSendToOneSub(t *testing.T) {
     9  	p := NewPublisher(100*time.Millisecond, 10)
    10  	c := p.Subscribe()
    11  
    12  	p.Publish("hi")
    13  
    14  	msg := <-c
    15  	if msg.(string) != "hi" {
    16  		t.Fatalf("expected message hi but received %v", msg)
    17  	}
    18  }
    19  
    20  func TestSendToMultipleSubs(t *testing.T) {
    21  	p := NewPublisher(100*time.Millisecond, 10)
    22  	subs := []chan interface{}{}
    23  	subs = append(subs, p.Subscribe(), p.Subscribe(), p.Subscribe())
    24  
    25  	p.Publish("hi")
    26  
    27  	for _, c := range subs {
    28  		msg := <-c
    29  		if msg.(string) != "hi" {
    30  			t.Fatalf("expected message hi but received %v", msg)
    31  		}
    32  	}
    33  }
    34  
    35  func TestEvictOneSub(t *testing.T) {
    36  	p := NewPublisher(100*time.Millisecond, 10)
    37  	s1 := p.Subscribe()
    38  	s2 := p.Subscribe()
    39  
    40  	p.Evict(s1)
    41  	p.Publish("hi")
    42  	if _, ok := <-s1; ok {
    43  		t.Fatal("expected s1 to not receive the published message")
    44  	}
    45  
    46  	msg := <-s2
    47  	if msg.(string) != "hi" {
    48  		t.Fatalf("expected message hi but received %v", msg)
    49  	}
    50  }
    51  
    52  func TestClosePublisher(t *testing.T) {
    53  	p := NewPublisher(100*time.Millisecond, 10)
    54  	subs := []chan interface{}{}
    55  	subs = append(subs, p.Subscribe(), p.Subscribe(), p.Subscribe())
    56  	p.Close()
    57  
    58  	for _, c := range subs {
    59  		if _, ok := <-c; ok {
    60  			t.Fatal("expected all subscriber channels to be closed")
    61  		}
    62  	}
    63  }