Skip to content

Commit

Permalink
Possibly fix issue #51
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielerzinger committed Sep 3, 2018
1 parent 25127e9 commit 5924d1a
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 6 deletions.
1 change: 1 addition & 0 deletions constants/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var (
ErrEtcdGrantLeaseTimeout = errors.New("timed out waiting for etcd lease grant")
ErrFrontSessionCantPushToFront = errors.New("frontend session can't push to front")
ErrIllegalUID = errors.New("illegal uid")
ErrKickingUsers = errors.New("failed to kick users, check array with failed kicks")
ErrMemberNotFound = errors.New("member not found in the group")
ErrNatsMessagesBufferSizeZero = errors.New("pitaya.buffer.cluster.rpc.server.nats.messages cant be zero")
ErrNatsNoRequestTimeout = errors.New("pitaya.cluster.rpc.client.nats.requesttimeout cant be empty")
Expand Down
17 changes: 14 additions & 3 deletions kick.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,34 @@ import (
)

// SendKickToUsers sends kick to an user
func SendKickToUsers(uids []string, frontendType string) error {
func SendKickToUsers(uids []string, frontendType string) ([]string, error) {
if !app.server.Frontend && frontendType == "" {
return constants.ErrFrontendTypeNotSpecified
return nil, constants.ErrFrontendTypeNotSpecified
}

var notKickedUids []string

for _, uid := range uids {
if s := session.GetSessionByUID(uid); s != nil {
if err := s.Kick(context.Background()); err != nil {
notKickedUids = append(notKickedUids, uid)
logger.Log.Errorf("Session kick error, ID=%d, UID=%d, ERROR=%s", s.ID(), s.UID(), err.Error())
}
} else if app.rpcClient != nil {
kick := &protos.KickMsg{UserId: uid}
if err := app.rpcClient.SendKick(uid, frontendType, kick); err != nil {
notKickedUids = append(notKickedUids, uid)
logger.Log.Errorf("RPCClient send kick error, UID=%d, SvType=%s, Error=%s", uid, frontendType, err.Error())
}
} else {
notKickedUids = append(notKickedUids, uid)
}

}
return nil

if len(notKickedUids) != 0 {
return notKickedUids, constants.ErrKickingUsers
}

return nil, nil
}
39 changes: 36 additions & 3 deletions kick_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
clustermocks "github.com/topfreegames/pitaya/cluster/mocks"
"github.com/topfreegames/pitaya/constants"
"github.com/topfreegames/pitaya/protos"
"github.com/topfreegames/pitaya/session"
"github.com/topfreegames/pitaya/session/mocks"
Expand All @@ -42,7 +43,6 @@ func TestSendKickToUsersLocalSession(t *testing.T) {
err error
}{
{"success", uuid.New().String(), uuid.New().String(), "connector", nil},
{"fail", uuid.New().String(), uuid.New().String(), "connector", nil},
}

for _, table := range tables {
Expand All @@ -63,13 +63,45 @@ func TestSendKickToUsersLocalSession(t *testing.T) {
if table.err == nil {
mockNetworkEntity.EXPECT().Close().Times(2)
}
err = SendKickToUsers([]string{table.uid1, table.uid2}, table.frontendType)

failedUids, err := SendKickToUsers([]string{table.uid1, table.uid2}, table.frontendType)
assert.Len(t, failedUids, 0)
assert.NoError(t, err)
})
}
}

func TestSendKickToUsersFail(t *testing.T) {
tables := []struct {
name string
uid1 string
uid2 string
frontendType string
err error
}{
{"fail for one user", uuid.New().String(), uuid.New().String(), "connector", constants.ErrKickingUsers},
}

for _, table := range tables {
t.Run(table.name, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockNetworkEntity := mocks.NewMockNetworkEntity(ctrl)

s1 := session.New(mockNetworkEntity, true)
err := s1.Bind(context.Background(), table.uid1)
assert.NoError(t, err)

mockNetworkEntity.EXPECT().Kick(context.Background()).Times(1)
mockNetworkEntity.EXPECT().Close()
failedUids, err := SendKickToUsers([]string{table.uid1, table.uid2}, table.frontendType)
assert.Len(t, failedUids, 1)
assert.Equal(t, err, table.err)

})
}
}

func TestSendKickToUsersRemoteSession(t *testing.T) {
tables := []struct {
name string
Expand All @@ -91,8 +123,9 @@ func TestSendKickToUsersRemoteSession(t *testing.T) {
mockRPCClient.EXPECT().SendKick(uid, gomock.Any(), expectedKick)
}

err := SendKickToUsers(table.uids, table.frontendType)
failedUids, err := SendKickToUsers(table.uids, table.frontendType)
assert.NoError(t, err)
assert.Nil(t, failedUids)
})
}
}

0 comments on commit 5924d1a

Please sign in to comment.