[#85] pool: update tests

Signed-off-by: Denis Kirillov <denis@nspcc.ru>
This commit is contained in:
Denis Kirillov 2022-04-08 09:33:59 +03:00 committed by LeL
parent 97c8274dc0
commit 1e8aa6a99f

View file

@ -7,7 +7,6 @@ import (
"context" "context"
"crypto/ecdsa" "crypto/ecdsa"
"fmt" "fmt"
"math/rand"
"testing" "testing"
"time" "time"
@ -78,32 +77,30 @@ func TestBuildPoolOneNodeFailed(t *testing.T) {
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
ctrl2 := gomock.NewController(t) ctrl2 := gomock.NewController(t)
ni := &netmap.NodeInfo{}
ni.SetAddresses("addr1", "addr2")
var expectedToken *session.Token var expectedToken *session.Token
clientCount := -1 clientCount := -1
clientBuilder := func(_ string) (client, error) { clientBuilder := func(_ string) (client, error) {
clientCount++ clientCount++
mockClient := NewMockClient(ctrl) mockClient := NewMockClient(ctrl)
mockInvokes := 0 mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*resCreateSession, error) { tok := newToken(t)
mockInvokes++ return &resCreateSession{
if mockInvokes == 1 { sessionKey: tok.SessionKey(),
expectedToken = newToken(t) id: tok.ID(),
return nil, fmt.Errorf("error session") }, nil
} }).AnyTimes()
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error")).AnyTimes()
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
mockClient2 := NewMockClient(ctrl2)
mockClient2.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
expectedToken = newToken(t)
return &resCreateSession{ return &resCreateSession{
sessionKey: expectedToken.SessionKey(), sessionKey: expectedToken.SessionKey(),
id: expectedToken.ID(), id: expectedToken.ID(),
}, nil }, nil
}).AnyTimes() }).AnyTimes()
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
mockClient2 := NewMockClient(ctrl2)
mockClient2.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(&resCreateSession{}, nil).AnyTimes()
mockClient2.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes() mockClient2.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
mockClient2.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes() mockClient2.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
@ -570,8 +567,6 @@ func newToken(t *testing.T) *session.Token {
} }
func TestSessionTokenOwner(t *testing.T) { func TestSessionTokenOwner(t *testing.T) {
t.Skip("NeoFS API client can't be mocked")
ctrl := gomock.NewController(t) ctrl := gomock.NewController(t)
clientBuilder := func(_ string) (client, error) { clientBuilder := func(_ string) (client, error) {
mockClient := NewMockClient(ctrl) mockClient := NewMockClient(ctrl)
@ -628,23 +623,6 @@ func TestWaitPresence(t *testing.T) {
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes() mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
mockClient.EXPECT().containerGet(gomock.Any(), gomock.Any()).Return(&container.Container{}, nil).AnyTimes() mockClient.EXPECT().containerGet(gomock.Any(), gomock.Any()).Return(&container.Container{}, nil).AnyTimes()
cache, err := newCache()
require.NoError(t, err)
inner := &innerPool{
sampler: newSampler([]float64{1}, rand.NewSource(0)),
clientPacks: []*clientPack{{
client: mockClient,
healthy: true,
}},
}
p := &Pool{
innerPools: []*innerPool{inner},
key: newPrivateKey(t),
cache: cache,
}
t.Run("context canceled", func(t *testing.T) { t.Run("context canceled", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
go func() { go func() {
@ -652,7 +630,7 @@ func TestWaitPresence(t *testing.T) {
cancel() cancel()
}() }()
err = waitForContainerPresence(ctx, p, nil, &WaitParams{ err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
timeout: 120 * time.Second, timeout: 120 * time.Second,
pollInterval: 5 * time.Second, pollInterval: 5 * time.Second,
}) })
@ -662,7 +640,7 @@ func TestWaitPresence(t *testing.T) {
t.Run("context deadline exceeded", func(t *testing.T) { t.Run("context deadline exceeded", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
err := waitForContainerPresence(ctx, p, nil, &WaitParams{ err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
timeout: 500 * time.Millisecond, timeout: 500 * time.Millisecond,
pollInterval: 5 * time.Second, pollInterval: 5 * time.Second,
}) })
@ -672,7 +650,7 @@ func TestWaitPresence(t *testing.T) {
t.Run("ok", func(t *testing.T) { t.Run("ok", func(t *testing.T) {
ctx := context.Background() ctx := context.Background()
err := waitForContainerPresence(ctx, p, nil, &WaitParams{ err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
timeout: 10 * time.Second, timeout: 10 * time.Second,
pollInterval: 500 * time.Millisecond, pollInterval: 500 * time.Millisecond,
}) })