Example #1
0
func TestRegisterWithMetadata(t *testing.T) {
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "echo",
		Params: protocol.Params{"msg": "foobar"},
	}
	server := buildTestServer(rw, req)

	err := server.RegisterWithMetadata(
		"echo",
		handler.HandlerFunc(echoHandler),
		map[string]interface{}{"datacenter": "eu"},
	)

	if err != nil {
		t.Fatalf("unexpected error %s", err)
	}

	if _, ok := server.mux.hdlrs["echo"]; !ok {
		t.Errorf("'echo' not found in mux handlers")
	}

	if _, ok := server.metas["echo"]; !ok {
		t.Errorf("'echo' not found in metas")
	}
}
Example #2
0
func TestLoadBalancer(t *testing.T) {
	lb := New(staticResolver{}, firstStrategy{})
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "foo",
	}

	sender := lb(dummy.New(rw, req))

	_, err := sender.Send("nanoTest", req)
	if err != nil {
		t.Fatalf("Unexcpected failure %s", err)
	}
}
Example #3
0
func TestAnnounce(t *testing.T) {
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "echo",
		Params: protocol.Params{"msg": "foobar"},
	}
	server := buildTestServer(rw, req)

	err := server.Register(
		"echo",
		handler.HandlerFunc(echoHandler),
	)

	if err != nil {
		t.Fatalf("unexpected error %s", err)
	}

	err = server.RegisterWithMetadata(
		"demo",
		handler.HandlerFunc(echoHandler),
		map[string]interface{}{"datacenter": "eu"},
	)

	if err != nil {
		t.Fatalf("unexpected error %s", err)
	}

	an := &dummyAnnouncer{}
	server.ServeAndAnnounce(an)

	if _, ok := an.instances["echo"]; ok {
		t.Errorf("echo not announced correctly")
	}

	if inst, ok := an.instances["demo"]; ok {
		t.Errorf("demo not announced correctly")

		if inst.Meta["datacenter"] != "eu" {
			t.Errorf("instance metadata expected to contain 'datacenter' else was %s", inst.Meta["datacenter"])
		}
	}
}
Example #4
0
func TestServeHandler(t *testing.T) {
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "echo",
		Params: protocol.Params{"msg": "foobar"},
	}
	server := buildTestServer(rw, req)

	err := server.Register("echo", handler.HandlerFunc(echoHandler))
	if err != nil {
		t.Fatalf("unexpected failure %s", err)
	}

	go server.Serve()

	<-time.After(1 * time.Second)

	if rw.Data.(string) != "foobar" {
		t.Errorf("handler want %q, got %q", "foobar", rw.Data)
	}
}
Example #5
0
func TestMultipleRegister(t *testing.T) {
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "echo",
		Params: protocol.Params{"msg": "foobar"},
	}
	server := buildTestServer(rw, req)

	err := server.Register("echo", handler.HandlerFunc(echoHandler))
	if err != nil {
		t.Errorf("unexpected failure %s", err)
	}

	if _, ok := server.mux.hdlrs["echo"]; !ok {
		t.Errorf("'echo' not found in mux handlers")
	}

	err = server.Register("echo", handler.HandlerFunc(echoHandler))
	if err == nil {
		t.Errorf("expected to fail when registering with same name")
	}
}
Example #6
0
func TestServeUnknownHandler(t *testing.T) {
	// FIXME:
	t.Skip("Fail on go tip")
	rw := dummy.NewResponseRecorder()
	req := &protocol.Request{
		Method: "UnknownMethod",
	}
	server := buildTestServer(rw, req)

	err := server.Register("echo", handler.HandlerFunc(echoHandler))
	if err != nil {
		t.Fatalf("unexpected failure %s", err)
	}

	go server.Serve()

	<-time.After(1 * time.Second)

	if rw.Error == nil || rw.Error.Error() != `Unknown handler "UnknownMethod"` {
		t.Errorf("expected error got %q", rw.Error)
	}
}