(t *testing.T)
| 104 | } |
| 105 | |
| 106 | func TestNewSessionPool(t *testing.T) { |
| 107 | Convey("session pool", t, func(c C) { |
| 108 | log.SetLevel(log.FatalLevel) |
| 109 | |
| 110 | // setup pool |
| 111 | p := &SessionPool{ |
| 112 | sessions: make(map[proto.NodeID]*Session), |
| 113 | } |
| 114 | defer withTCPDialer()() |
| 115 | |
| 116 | // setup server |
| 117 | l, err := net.Listen("tcp", ":0") |
| 118 | if err != nil { |
| 119 | log.Fatal(err) |
| 120 | } |
| 121 | server, err := NewServerWithService(ServiceMap{"Test": NewTestService()}) |
| 122 | server.SetListener(l) |
| 123 | go server.WithAcceptConnFunc(rpc.AcceptRawConn).Serve() |
| 124 | |
| 125 | wg := &sync.WaitGroup{} |
| 126 | wg.Add(concurrency) |
| 127 | for i := 0; i < concurrency; i++ { |
| 128 | go func(c2 C, n int) { |
| 129 | defer wg.Done() |
| 130 | client, err := p.Get(proto.NodeID(l.Addr().String())) |
| 131 | defer func() { _ = client.Close() }() |
| 132 | if err != nil { |
| 133 | log.Errorf("get session failed: %s", err) |
| 134 | return |
| 135 | } |
| 136 | c2.So(err, ShouldBeNil) |
| 137 | err = client.Call("Test.IncCounter", &TestReq{Step: 1}, &TestRep{}) |
| 138 | c2.So(err, ShouldBeNil) |
| 139 | }(c, packetCount) |
| 140 | } |
| 141 | |
| 142 | wg.Wait() |
| 143 | So(p.Len(), ShouldEqual, conf.MaxRPCMuxPoolPhysicalConnection) |
| 144 | |
| 145 | // setup server |
| 146 | l2, err := net.Listen("tcp", ":0") |
| 147 | if err != nil { |
| 148 | log.Fatal(err) |
| 149 | } |
| 150 | server2, err := NewServerWithService(ServiceMap{"Test": NewTestService()}) |
| 151 | server2.SetListener(l2) |
| 152 | go server2.WithAcceptConnFunc(rpc.AcceptRawConn).Serve() |
| 153 | |
| 154 | _, err = p.Get(proto.NodeID(l2.Addr().String())) |
| 155 | So(err, ShouldBeNil) |
| 156 | So(p.Len(), ShouldEqual, conf.MaxRPCMuxPoolPhysicalConnection+1) |
| 157 | |
| 158 | wg2 := &sync.WaitGroup{} |
| 159 | wg2.Add(concurrency) |
| 160 | for i := 0; i < concurrency; i++ { |
| 161 | go func(c2 C, n int) { |
| 162 | // Open a new stream |
| 163 | // Stream implements net.Conn |
nothing calls this directly
no test coverage detected