Skip to content

Commit

Permalink
Fix another flaky test and fix wrong codes in local calls.
Browse files Browse the repository at this point in the history
  • Loading branch information
cscatolini committed Apr 17, 2018
1 parent 907e6e5 commit 8e6501c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 26 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ e2e-test: ensure-e2e-deps ensure-e2e-bin
@echo "===============RUNNING E2E TESTS==============="
@go test ./e2e/e2e_test.go

e2e-test-update: ensure-e2e-deps ensure-e2e-bin
@echo "===============RUNNING E2E TESTS==============="
@go test ./e2e/e2e_test.go -update

unit-test-coverage: kill-e2e-deps
@echo "===============RUNNING UNIT TESTS==============="
@go test $(TESTABLE_PACKAGES) -coverprofile coverprofile.out
Expand Down
22 changes: 7 additions & 15 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -764,32 +764,24 @@ func TestAgentHandle(t *testing.T) {
assert.NotNil(t, ag)

go ag.Handle()

expected := []byte("hello")
ag.chWrite <- expected

// Sends two heartbeats and then times out
mockConn.EXPECT().Write(hbd).Return(0, nil).Times(2)
die := false
go func() {
for {
select {
case <-ag.chDie:
die = true
}
}
}()

received := false
received := make(chan bool)
mockConn.EXPECT().Write(expected).Return(0, nil).Do(func(d []byte) {
received = true
received <- true
})

// ag.Close on method exit
mockConn.EXPECT().RemoteAddr()
mockConn.EXPECT().Close()
mockConn.EXPECT().Close().MaxTimes(1)

// heartbeat
helpers.ShouldEventuallyReturn(t, func() bool { return die }, true, 500*time.Millisecond, 5*time.Second)
// chWrite
helpers.ShouldEventuallyReturn(t, func() bool { return received }, true)
<-received
// heartbeat
<-ag.chDie
}
8 changes: 4 additions & 4 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ func TestHandlerCallToFront(t *testing.T) {
{"connector.testsvc.testrequestreturnsptr", []byte(`{"msg":"good"}`), []byte(`{"code":200,"msg":"good"}`)},
{"connector.testsvc.testrequestreturnsraw", []byte(`{"msg":"good"}`), []byte(`good`)},
{"connector.testsvc.testrequestreceivereturnsraw", []byte(`woow`), []byte(`woow`)},
{"connector.testsvc.nonexistenthandler", []byte(`woow`), []byte(`{"Code":"PIT-000","Msg":"pitaya/handler: connector.testsvc.nonexistenthandler not found"}`)},
{"connector.testsvc.testrequestreturnserror", []byte(`woow`), []byte(`{"Code":"PIT-500","Msg":"somerror"}`)},
{"connector.testsvc.nonexistenthandler", []byte(`woow`), []byte(`{"Code":"PIT-404","Msg":"pitaya/handler: connector.testsvc.nonexistenthandler not found"}`)},
{"connector.testsvc.testrequestreturnserror", []byte(`woow`), []byte(`{"Code":"PIT-555","Msg":"somerror"}`)},
}
port := helpers.GetFreePort(t)
sdPrefix := fmt.Sprintf("%s/", uuid.New().String())
Expand Down Expand Up @@ -199,8 +199,8 @@ func TestForwardToBackend(t *testing.T) {
{"game.testsvc.testrequestreturnsptr", []byte(`{"msg":"good"}`), []byte(`{"code":200,"msg":"good"}`)},
{"game.testsvc.testrequestreturnsraw", []byte(`{"msg":"good"}`), []byte(`good`)},
{"game.testsvc.testrequestreceivereturnsraw", []byte(`woow`), []byte(`woow`)},
{"game.testsvc.nonexistenthandler", []byte(`woow`), []byte(`{"Code":"PIT-000","Msg":"pitaya/handler: game.testsvc.nonexistenthandler not found"}`)},
{"game.testsvc.testrequestreturnserror", []byte(`woow`), []byte(`{"Code":"PIT-500","Msg":"somerror"}`)},
{"game.testsvc.nonexistenthandler", []byte(`woow`), []byte(`{"Code":"PIT-404","Msg":"pitaya/handler: game.testsvc.nonexistenthandler not found"}`)},
{"game.testsvc.testrequestreturnserror", []byte(`woow`), []byte(`{"Code":"PIT-555","Msg":"somerror"}`)},
}

