123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- package rpc
- import (
- "context"
- "net"
- "sync"
- "testing"
- "time"
- "go-common/library/conf/env"
- "go-common/library/naming"
- "go-common/library/naming/discovery"
- xtime "go-common/library/time"
- . "github.com/smartystreets/goconvey/convey"
- )
- var c = &discovery.Config{
- Nodes: []string{"api.bilibili.co"},
- Zone: "sh001",
- Env: "test",
- Key: "0c4b8fe3ff35a4b6",
- Secret: "b370880d1aca7d3a289b9b9a7f4d6812",
- Host: "host_1",
- }
- var in = &naming.Instance{
- AppID: "test2",
- Version: "1",
- Metadata: map[string]string{
- "test": "1",
- "weight": "8",
- "color": "",
- "cluster": "red",
- },
- }
- var in2 = &naming.Instance{
- AppID: "test3",
- Version: "1",
- Metadata: map[string]string{
- "test": "1",
- "weight": "8",
- "color": "",
- "cluster": "red",
- },
- }
- var (
- svrAddr1, svrAddr2, svrAddr3 string
- once1, once2, once3 sync.Once
- )
- type TestArgs struct {
- A, B int
- }
- type TestReply struct {
- C int
- }
- type TestTimeout struct {
- T time.Duration
- }
- type TestRPC int
- func startTestServer1() {
- svr := newServer()
- svr.RegisterName("RPC", new(TestRPC))
- var l net.Listener
- l, svrAddr1 = listenTCP()
- go svr.Accept(l)
- }
- func TestDiscoveryCli(t *testing.T) {
- env.Hostname = "host_1"
- env.Zone = "sh001"
- once1.Do(startTestServer1)
- Convey("test discovery cli", t, func() {
- once1.Do(startTestServer1)
- in.Addrs = []string{scheme + "://" + svrAddr1}
- dis := discovery.New(c)
- _, err := dis.Register(context.TODO(), in)
- So(err, ShouldBeNil)
- cli := NewDiscoveryCli("test2", &ClientConfig{
- Cluster: "",
- Timeout: xtime.Duration(time.Second),
- })
- time.Sleep(time.Second * 2)
- args := &TestArgs{7, 8}
- reply := new(TestReply)
- err = cli.Call(context.TODO(), "RPC.Add", args, reply)
- So(err, ShouldBeNil)
- })
- Convey("test discovery no zone", t, func() {
- env.Zone = "test2"
- cli := NewDiscoveryCli("test2", &ClientConfig{
- Cluster: "",
- Timeout: xtime.Duration(time.Second),
- })
- time.Sleep(time.Second * 2)
- args := &TestArgs{7, 8}
- reply := new(TestReply)
- err := cli.Call(context.TODO(), "RPC.Add", args, reply)
- So(err, ShouldBeNil)
- })
- Convey("test discovery with color", t, func() {
- env.Zone = "test2"
- cli := NewDiscoveryCli("test2", &ClientConfig{
- Color: "red",
- Timeout: xtime.Duration(time.Second),
- })
- time.Sleep(time.Second * 2)
- args := &TestArgs{7, 8}
- reply := new(TestReply)
- err := cli.Call(context.TODO(), "RPC.Add", args, reply)
- So(err, ShouldBeNil)
- })
- Convey("test discovery with cluster", t, func() {
- env.Zone = "test2"
- cli := NewDiscoveryCli("test2", &ClientConfig{
- Cluster: "red",
- Timeout: xtime.Duration(time.Second),
- })
- time.Sleep(time.Second * 2)
- args := &TestArgs{7, 8}
- reply := new(TestReply)
- err := cli.Call(context.TODO(), "RPC.Add", args, reply)
- So(err, ShouldBeNil)
- })
- Convey("test conf Zone cli", t, func() {
- env.Zone = "testsh"
- once1.Do(startTestServer1)
- in2.Addrs = []string{scheme + "://" + svrAddr1}
- dis := discovery.New(c)
- _, err := dis.Register(context.TODO(), in2)
- So(err, ShouldBeNil)
- env.Zone = "sh001"
- cli := NewDiscoveryCli("test3", &ClientConfig{
- Cluster: "",
- Timeout: xtime.Duration(time.Second),
- Zone: "testsh",
- })
- time.Sleep(time.Second * 2)
- args := &TestArgs{7, 8}
- reply := new(TestReply)
- err = cli.Call(context.TODO(), "RPC.Add", args, reply)
- So(err, ShouldBeNil)
- })
- }
|