[#38] Add session cache tests

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2021-10-25 16:24:43 +03:00 committed by Alex Vanin
parent c651a714fd
commit e08e3d6c00
2 changed files with 138 additions and 31 deletions

View file

@ -93,21 +93,21 @@ type Pool interface {
WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error WaitForContainerPresence(context.Context, *cid.ID, *ContainerPollingParams) error
Close() Close()
PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam CallParam) (*object.ID, error) PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam *CallParam) (*object.ID, error)
DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam CallParam) error DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam *CallParam) error
GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam CallParam) (*object.Object, error) GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam *CallParam) (*object.Object, error)
GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam CallParam) (*object.Object, error) GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam *CallParam) (*object.Object, error)
ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam CallParam) ([]byte, error) ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam *CallParam) ([]byte, error)
ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam CallParam) ([][32]byte, error) ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][32]byte, error)
ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam CallParam) ([][64]byte, error) ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][64]byte, error)
SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam CallParam) ([]*object.ID, error) SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam *CallParam) ([]*object.ID, error)
PutContainerParam(ctx context.Context, cnr *container.Container, callParam CallParam) (*cid.ID, error) PutContainerParam(ctx context.Context, cnr *container.Container, callParam *CallParam) (*cid.ID, error)
GetContainerParam(ctx context.Context, cid *cid.ID, callParam CallParam) (*container.Container, error) GetContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*container.Container, error)
ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam CallParam) ([]*cid.ID, error) ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam *CallParam) ([]*cid.ID, error)
DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam CallParam) error DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) error
GetEACLParam(ctx context.Context, cid *cid.ID, callParam CallParam) (*client.EACLWithSignature, error) GetEACLParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*client.EACLWithSignature, error)
SetEACLParam(ctx context.Context, table *eacl.Table, callParam CallParam) error SetEACLParam(ctx context.Context, table *eacl.Table, callParam *CallParam) error
AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam CallParam) error AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam *CallParam) error
} }
type clientPack struct { type clientPack struct {
@ -319,7 +319,7 @@ func formCacheKey(address string, key *ecdsa.PrivateKey) string {
return address + k.String() return address + k.String()
} }
func (p *pool) connParam(ctx context.Context, param CallParam) (*clientPack, []client.CallOption, error) { func (p *pool) connParam(ctx context.Context, param *CallParam) (*clientPack, []client.CallOption, error) {
cp, err := p.connection() cp, err := p.connection()
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
@ -474,7 +474,7 @@ func (p *pool) checkSessionTokenErr(err error, address string) {
} }
} }
func (p *pool) PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam CallParam) (*object.ID, error) { func (p *pool) PutObjectParam(ctx context.Context, params *client.PutObjectParams, callParam *CallParam) (*object.ID, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -484,7 +484,7 @@ func (p *pool) PutObjectParam(ctx context.Context, params *client.PutObjectParam
return res, err return res, err
} }
func (p *pool) DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam CallParam) error { func (p *pool) DeleteObjectParam(ctx context.Context, params *client.DeleteObjectParams, callParam *CallParam) error {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return err return err
@ -494,7 +494,7 @@ func (p *pool) DeleteObjectParam(ctx context.Context, params *client.DeleteObjec
return err return err
} }
func (p *pool) GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam CallParam) (*object.Object, error) { func (p *pool) GetObjectParam(ctx context.Context, params *client.GetObjectParams, callParam *CallParam) (*object.Object, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -504,7 +504,7 @@ func (p *pool) GetObjectParam(ctx context.Context, params *client.GetObjectParam
return res, err return res, err
} }
func (p *pool) GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam CallParam) (*object.Object, error) { func (p *pool) GetObjectHeaderParam(ctx context.Context, params *client.ObjectHeaderParams, callParam *CallParam) (*object.Object, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -514,7 +514,7 @@ func (p *pool) GetObjectHeaderParam(ctx context.Context, params *client.ObjectHe
return res, err return res, err
} }
func (p *pool) ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam CallParam) ([]byte, error) { func (p *pool) ObjectPayloadRangeDataParam(ctx context.Context, params *client.RangeDataParams, callParam *CallParam) ([]byte, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -524,7 +524,7 @@ func (p *pool) ObjectPayloadRangeDataParam(ctx context.Context, params *client.R
return res, err return res, err
} }
func (p *pool) ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam CallParam) ([][32]byte, error) { func (p *pool) ObjectPayloadRangeSHA256Param(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][32]byte, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -534,7 +534,7 @@ func (p *pool) ObjectPayloadRangeSHA256Param(ctx context.Context, params *client
return res, err return res, err
} }
func (p *pool) ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam CallParam) ([][64]byte, error) { func (p *pool) ObjectPayloadRangeTZParam(ctx context.Context, params *client.RangeChecksumParams, callParam *CallParam) ([][64]byte, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -544,7 +544,7 @@ func (p *pool) ObjectPayloadRangeTZParam(ctx context.Context, params *client.Ran
return res, err return res, err
} }
func (p *pool) SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam CallParam) ([]*object.ID, error) { func (p *pool) SearchObjectParam(ctx context.Context, params *client.SearchObjectParams, callParam *CallParam) ([]*object.ID, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -554,7 +554,7 @@ func (p *pool) SearchObjectParam(ctx context.Context, params *client.SearchObjec
return res, err return res, err
} }
func (p *pool) PutContainerParam(ctx context.Context, cnr *container.Container, callParam CallParam) (*cid.ID, error) { func (p *pool) PutContainerParam(ctx context.Context, cnr *container.Container, callParam *CallParam) (*cid.ID, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -564,7 +564,7 @@ func (p *pool) PutContainerParam(ctx context.Context, cnr *container.Container,
return res, err return res, err
} }
func (p *pool) GetContainerParam(ctx context.Context, cid *cid.ID, callParam CallParam) (*container.Container, error) { func (p *pool) GetContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*container.Container, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -574,7 +574,7 @@ func (p *pool) GetContainerParam(ctx context.Context, cid *cid.ID, callParam Cal
return res, err return res, err
} }
func (p *pool) ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam CallParam) ([]*cid.ID, error) { func (p *pool) ListContainersParam(ctx context.Context, ownerID *owner.ID, callParam *CallParam) ([]*cid.ID, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -584,7 +584,7 @@ func (p *pool) ListContainersParam(ctx context.Context, ownerID *owner.ID, callP
return res, err return res, err
} }
func (p *pool) DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam CallParam) error { func (p *pool) DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam *CallParam) error {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return err return err
@ -594,7 +594,7 @@ func (p *pool) DeleteContainerParam(ctx context.Context, cid *cid.ID, callParam
return err return err
} }
func (p *pool) GetEACLParam(ctx context.Context, cid *cid.ID, callParam CallParam) (*client.EACLWithSignature, error) { func (p *pool) GetEACLParam(ctx context.Context, cid *cid.ID, callParam *CallParam) (*client.EACLWithSignature, error) {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return nil, err return nil, err
@ -604,7 +604,7 @@ func (p *pool) GetEACLParam(ctx context.Context, cid *cid.ID, callParam CallPara
return res, err return res, err
} }
func (p *pool) SetEACLParam(ctx context.Context, table *eacl.Table, callParam CallParam) error { func (p *pool) SetEACLParam(ctx context.Context, table *eacl.Table, callParam *CallParam) error {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return err return err
@ -614,7 +614,7 @@ func (p *pool) SetEACLParam(ctx context.Context, table *eacl.Table, callParam Ca
return err return err
} }
func (p *pool) AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam CallParam) error { func (p *pool) AnnounceContainerUsedSpaceParam(ctx context.Context, announce []container.UsedSpaceAnnouncement, callParam *CallParam) error {
cp, options, err := p.connParam(ctx, callParam) cp, options, err := p.connParam(ctx, callParam)
if err != nil { if err != nil {
return err return err

View file

@ -308,7 +308,114 @@ func TestTwoFailed(t *testing.T) {
} }
func TestSessionCache(t *testing.T) { func TestSessionCache(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var tokens []*session.Token
clientBuilder := func(opts ...client.Option) (client.Client, error) {
mockClient := NewMockClient(ctrl)
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*session.Token, error) {
tok := session.NewToken()
uid, err := uuid.New().MarshalBinary()
require.NoError(t, err)
tok.SetID(uid)
tokens = append(tokens, tok)
return tok, err
}).MaxTimes(2)
mockClient.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("session token does not exist"))
mockClient.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
return mockClient, nil
}
key, err := keys.NewPrivateKey()
require.NoError(t, err)
pb := new(Builder)
pb.AddNode("peer0", 1)
opts := &BuilderOptions{
Key: &key.PrivateKey,
clientBuilder: clientBuilder,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pool, err := pb.Build(ctx, opts)
require.NoError(t, err)
// cache must contain session token
_, st, err := pool.Connection()
require.NoError(t, err)
require.Contains(t, tokens, st)
_, err = pool.GetObjectParam(ctx, nil, &CallParam{})
require.Error(t, err)
// cache must not contain session token
_, st, err = pool.Connection()
require.NoError(t, err)
require.Nil(t, st)
_, err = pool.PutObjectParam(ctx, nil, &CallParam{})
require.NoError(t, err)
// cache must contain session token
_, st, err = pool.Connection()
require.NoError(t, err)
require.Contains(t, tokens, st)
}
func TestSessionCacheWithKey(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var tokens []*session.Token
clientBuilder := func(opts ...client.Option) (client.Client, error) {
mockClient := NewMockClient(ctrl)
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*session.Token, error) {
tok := session.NewToken()
uid, err := uuid.New().MarshalBinary()
require.NoError(t, err)
tok.SetID(uid)
tokens = append(tokens, tok)
return tok, err
}).MaxTimes(2)
mockClient.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
return mockClient, nil
}
key, err := keys.NewPrivateKey()
require.NoError(t, err)
key2, err := keys.NewPrivateKey()
require.NoError(t, err)
pb := new(Builder)
pb.AddNode("peer0", 1)
opts := &BuilderOptions{
Key: &key.PrivateKey,
clientBuilder: clientBuilder,
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pool, err := pb.Build(ctx, opts)
require.NoError(t, err)
// cache must contain session token
_, st, err := pool.Connection()
require.NoError(t, err)
require.Contains(t, tokens, st)
_, err = pool.GetObjectParam(ctx, nil, &CallParam{Key: &key2.PrivateKey})
require.NoError(t, err)
require.Len(t, tokens, 2)
} }
func newToken(t *testing.T) *session.Token { func newToken(t *testing.T) *session.Token {