func NewTestEndpoint(t *testing.T, addr string) *TestEndpoint {
	// shutdown chan size 1 so that Close() doesn't have to wait on the write
	// because the loops will typically be stuck in Accept and Readline
	return &TestEndpoint{
		t:              t,
		addr:           addr,
		seen:           make(chan []byte),
		seenBufs:       make([][]byte, 0),
		shutdown:       make(chan bool, 1),
		shutdownHandle: make(chan bool, 1),
		WhatHaveISeen:  make(chan bool),
		IHaveSeen:      make(chan [][]byte),
		accepts:        topic.New(),
		numSeen:        topic.New(),
	}
}
Ejemplo n.º 2
0
func TestSlowConsumerIsClosed(t *testing.T) {
	top := topic.New()
	defer close(top.Broadcast)

	consumer := make(chan interface{}, 1)
	top.Register(consumer)

	top.Broadcast <- "one"
	top.Broadcast <- "two"
	top.Broadcast <- "three"

	// consumer can queue at most one message, run() may be
	// processing at most one at a time; we send three messages to
	// guarantee an overflow, then wait until run() has drained
	// its queue ("three" might still be in processing, but it
	// must have processed "two", and thus triggered slow consumer
	// logic)
	for len(top.Broadcast) > 0 {
		runtime.Gosched()
	}

	one := <-consumer
	two := <-consumer
	three, ok := <-consumer
	if ok {
		t.Fatalf("Should have been closed as too slow already: %v %v %v", one, two, three)
	}

	// but worst case the consumer was just about to unregister then
	top.Unregister(consumer)
}
Ejemplo n.º 3
0
func TestClose(t *testing.T) {
	top := topic.New()
	consumer := make(chan interface{}, 1)
	top.Register(consumer)

	close(top.Broadcast)

	for msg := range consumer {
		t.Fatalf("Received after unregistering: %v", msg)
	}
}
Ejemplo n.º 4
0
func TestUnregister(t *testing.T) {
	top := topic.New()
	defer close(top.Broadcast)
	consumer := make(chan interface{}, 1)
	top.Register(consumer)
	top.Unregister(consumer)

	top.Broadcast <- "hello"
	msg, ok := <-consumer
	if ok {
		t.Fatalf("Received after unregistering: %v", msg)
	}
}
Ejemplo n.º 5
0
func TestAudienceOne(t *testing.T) {
	top := topic.New()
	defer close(top.Broadcast)
	consumer := make(chan interface{}, 1)
	top.Register(consumer)

	top.Broadcast <- "hello"
	msg := <-consumer
	got, ok := msg.(string)
	if !ok {
		t.Fatalf("Message is wrong type: %v", msg)
	}
	if got != "hello" {
		t.Errorf("Message has wrong content: %v", got)
	}
}
Ejemplo n.º 6
0
func TestAudienceEmpty(t *testing.T) {
	top := topic.New()
	defer close(top.Broadcast)
	top.Broadcast <- "crickets"
}