c := client.New(false)
Expand Down
6 changes: 3 additions & 3 deletions e2e/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,19 +120,19 @@ func (t *TestSvc) TestRequestReceiveReturnsRaw(s *session.Session, in []byte) ([

// TestRequestReturnsError handler for e2e tests
func (t *TestSvc) TestRequestReturnsError(s *session.Session, in []byte) ([]byte, error) {
return nil, pitaya.Error(errors.New("somerror"), "PIT-500")
return nil, pitaya.Error(errors.New("somerror"), "PIT-555")
}

// TestBind handler for e2e tests
func (t *TestSvc) TestBind(s *session.Session) ([]byte, error) {
uid := uuid.New().String()
err := s.Bind(uid)
if err != nil {
return nil, pitaya.Error(err, "PIT-400")
return nil, pitaya.Error(err, "PIT-444")
}
err = t.group.Add(s)
if err != nil {
return nil, pitaya.Error(err, "PIT-400")
return nil, pitaya.Error(err, "PIT-441")
}
return []byte("ack"), nil
}
Expand Down
3 changes: 2 additions & 1 deletion service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/topfreegames/pitaya/cluster"
"github.com/topfreegames/pitaya/component"
"github.com/topfreegames/pitaya/constants"
e "github.com/topfreegames/pitaya/errors"
"github.com/topfreegames/pitaya/internal/codec"
"github.com/topfreegames/pitaya/internal/message"
"github.com/topfreegames/pitaya/internal/packet"
Expand Down Expand Up @@ -227,7 +228,7 @@ func (h *HandlerService) processMessage(a *agent.Agent, msg *message.Message) {
r, err := route.Decode(msg.Route)
if err != nil {
log.Error(err.Error())
a.AnswerWithError(msg.ID, err)
a.AnswerWithError(msg.ID, e.NewError(err, e.ErrBadRequestCode))
return
}

Expand Down
2 changes: 1 addition & 1 deletion service/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ func (r *RemoteService) remoteCall(
if target == nil {
target, err = r.router.Route(rpcType, svType, session, route)
if err != nil {
return nil, err
return nil, e.NewError(err, e.ErrInternalCode)
}
}

Expand Down
2 changes: 1 addition & 1 deletion service/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func processHandlerMessage(
) ([]byte, error) {
h, err := getHandler(rt)
if err != nil {
return nil, err
return nil, e.NewError(err, e.ErrNotFoundCode)
}

msgType, err := getMsgType(msgTypeIface)
Expand Down
2 changes: 1 addition & 1 deletion service/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ func TestProcessHandlerMessage(t *testing.T) {
out []byte
err error
}{
{"invalid_route", route.NewRoute("", "no", "no"), nil, nil, nil, message.Request, nil, false, nil, errors.New("pitaya/handler: no.no not found")},
{"invalid_route", route.NewRoute("", "no", "no"), nil, nil, nil, message.Request, nil, false, nil, e.NewError(errors.New("pitaya/handler: no.no not found"), e.ErrNotFoundCode)},
{"invalid_msg_type", rt, nil, nil, nil, message.Request, nil, false, nil, e.NewError(errInvalidMsg, e.ErrInternalCode)},
{"request_on_notify", rt, nil, nil, nil, message.Notify, message.Request, false, nil, e.NewError(constants.ErrRequestOnNotify, e.ErrBadRequestCode)},
{"failed_handle_args_unmarshal", rt, nil, errors.New("some error"), &SomeStruct{}, message.Request, message.Request, false, nil, e.NewError(errors.New("some error"), e.ErrBadRequestCode)},
Expand Down

0 comments on commit 8e6501c

Please sign in to comment.