コード例 #1
0
ファイル: crouter_test.go プロジェクト: hongchaodeng/raccoon
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)
	}
}
コード例 #2
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)
}
コード例 #3
0
ファイル: demo.go プロジェクト: hongchaodeng/raccoon
// Service: "test service"
func plotInstance() error {
	if len(os.Args) < 4 {
		return fmt.Errorf("Usage: demo i <cAddr> <id>")
	}

	cAddr := os.Args[2]
	id := os.Args[3]
	service := "test service"

	// start http server
	addr, err := getInterfaceAddr()
	if err != nil {
		return err
	}

	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		fmt.Fprintf(w, "hello, world %v\n", id)
	})

	l, err := net.Listen("tcp", addr+":0")
	if err != nil {
		return err
	}
	go http.Serve(l, nil)

	iAddr := l.Addr().String()
	//fmt.Println("http address:", iAddr)

	// create instance
	instance, err := instance.NewInstance(id, service, iAddr)
	if err != nil {
		return err
	}

	// register instance to controller

	err = instance.RegisterOnCtler(cAddr)
	if err != nil {
		return err
	}

	resp, err := http.Get("http://" + iAddr + "/")
	if err != nil {
		return err
	}
	defer resp.Body.Close()

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

	log.Println(string(reply))

	return err
}
コード例 #4
0
ファイル: service_test.go プロジェクト: hongchaodeng/raccoon
func TestService(t *testing.T) {
	localAddr := "127.0.0.1:8080"

	expectedReply, err := genRandomBytesSlice(4096)
	if err != nil {
		t.Fatal(err)
	}

	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", "test", remoteAddr)
	if err != nil {
		t.Fatal(err)
	}

	s, err := newService("name", localAddr, NewRandomSelectPolicy(), nil)
	if err != nil {
		t.Fatal(err)
	}

	err = s.addInstance(mapTo)
	if err != nil {
		t.Fatal(err)
	}

	go s.start()

	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 = s.stop()
	if err != nil {
		t.Fatal(err)
	}
}
コード例 #5
0
ファイル: rpc_test.go プロジェクト: hongchaodeng/raccoon
func prepareRouterByRPC(routerAddr, sName, localAddr, remoteAddr string) error {
	mapTo, err := instance.NewInstance("test instance", "test service", remoteAddr)
	if err != nil {
		return err
	}

	//client, err := rpc.DialHTTP("tcp", routerAddr)
	client, err := rpc.Dial("tcp", routerAddr)
	if err != nil {
		return err
	}

	sArgs := &ServiceArgs{
		ServiceName: sName,
		LocalAddr:   localAddr,
		Policy:      NewRandomSelectPolicy(),
	}

	gob.Register(sArgs.Policy)

	err = client.Call("RouterRPC.AddService", sArgs, nil)
	if err != nil {
		return err
	}

	iArgs := &InstanceArgs{
		Instance: mapTo,
	}

	err = client.Call("RouterRPC.AddServiceInstance", iArgs, nil)
	if err != nil {
		return err
	}

	return nil
}
コード例 #6
0
ファイル: rpc_test.go プロジェクト: hongchaodeng/raccoon
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)
}
コード例 #7
0
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)
}