Exemplo n.º 1
0
func testSeperateRouter(b *testing.B, server_r *Router, client_r *Router, n int, m int) {
	name := "scheduler"
	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			var wg sync.WaitGroup
			for i := 0; i < m; i++ {
				req := pbt.NewResourceReq()
				req.Id = proto.Uint64(0)
				i := rand.Intn(n)
				if m == 1 {
					if _, err := client_r.CallWait(name+string(i), "rpc", req, 5); err != nil {
						b.Log(err)
						b.FailNow()
					}
				} else {
					wg.Add(1)
					client_r.Call(name+string(i), "rpc", req, ClientProcessReponseWaitGroup, &wg, 0)
				}
			}
			if m > 1 {
				wg.Wait()
			}
		}
	})

	client_r.Stop()
	server_r.Stop()
}
Exemplo n.º 2
0
func TestRouterSingle(t *testing.T) {
	r, err := NewRouter(nil, ServiceProcessPayload)
	if err != nil {
		t.FailNow()
	}

	hf := NewRPCHeaderFactory(NewProtobufFactory())

	name := "scheduler"
	network := "tcp"
	address := "localhost:10000"

	r.Run()

	if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil {
		t.Log(err)
		t.FailNow()
	}
	if err := r.Dial(name, network, address, hf); err != nil {
		t.Log(err)
		t.FailNow()
	}

	req := pbt.NewResourceReq()
	req.Id = proto.Uint64(1)

	done := make(chan bool)
	r.Call("scheduler", "rpc", req, ClientProcessReponse, done, 0)
	<-done

	r.Stop()
}
Exemplo n.º 3
0
func BenchmarkPipeReadWriter(b *testing.B) {
	s, c := net.Pipe()

	ch_c_w := make(chanPayload, 1024)
	ch_s_w := make(chanPayload, 1024)
	ch_d := make(chanPayload, 1024)

	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	ep_c := NewEndPoint("c", c, ch_c_w, ch_d, hf, nil, nil)
	ep_s := NewEndPoint("s", s, ch_s_w, ch_s_w, hf, nil, nil)

	ep_c.Run()
	ep_s.Run()

	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		req := pbt.NewResourceReq()
		req.Id = proto.Uint64(1)
		for pb.Next() {
			ch_c_w <- req
			<-ch_d
		}
	})

}
Exemplo n.º 4
0
func TestMockMsg(t *testing.T) {
	pr, pw := io.Pipe()
	ch := make(chanPayload, 128)

	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	w := NewWriter(pw, ch, hf.NewBuffer(), nil)
	r := NewReader(pr, ch, hf.NewBuffer(), nil)

	w.Run()
	r.Run()

	req := pbt.NewResourceReq()
	req.Id = proto.Uint64(10000)
	w.Write(req)
	<-time.After(1 * time.Second)

	select {
	case resp := <-ch:
		if resp.(*pbt.ResourceReq).GetId() != 10000 {
			t.Log(req, resp)
			t.Fail()
		}
	case <-time.After(1 * time.Second):
		t.Log("timeout")
		t.FailNow()
	}

	r.Stop()
	w.Stop()
}
Exemplo n.º 5
0
func BenchmarkTCPReconnectRouter(b *testing.B) {
	r, err := NewRouter(nil, ServiceProcessPayload)
	if err != nil {
		b.FailNow()
	}
	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	r.Run()
	<-time.Tick(1 * time.Millisecond)

	network := "tcp"
	address := "localhost:10000"
	if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil {
		b.Log(err)
		b.FailNow()
	}

	<-time.Tick(1 * time.Millisecond)

	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			name := "scheduler"

			req := pbt.NewResourceReq()
			req.Id = proto.Uint64(1)

			r, err := NewRouter(nil, ServiceProcessPayload)
			if err != nil {
				b.FailNow()
			}

			hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

			r.Run()
			if err := r.Dial(name, network, address, hf); err != nil {
				b.Log(err)
				b.FailNow()
			}

			if _, err := r.CallWait(name, "rpc", req, 5); err != nil {
				b.Log(err)
				b.FailNow()
			}
			r.Stop()
		}
	})

	r.Stop()
}
Exemplo n.º 6
0
func ServiceProcessPayload(r *Router, name string, p Payload) Payload {
	if req, ok := p.(*pbt.ResourceReq); ok {
		resp := pbt.NewResourceResp()
		resp.Id = proto.Uint64(req.GetId())
		return resp
	} else if b, ok := p.([]byte); ok {
		req := pbt.NewResourceReq()
		if proto.Unmarshal(b, req) != nil {
			panic("proto.Unmarshal error")
		}
		resp := pbt.NewResourceResp()
		resp.Id = proto.Uint64(req.GetId())
		return resp
	} else {
		panic("ServiceProcessPayload receieve wrong info")
	}
}
Exemplo n.º 7
0
func TestRouterMultiple(t *testing.T) {
	r, err := NewRouter(nil, ServiceProcessPayload)
	if err != nil {
		t.FailNow()
	}

	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	name := "scheduler"
	network := "tcp"
	address := "localhost:10000"

	r.Run()

	if err := r.ListenAndServe("client", network, address, hf, ServiceProcessConn); err != nil {
		t.Log(err)
		t.FailNow()
	}
	if err := r.Dial(name, network, address, hf); err != nil {
		t.Log(err)
		t.FailNow()
	}

	time.Sleep(1)

	for i := 1; i < 10000; i++ {
		req := pbt.NewResourceReq()
		req.Id = proto.Uint64(uint64(i))
		if _, err := r.CallWait(name, "rpc", req, 5); err != nil {
			t.Log(i, ":", err)
			t.FailNow()
		}
	}

	r.DelEndPoint("scheduler")

	r.DelListener("client")

	r.Stop()
}
Exemplo n.º 8
0
func BenchmarkTCPReadWriter(b *testing.B) {
	network := "tcp"
	address := "localhost:10008"

	l, err := net.Listen(network, address)
	if err != nil {
		b.FailNow()
	}
	c, err := net.Dial(network, address)
	if err != nil {
		b.FailNow()
	}
	s, err := l.Accept()
	if err != nil {
		b.FailNow()
	}

	ch_c_w := make(chanPayload, 1024)
	ch_s_w := make(chanPayload, 1024)
	ch_d := make(chanPayload, 1024)

	hf := NewMsgHeaderFactory(pbt.NewMsgProtobufFactory())

	ep_c := NewEndPoint("c", c, ch_c_w, ch_d, hf, nil, nil)
	ep_s := NewEndPoint("s", s, ch_s_w, ch_s_w, hf, nil, nil)

	ep_c.Run()
	ep_s.Run()

	b.ResetTimer()
	b.RunParallel(func(pb *testing.PB) {
		req := pbt.NewResourceReq()
		req.Id = proto.Uint64(1)
		for pb.Next() {
			ch_c_w <- req
			<-ch_d
		}
	})

}