Fix TestCtx

This commit is contained in:
Łukasz Magiera 2019-07-02 15:20:33 +02:00
parent 4fcdd4a400
commit bed044b5a1
2 changed files with 7 additions and 7 deletions

View File

@ -77,7 +77,7 @@ func (s *RPCServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
handler, ok := s.methods[req.Method]
if !ok {
fmt.Println("rpcserver: unknown method")
fmt.Printf("rpcserver: unknown method %s\n", req.Method)
w.WriteHeader(500)
return
}

View File

@ -5,6 +5,7 @@ import (
"errors"
"net/http/httptest"
"strconv"
"sync/atomic"
"testing"
"time"
)
@ -167,12 +168,12 @@ func TestRPC(t *testing.T) {
type CtxHandler struct {
cancelled bool
i int
i int32
}
func (h *CtxHandler) Test(ctx context.Context) {
timeout := time.After(300 * time.Millisecond)
h.i++
atomic.AddInt32(&h.i, 1)
select {
case <-timeout:
@ -182,13 +183,12 @@ func (h *CtxHandler) Test(ctx context.Context) {
}
func TestCtx(t *testing.T) {
t.SkipNow()
// setup server
serverHandler := &CtxHandler{}
rpcServer := NewServer()
rpcServer.Register("SimpleServerHandler", serverHandler)
rpcServer.Register("CtxHandler", serverHandler)
// httptest stuff
testServ := httptest.NewServer(rpcServer)
@ -201,7 +201,7 @@ func TestCtx(t *testing.T) {
}
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()
client.Test(ctx)
@ -217,7 +217,7 @@ func TestCtx(t *testing.T) {
NewClient(testServ.URL, "CtxHandler", &noCtxClient)
noCtxClient.Test()
if serverHandler.cancelled || serverHandler.i != 2 {
if serverHandler.cancelled || atomic.LoadInt32(&serverHandler.i) != 2 {
t.Error("wrong serverHandler state")
}
}