Example #1
0
func TestRegisterServiceInstance(t *testing.T) {
	rId := "test router"
	r, err := router.New(rId, ":14817", "")
	if err != nil {
		t.Fatal(err)
	}

	err = r.Start()
	if err != nil {
		t.Fatal("router start:", err)
	}
	defer func() {
		r.Stop()
		time.Sleep(time.Millisecond * 50)
	}()

	ins, err := instance.NewInstance("test instance", "test service", ":8888")
	if err != nil {
		t.Fatal(err)
	}

	cAddr := "127.0.0.1:14818"
	c, err := New(cAddr)
	if err != nil {
		t.Fatal(err)
	}

	err = c.RegisterServiceInstance(ins)
	if err != nil {
		t.Fatal(err)
	}

	err = c.RegisterServiceInstance(ins)
	assert.NotNil(t, err)
}
Example #2
0
func TestCRouter(t *testing.T) {
	rId := "test cRouter"
	r, _ := router.New(rId, ":14817", "")
	err := r.Start()
	if err != nil {
		t.Fatal("router start:", err)
	}
	defer r.Stop()

	cr, err := NewCRouter(rId, ":14817")
	if err != nil {
		t.Fatal(err)
	}

	expectedReply, _ := genRandomBytesSlice(4096)

	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write(expectedReply)
	}))
	defer ts.Close()

	sName := "test service"
	localAddr := "127.0.0.1:8080"
	remoteAddr := ts.Listener.Addr().String()

	mapTo, err := instance.NewInstance("test instance", sName, remoteAddr)
	if err != nil {
		t.Fatal(err)
	}

	// setting up service
	err = cr.AddService(sName, localAddr, router.NewRandomSelectPolicy())
	if err != nil {
		t.Fatal(err)
	}

	err = cr.AddServiceInstance(sName, mapTo)
	if err != nil {
		t.Fatal(err)
	}

	// testing service routing
	resp, err := http.Get("http://" + localAddr + "/")
	if err != nil {
		t.Fatal(err)
	}

	reply, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, reply, expectedReply)

	err = r.RemoveService(sName)
	if err != nil {
		t.Fatal(err)
	}
}
Example #3
0
func plotRouter() error {
	if len(os.Args) < 5 {
		return fmt.Errorf("Usage: demo r <cAddr> <port> <id>")
	}

	addr, err := getInterfaceAddr()
	if err != nil {
		return err
	}

	cAddr := os.Args[2]
	port := os.Args[3]
	rAddr := addr + port
	id := os.Args[4]

	if id != "rand" && id != "rr" {
		return fmt.Errorf("for the sake of this demo, only ids of 'rand' and 'rr' are supported: %v", id)
	}

	// start router
	r, err := router.New(id, rAddr, cAddr)
	if err != nil {
		return err
	}

	err = r.Start()
	if err != nil {
		return err
	}

	err = r.RegisterOnCtler()

	return err
}
Example #4
0
func TestRegisterRouter(t *testing.T) {
	rId := "test router"
	r, err := router.New(rId, ":14817", "")
	if err != nil {
		t.Fatal(err)
	}

	err = r.Start()
	if err != nil {
		t.Fatal("router start:", err)
	}
	defer func() {
		r.Stop()
		time.Sleep(time.Millisecond * 50)
	}()

	cr, err := NewCRouter(rId, ":14817")
	if err != nil {
		t.Fatal(err)
	}

	cAddr := "127.0.0.1:14818"
	c, err := New(cAddr)
	if err != nil {
		t.Fatal(err)
	}

	assert.Empty(t, c.Routers)

	err = c.RegisterRouter(cr)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, len(c.Routers), 1)
	assert.Equal(t, c.Routers["test router"], cr)

	err = c.RegisterRouter(cr)
	assert.NotNil(t, err)
}
Example #5
0
func TestRPC(t *testing.T) {
	r, err := router.New("id", ":14817", "")
	if err != nil {
		t.Fatal(err)
	}

	err = r.Start()
	if err != nil {
		t.Fatal("router start:", err)
	}
	defer func() {
		r.Stop()
		time.Sleep(time.Millisecond * 50)
	}()

	cAddr := "127.0.0.1:14818"
	c, err := New(cAddr)
	if err != nil {
		t.Fatal(err)
	}

	err = c.Start()
	if err != nil {
		t.Fatal(err)
	}
	defer c.Stop()

	client, err := rpc.Dial("tcp", cAddr)
	if err != nil {
		t.Fatal("dialing:", err)
	}

	regRouterArgs := &RegRouterArgs{
		Id:   "test router",
		Addr: ":14817",
	}

	assert.Empty(t, c.Routers)

	err = client.Call("ControllerRPC.RegisterRouter", regRouterArgs, nil)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, len(c.Routers), 1)
	assert.NotNil(t, c.Routers["test router"])

	mapTo, err := instance.NewInstance("test instance", "test service", ":8888")
	if err != nil {
		t.Fatal(err)
	}

	regInstanceArgs := &RegInstanceArgs{
		Instance: mapTo,
	}

	assert.Empty(t, c.ServiceInstances)

	err = client.Call("ControllerRPC.RegisterServiceInstance", regInstanceArgs, nil)
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, len(c.ServiceInstances), 1)
	assert.NotNil(t, c.ServiceInstances["test service"])
	assert.Equal(t, c.ServiceInstances["test service"][0], mapTo)
}
func testLBFunction(t *testing.T, option int) {
	rName := "test router"
	sName := "test service"
	expectedReply := []byte("hello, world")
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Write(expectedReply)
	}))
	defer ts.Close()
	remoteAddr := ts.Listener.Addr().String()

	mapTo, err := instance.NewInstance("test instance", sName, remoteAddr)
	if err != nil {
		t.Fatal(err)
	}

	cAddr := "127.0.0.1:14818"
	rAddr := "127.0.0.1:14817"

	r, err := router.New(rName, rAddr, cAddr)
	if err != nil {
		t.Fatal(err)
	}

	r.Start()
	defer r.Stop()

	c, err := controller.New(cAddr)
	if err != nil {
		t.Fatal(err)
	}

	c.Start()
	defer c.Stop()

	cr, err := controller.NewCRouter(rName, rAddr)
	if err != nil {
		t.Fatal(err)
	}

	lb := NewLoadBalancer(c)
	switch option {
	case 0:
		c.AddListener(controller.AddRouterEventType, lb.AddRouterListener)

		instances := make([]*instance.Instance, 0)
		c.ServiceInstances[sName] = append(instances, mapTo)

		c.RegisterRouter(cr)
	case 1:
		c.AddListener(controller.AddInstanceEventType, lb.AddInstanceListener)

		c.Routers[rName] = cr

		c.RegisterServiceInstance(mapTo)
	default:
		t.Fatal("Unknown option")
	}

	// http test
	resp, err := http.Get("http://127.0.0.1" + ServicePortMap[sName] + "/")
	if err != nil {
		t.Fatal(err)
	}

	reply, err := ioutil.ReadAll(resp.Body)
	resp.Body.Close()
	if err != nil {
		t.Fatal(err)
	}

	assert.Equal(t, reply, expectedReply)

	c.AddListener(controller.FailureInstanceEventType, lb.FailureInstanceListener)

	err = r.ReportFailure(mapTo)
	assert.Nil(t, err)
}