Exemplo n.º 1
0
func TestHandlerCancelsAfterGivenTimeWindow(t *testing.T) {
	ca := cache.NewCache()
	h := NewHandler(window, ca)

	ctx, _ := context.WithCancel(context.Background())
	c, s := net.Pipe()

	returned := make(chan struct{})

	go func() {
		err := h(ctx, s)
		if err != io.ErrClosedPipe {
			t.Errorf("expected ErrClosedPipe but got %s", err)
		}
		close(returned)
	}()

	data := string(parser.Send) + "name\n5\nhel"
	c.Write([]byte(data))

	select {
	case <-returned:
	case <-time.After(2 * window):
		t.Error("expected parser to timeout")
	}
}
Exemplo n.º 2
0
func TestHandlerCanHandleSubscribeAndReceive(t *testing.T) {
	ca := cache.NewCache()
	h := NewHandler(window, ca)

	ctx, cancel := context.WithCancel(context.Background())
	c, s := net.Pipe()

	returned := make(chan struct{})

	go func() {
		err := h(ctx, s)
		if err != nil {
			t.Error(err)
		}
		close(returned)
	}()

	data := string(parser.Subscribe) + "name\n"
	c.Write([]byte(data))

	// Make sure handler got the subscribe command.
	var got cache.Sub
	select {
	case got = <-ca.Subscribe:
	case <-time.After(time.Second):
		t.Error("send not received in time")
	}
	if got.Channel != "name" {
		t.Error("expected channel name")
	}

	// Reply on the receive channel.
	got.Receive <- writer.Response{writer.Send, "name", []byte("hello")}

	// Check if client received the message.
	expected := string(writer.Send) + "name\n5\nhello"
	buf := make([]byte, len(expected))
	_, err := c.Read(buf)
	if err != nil {
		t.Error(err)
	}

	if string(buf) != expected {
		t.Errorf("got %s but expected %s", string(buf), expected)
	}

	cancel()
	select {
	case <-returned:
	case <-time.After(time.Second):
		t.Error("handler did not return on context cancel")
	}
}
Exemplo n.º 3
0
func TestHandlerCanHandleSendCommands(t *testing.T) {
	ca := cache.NewCache()
	h := NewHandler(window, ca)

	ctx, cancel := context.WithCancel(context.Background())
	c, s := net.Pipe()

	returned := make(chan struct{})

	go func() {
		err := h(ctx, s)
		if err != nil {
			t.Error(err)
		}
		close(returned)
	}()

	data := string(parser.Send) + "name\n5\nhello"
	c.Write([]byte(data))

	// Make sure handler got the command.
	var got cache.Message
	select {
	case got = <-ca.Send:
	case <-time.After(time.Second):
		t.Error("send not received in time")
	}
	if got.Channel != "name" {
		t.Error("expected channel name")
	} else if string(got.Message) != "hello" {
		t.Error("expected message hello")
	}

	cancel()
	select {
	case <-returned:
	case <-time.After(time.Second):
		t.Error("handler did not return on context cancel")
	}
}
Exemplo n.º 4
0
func TestHandlerCanHandleSubUnsub(t *testing.T) {
	ca := cache.NewCache()
	h := NewHandler(window, ca)

	ctx, cancel := context.WithCancel(context.Background())
	c, s := net.Pipe()

	returned := make(chan struct{})

	go func() {
		err := h(ctx, s)
		if err != nil {
			t.Error(err)
		}
		close(returned)
	}()

	// This should only trigger one subscribe and one unsubscribe.
	firstUnsub := string(parser.Unsubscribe) + "name\n"
	thenSub := string(parser.Subscribe) + "name\n"
	thenSubAgain := string(parser.Subscribe) + "name\n"
	thenUnsub := string(parser.Unsubscribe) + "name\n"
	c.Write([]byte(firstUnsub + thenSub + thenSubAgain + thenUnsub))

	var sub cache.Sub
	var unsub cache.Unsub

	// Check for the subscribe.
	select {
	case sub = <-ca.Subscribe:
	case <-time.After(time.Second):
		t.Error("send not received in time")
	}
	if sub.Channel != "name" {
		t.Error("expected channel name")
	}

	// Check for the unsubscribe.
	select {
	case unsub = <-ca.Unsubscribe:
	case <-time.After(time.Second):
		t.Error("send not received in time")
	}
	if unsub.Channel != "name" {
		t.Error("expected channel name")
	}

	// Make sure there are no extra messages.
	select {
	case <-ca.Subscribe:
		t.Error("only expected one subscribe message")
	case <-ca.Unsubscribe:
		t.Error("only expected one unsubscribe message")
	default:
	}

	cancel()
	select {
	case <-returned:
	case <-time.After(time.Second):
		t.Error("handler did not return on context cancel")
	}
}