Skip to content

Commit

Permalink
adding rpc_storage_handler tests + goimports
Browse files Browse the repository at this point in the history
  • Loading branch information
tbuchaillot committed Dec 30, 2022
1 parent 52f3b3f commit ea0dcca
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 13 deletions.
27 changes: 15 additions & 12 deletions gateway/rpc_storage_handler.go
Expand Up @@ -128,29 +128,32 @@ func (r *RPCStorageHandler) Connect() bool {
RPCPoolSize: slaveOptions.RPCPoolSize,
}

groupLoginCallbackFn := func(userKey string, groupID string) interface{} {
return apidef.GroupLoginRequest{
UserKey: userKey,
GroupID: groupID,
}
}
if r.Gw.GetConfig().SlaveOptions.SynchroniserEnabled {
forcer := rpc.NewSyncForcer(r.Gw.RedisController)
groupLoginCallbackFn = forcer.GrouLoginCallback
}

return rpc.Connect(
rpcConfig,
r.SuppressRegister,
dispatcherFuncs,
groupLoginCallbackFn,
r.getGroupLoginCallback(r.Gw.GetConfig().SlaveOptions.SynchroniserEnabled),
func() {
r.Gw.reloadURLStructure(nil)
},
r.DoReload,
)
}

func (r *RPCStorageHandler) getGroupLoginCallback(synchroniserEnabled bool) func(userKey string, groupID string) interface{} {
groupLoginCallbackFn := func(userKey string, groupID string) interface{} {
return apidef.GroupLoginRequest{
UserKey: userKey,
GroupID: groupID,
}
}
if synchroniserEnabled {
forcer := rpc.NewSyncForcer(r.Gw.RedisController)
groupLoginCallbackFn = forcer.GrouLoginCallback
}
return groupLoginCallbackFn
}

func (r *RPCStorageHandler) hashKey(in string) string {
if !r.HashKeys {
// Not hashing? Return the raw key
Expand Down
48 changes: 48 additions & 0 deletions gateway/rpc_storage_handler_test.go
Expand Up @@ -5,6 +5,7 @@ import (
"net/http"
"testing"

"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/config"

"github.com/lonelycode/osin"
Expand Down Expand Up @@ -281,3 +282,50 @@ func TestRPCUpdateKey(t *testing.T) {
})
}
}

func TestGetGroupLoginCallback(t *testing.T) {
tcs := []struct {
testName string
syncEnabled bool
givenKey string
givenGroup string
expectedCallbackResponse interface{}
}{
{
testName: "sync disabled",
syncEnabled: false,
givenKey: "key",
givenGroup: "group",
expectedCallbackResponse: apidef.GroupLoginRequest{UserKey: "key", GroupID: "group"},
},
{
testName: "sync enabled",
syncEnabled: true,
givenKey: "key",
givenGroup: "group",
expectedCallbackResponse: apidef.GroupLoginRequest{UserKey: "key", GroupID: "group", ForceSync: true},
},
}

for _, tc := range tcs {
t.Run(tc.testName, func(t *testing.T) {
g := StartTest(func(globalConf *config.Config) {
globalConf.SlaveOptions.SynchroniserEnabled = tc.syncEnabled
})
defer g.Close()
defer g.Gw.GlobalSessionManager.Store().DeleteAllKeys()

rpcListener := RPCStorageHandler{
KeyPrefix: "rpc.listener.",
SuppressRegister: true,
Gw: g.Gw,
}

fn := rpcListener.getGroupLoginCallback(tc.syncEnabled)
groupLogin := fn(tc.givenKey, tc.givenGroup).(apidef.GroupLoginRequest)

assert.Equal(t, tc.expectedCallbackResponse, groupLogin)
})
}

}
1 change: 0 additions & 1 deletion rpc/synchronization_forcer.go
Expand Up @@ -37,5 +37,4 @@ func (sf *SyncronizerForcer) GrouLoginCallback(userKey string, groupID string) i
GroupID: groupID,
ForceSync: shouldForce,
}

}

0 comments on commit ea0dcca

Please sign in to comment.