Fix TestCtx
This commit is contained in:
parent
4fcdd4a400
commit
bed044b5a1
@ -77,7 +77,7 @@ func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
handler, ok := s.methods[req.Method]
|
handler, ok := s.methods[req.Method]
|
||||||
if !ok {
|
if !ok {
|
||||||
fmt.Println("rpcserver: unknown method")
|
fmt.Printf("rpcserver: unknown method %s\n", req.Method)
|
||||||
w.WriteHeader(500)
|
w.WriteHeader(500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -167,12 +168,12 @@ func TestRPC(t *testing.T) {
|
|||||||
|
|
||||||
type CtxHandler struct {
|
type CtxHandler struct {
|
||||||
cancelled bool
|
cancelled bool
|
||||||
i int
|
i int32
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *CtxHandler) Test(ctx context.Context) {
|
func (h *CtxHandler) Test(ctx context.Context) {
|
||||||
timeout := time.After(300 * time.Millisecond)
|
timeout := time.After(300 * time.Millisecond)
|
||||||
h.i++
|
atomic.AddInt32(&h.i, 1)
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-timeout:
|
case <-timeout:
|
||||||
@ -182,13 +183,12 @@ func (h *CtxHandler) Test(ctx context.Context) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCtx(t *testing.T) {
|
func TestCtx(t *testing.T) {
|
||||||
t.SkipNow()
|
|
||||||
// setup server
|
// setup server
|
||||||
|
|
||||||
serverHandler := &CtxHandler{}
|
serverHandler := &CtxHandler{}
|
||||||
|
|
||||||
rpcServer := NewServer()
|
rpcServer := NewServer()
|
||||||
rpcServer.Register("SimpleServerHandler", serverHandler)
|
rpcServer.Register("CtxHandler", serverHandler)
|
||||||
|
|
||||||
// httptest stuff
|
// httptest stuff
|
||||||
testServ := httptest.NewServer(rpcServer)
|
testServ := httptest.NewServer(rpcServer)
|
||||||
@ -201,7 +201,7 @@ func TestCtx(t *testing.T) {
|
|||||||
}
|
}
|
||||||
NewClient(testServ.URL, "CtxHandler", &client)
|
NewClient(testServ.URL, "CtxHandler", &client)
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Millisecond)
|
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
|
|
||||||
client.Test(ctx)
|
client.Test(ctx)
|
||||||
@ -217,7 +217,7 @@ func TestCtx(t *testing.T) {
|
|||||||
NewClient(testServ.URL, "CtxHandler", &noCtxClient)
|
NewClient(testServ.URL, "CtxHandler", &noCtxClient)
|
||||||
|
|
||||||
noCtxClient.Test()
|
noCtxClient.Test()
|
||||||
if serverHandler.cancelled || serverHandler.i != 2 {
|
if serverHandler.cancelled || atomic.LoadInt32(&serverHandler.i) != 2 {
|
||||||
t.Error("wrong serverHandler state")
|
t.Error("wrong serverHandler state")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user