2021-07-20 08:02:14 +00:00
|
|
|
package pool
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
//go:generate mockgen -destination mock_test.go -source pool.go -mock_names client=MockClient -package pool . client
|
2021-10-26 12:54:07 +00:00
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
import (
|
2022-03-25 08:34:09 +00:00
|
|
|
"bytes"
|
2021-07-20 08:02:14 +00:00
|
|
|
"context"
|
2021-11-22 12:38:15 +00:00
|
|
|
"crypto/ecdsa"
|
2021-07-20 08:02:14 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/google/uuid"
|
2021-12-10 13:56:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-03-25 08:34:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/container"
|
2022-04-07 16:09:15 +00:00
|
|
|
neofsecdsa "github.com/nspcc-dev/neofs-sdk-go/crypto/ecdsa"
|
2021-11-09 08:20:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
2022-02-07 20:27:56 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
2022-05-25 09:03:22 +00:00
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
2021-11-09 08:20:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
2022-04-11 06:30:22 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2021-07-20 08:02:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-09-07 14:34:20 +00:00
|
|
|
"go.uber.org/zap"
|
2021-07-20 08:02:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestBuildPoolClientFailed(t *testing.T) {
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPoolCreateSessionFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
ni := &netmap.NodeInfo{}
|
2022-06-07 08:25:34 +00:00
|
|
|
ni.SetNetworkEndpoints("addr1", "addr2")
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error session")).AnyTimes()
|
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(&netmap.NodeInfo{}, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
func newPrivateKey(t *testing.T) *ecdsa.PrivateKey {
|
2021-12-10 13:56:04 +00:00
|
|
|
p, err := keys.NewPrivateKey()
|
2021-11-22 12:38:15 +00:00
|
|
|
require.NoError(t, err)
|
2021-12-10 13:56:04 +00:00
|
|
|
return &p.PrivateKey
|
2021-11-22 12:38:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func newBinPublicKey(t *testing.T) []byte {
|
|
|
|
authKey := neofsecdsa.PublicKey(newPrivateKey(t).PublicKey)
|
|
|
|
|
|
|
|
bKey := make([]byte, authKey.MaxEncodedSize())
|
|
|
|
bKey = bKey[:authKey.Encode(bKey)]
|
|
|
|
|
|
|
|
return bKey
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:34:20 +00:00
|
|
|
func TestBuildPoolOneNodeFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
ctrl2 := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var expectedToken *session.Object
|
2021-09-07 14:34:20 +00:00
|
|
|
clientCount := -1
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-09-07 14:34:20 +00:00
|
|
|
clientCount++
|
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-04-08 06:33:59 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
|
|
|
tok := newToken(t)
|
2022-04-07 16:09:15 +00:00
|
|
|
id := tok.ID()
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}, nil
|
2021-09-07 14:34:20 +00:00
|
|
|
}).AnyTimes()
|
|
|
|
|
2022-04-08 06:33:59 +00:00
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error")).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-09-07 14:34:20 +00:00
|
|
|
|
|
|
|
mockClient2 := NewMockClient(ctrl2)
|
2022-04-08 06:33:59 +00:00
|
|
|
mockClient2.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
|
|
|
expectedToken = newToken(t)
|
2022-04-07 16:09:15 +00:00
|
|
|
id := expectedToken.ID()
|
2022-04-08 06:33:59 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-04-08 06:33:59 +00:00
|
|
|
}, nil
|
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient2.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-09-07 14:34:20 +00:00
|
|
|
|
|
|
|
if clientCount == 0 {
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
return mockClient2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
log, err := zap.NewProduction()
|
|
|
|
require.NoError(t, err)
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
2021-09-07 14:34:20 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 1000 * time.Millisecond,
|
|
|
|
logger: log,
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{9, "peer0", 1},
|
|
|
|
{1, "peer1", 1},
|
|
|
|
},
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
clientPool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = clientPool.Dial(context.Background())
|
2021-09-07 14:34:20 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(clientPool.Close)
|
2021-09-07 14:34:20 +00:00
|
|
|
|
|
|
|
condition := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := clientPool.connection()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := clientPool.cache.Get(formCacheKey(cp.address, clientPool.key))
|
|
|
|
return areEqualTokens(&st, expectedToken)
|
2021-09-07 14:34:20 +00:00
|
|
|
}
|
|
|
|
require.Never(t, condition, 900*time.Millisecond, 100*time.Millisecond)
|
|
|
|
require.Eventually(t, condition, 3*time.Second, 300*time.Millisecond)
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPoolZeroNodes(t *testing.T) {
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2022-03-11 14:55:02 +00:00
|
|
|
_, err := NewPool(opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneNode(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
uid := uuid.New()
|
|
|
|
|
|
|
|
var tok session.Object
|
2021-07-20 08:02:14 +00:00
|
|
|
tok.SetID(uid)
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
tokRes := &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: uid[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(tokRes, nil)
|
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(&netmap.NodeInfo{}, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{{1, "peer0", 1}},
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, areEqualTokens(&tok, &st))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func areEqualTokens(t1, t2 *session.Object) bool {
|
2022-03-25 08:34:09 +00:00
|
|
|
if t1 == nil || t2 == nil {
|
|
|
|
return false
|
|
|
|
}
|
2022-04-07 16:09:15 +00:00
|
|
|
|
|
|
|
id1, id2 := t1.ID(), t2.ID()
|
|
|
|
return bytes.Equal(id1[:], id2[:])
|
2022-03-25 08:34:09 +00:00
|
|
|
}
|
2021-11-17 15:52:43 +00:00
|
|
|
|
2022-03-25 08:34:09 +00:00
|
|
|
func TestTwoNodes(t *testing.T) {
|
2021-07-20 08:02:14 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var tokens []*session.Object
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var tok session.Object
|
|
|
|
uid := uuid.New()
|
2021-07-20 08:02:14 +00:00
|
|
|
tok.SetID(uid)
|
2022-04-07 16:09:15 +00:00
|
|
|
tokens = append(tokens, &tok)
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: uid[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
|
|
|
}, nil
|
2021-07-20 08:02:14 +00:00
|
|
|
})
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(&netmap.NodeInfo{}, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{1, "peer1", 1},
|
|
|
|
},
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, containsTokens(tokens, &st))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func containsTokens(list []*session.Object, item *session.Object) bool {
|
2022-03-25 08:34:09 +00:00
|
|
|
for _, tok := range list {
|
|
|
|
if areEqualTokens(tok, item) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2021-11-17 15:52:43 +00:00
|
|
|
|
2022-03-25 08:34:09 +00:00
|
|
|
func TestOneOfTwoFailed(t *testing.T) {
|
2021-07-20 08:02:14 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
ctrl2 := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var tokens []*session.Object
|
2021-07-20 08:02:14 +00:00
|
|
|
clientCount := -1
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
clientCount++
|
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
tok := newToken(t)
|
|
|
|
tokens = append(tokens, tok)
|
2022-04-07 16:09:15 +00:00
|
|
|
id := tok.ID()
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}, nil
|
2021-07-20 08:02:14 +00:00
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
mockClient2 := NewMockClient(ctrl2)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
tok := newToken(t)
|
|
|
|
tokens = append(tokens, tok)
|
2022-04-07 16:09:15 +00:00
|
|
|
id := tok.ID()
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}, nil
|
2021-07-20 08:02:14 +00:00
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).DoAndReturn(func(_ interface{}, _ ...interface{}) (*netmap.NodeInfo, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().networkInfo(gomock.Any(), gomock.Any()).DoAndReturn(func(_ interface{}, _ ...interface{}) (*netmap.NetworkInfo, error) {
|
2022-02-02 13:25:36 +00:00
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
if clientCount == 0 {
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
return mockClient2, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{9, "peer1", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 200 * time.Millisecond,
|
2022-03-11 11:45:36 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(context.Background())
|
|
|
|
require.NoError(t, err)
|
2022-03-11 11:45:36 +00:00
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
|
|
|
for i := 0; i < 5; i++ {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, areEqualTokens(tokens[0], &st))
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwoFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-07-20 08:02:14 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-04-07 16:09:15 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(&resCreateSession{
|
|
|
|
id: uuid.Nil[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
|
|
|
}, nil).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
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()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{1, "peer1", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 200 * time.Millisecond,
|
2022-03-11 11:45:36 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-11 14:55:02 +00:00
|
|
|
err = pool.Dial(context.Background())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
[#48] pool: add `Close` method
Fix occasional panic in tests:
```
> for i in (seq 1 100); go test -race -count=1 ./pool/... ; end
...
{"level":"warn","ts":1635251466.567485,"caller":"pool/pool.go:122","msg":"failed to create neofs session token for client","address":"peer0","error":"error session"}
panic: Fail in goroutine after TestTwoNodes has completed
goroutine 6 [running]:
testing.(*common).Fail(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:710 +0x1b4
testing.(*common).FailNow(0xc0002e1380)
/usr/lib/go/src/testing/testing.go:732 +0x2f
testing.(*common).Fatalf(0xc000074070, {0xd9d816, 0x2e}, {0xc000094050, 0x5, 0x5})
/usr/lib/go/src/testing/testing.go:830 +0x85
github.com/golang/mock/gomock.(*Controller).Call.func1(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:231 +0x44d
github.com/golang/mock/gomock.(*Controller).Call(0xc0002f4120, {0xd68380, 0xc0002dac30}, {0xd8847f, 0xc}, {0xc000074020, 0x1, 0x1})
/home/dzeta/go/pkg/mod/github.com/golang/mock@v1.6.0/gomock/controller.go:247 +0xce
github.com/nspcc-dev/neofs-sdk-go/pool.(*MockClient).EndpointInfo(0xc0002dac30, {0xe85528, 0xc00008a120}, {0x0, 0x0, 0x0})
/home/dzeta/repo/neofs-sdk-go/pool/mock_test.go:186 +0x298
github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth.func1(0x1, {0xe950d8, 0xc0002dac30})
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:183 +0x188
created by github.com/nspcc-dev/neofs-sdk-go/pool.updateNodesHealth
/home/dzeta/repo/neofs-sdk-go/pool/pool.go:174 +0x233
```
Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
2021-10-26 12:36:08 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
time.Sleep(2 * time.Second)
|
|
|
|
|
2022-03-15 12:00:38 +00:00
|
|
|
_, err = pool.connection()
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "no healthy")
|
|
|
|
}
|
|
|
|
|
2021-10-25 12:57:55 +00:00
|
|
|
func TestSessionCache(t *testing.T) {
|
2021-10-25 13:24:43 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
2021-10-25 12:57:55 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var tokens []*session.Object
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-10-25 13:24:43 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*resCreateSession, error) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var tok session.Object
|
|
|
|
uid := uuid.New()
|
2021-10-25 13:24:43 +00:00
|
|
|
tok.SetID(uid)
|
2022-04-07 16:09:15 +00:00
|
|
|
tokens = append(tokens, &tok)
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: uid[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
|
|
|
}, nil
|
2021-11-03 14:34:02 +00:00
|
|
|
}).MaxTimes(3)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().objectGet(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("session token does not exist"))
|
|
|
|
mockClient.EXPECT().objectPut(gomock.Any(), gomock.Any()).Return(nil, nil)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 30 * time.Second,
|
2022-03-11 11:45:36 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2021-11-16 13:50:33 +00:00
|
|
|
t.Cleanup(pool.Close)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, containsTokens(tokens, &st))
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm PrmObjectGet
|
2022-05-25 09:03:22 +00:00
|
|
|
prm.SetAddress(oid.Address{})
|
2022-04-07 16:09:15 +00:00
|
|
|
prm.UseSession(session.Object{})
|
2022-03-11 13:06:08 +00:00
|
|
|
|
|
|
|
_, err = pool.GetObject(ctx, prm)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// cache must not contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err = pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
_, ok := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.False(t, ok)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm2 PrmObjectPut
|
|
|
|
prm2.SetHeader(object.Object{})
|
|
|
|
|
|
|
|
_, err = pool.PutObject(ctx, prm2)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err = pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ = pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, containsTokens(tokens, &st))
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 13:50:33 +00:00
|
|
|
func TestPriority(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
ctrl2 := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
tokens := make([]*session.Object, 2)
|
2022-03-25 08:34:09 +00:00
|
|
|
clientBuilder := func(endpoint string) (client, error) {
|
2021-11-16 13:50:33 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2021-11-16 13:50:33 +00:00
|
|
|
tok := newToken(t)
|
2022-03-25 08:34:09 +00:00
|
|
|
tokens[0] = tok
|
2022-04-07 16:09:15 +00:00
|
|
|
id := tok.ID()
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}, nil
|
2021-11-16 13:50:33 +00:00
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
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()
|
2021-11-16 13:50:33 +00:00
|
|
|
|
|
|
|
mockClient2 := NewMockClient(ctrl2)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2021-11-16 13:50:33 +00:00
|
|
|
tok := newToken(t)
|
2022-03-25 08:34:09 +00:00
|
|
|
tokens[1] = tok
|
2022-04-07 16:09:15 +00:00
|
|
|
id := tok.ID()
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: id[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
2022-03-25 08:34:09 +00:00
|
|
|
}, nil
|
2021-11-16 13:50:33 +00:00
|
|
|
}).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient2.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(&netmap.NodeInfo{}, nil).AnyTimes()
|
|
|
|
mockClient2.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2021-11-16 13:50:33 +00:00
|
|
|
|
2022-03-25 08:34:09 +00:00
|
|
|
if endpoint == "peer0" {
|
2021-11-16 13:50:33 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
return mockClient2, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
{2, "peer1", 100},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 1500 * time.Millisecond,
|
2022-03-11 11:45:36 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
t.Cleanup(pool.Close)
|
|
|
|
|
|
|
|
firstNode := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
return areEqualTokens(&st, tokens[0])
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
secondNode := func() bool {
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-11-16 13:50:33 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
return areEqualTokens(&st, tokens[1])
|
2021-11-16 13:50:33 +00:00
|
|
|
}
|
|
|
|
require.Never(t, secondNode, time.Second, 200*time.Millisecond)
|
|
|
|
|
|
|
|
require.Eventually(t, secondNode, time.Second, 200*time.Millisecond)
|
|
|
|
require.Never(t, firstNode, time.Second, 200*time.Millisecond)
|
|
|
|
}
|
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
func TestSessionCacheWithKey(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
var tokens []*session.Object
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2021-10-25 13:24:43 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}) (*resCreateSession, error) {
|
2022-04-07 16:09:15 +00:00
|
|
|
var tok session.Object
|
|
|
|
uid := uuid.New()
|
2021-10-25 13:24:43 +00:00
|
|
|
tok.SetID(uid)
|
2022-04-07 16:09:15 +00:00
|
|
|
tokens = append(tokens, &tok)
|
2022-03-28 09:17:56 +00:00
|
|
|
return &resCreateSession{
|
2022-04-07 16:09:15 +00:00
|
|
|
id: uid[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
|
|
|
}, nil
|
2021-10-25 13:24:43 +00:00
|
|
|
}).MaxTimes(2)
|
|
|
|
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().objectGet(gomock.Any(), gomock.Any()).Return(nil, nil)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-03-15 12:49:14 +00:00
|
|
|
clientRebalanceInterval: 30 * time.Second,
|
2022-03-11 11:45:36 +00:00
|
|
|
clientBuilder: clientBuilder,
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
pool, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = pool.Dial(ctx)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// cache must contain session token
|
2022-03-15 12:00:38 +00:00
|
|
|
cp, err := pool.connection()
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
2022-04-07 16:09:15 +00:00
|
|
|
st, _ := pool.cache.Get(formCacheKey(cp.address, pool.key))
|
|
|
|
require.True(t, containsTokens(tokens, &st))
|
2021-10-25 13:24:43 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm PrmObjectGet
|
2022-05-25 09:03:22 +00:00
|
|
|
prm.SetAddress(oid.Address{})
|
2022-03-11 13:06:08 +00:00
|
|
|
prm.UseKey(newPrivateKey(t))
|
|
|
|
|
|
|
|
_, err = pool.GetObject(ctx, prm)
|
2021-10-25 13:24:43 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Len(t, tokens, 2)
|
2021-10-25 12:57:55 +00:00
|
|
|
}
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
func newToken(t *testing.T) *session.Object {
|
|
|
|
var tok session.Object
|
|
|
|
tok.SetID(uuid.New())
|
2021-07-20 08:02:14 +00:00
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
return &tok
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
|
|
|
|
2022-01-17 10:35:39 +00:00
|
|
|
func TestSessionTokenOwner(t *testing.T) {
|
2022-04-07 16:09:15 +00:00
|
|
|
t.Skip() // neofs-sdk-go#???
|
2022-01-17 10:35:39 +00:00
|
|
|
ctrl := gomock.NewController(t)
|
2022-03-15 12:00:38 +00:00
|
|
|
clientBuilder := func(_ string) (client, error) {
|
2022-01-17 10:35:39 +00:00
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-04-07 16:09:15 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(&resCreateSession{
|
|
|
|
id: uuid.Nil[:],
|
|
|
|
sessionKey: newBinPublicKey(t),
|
|
|
|
}, nil).AnyTimes()
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(&netmap.NodeInfo{}, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().networkInfo(gomock.Any(), gomock.Any()).Return(&netmap.NetworkInfo{}, nil).AnyTimes()
|
2022-01-17 10:35:39 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
2022-03-11 11:45:36 +00:00
|
|
|
opts := InitParameters{
|
2022-03-15 12:49:14 +00:00
|
|
|
key: newPrivateKey(t),
|
|
|
|
nodeParams: []NodeParam{
|
2022-03-11 11:45:36 +00:00
|
|
|
{1, "peer0", 1},
|
|
|
|
},
|
2022-01-17 10:35:39 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2022-03-11 14:55:02 +00:00
|
|
|
p, err := NewPool(opts)
|
|
|
|
require.NoError(t, err)
|
|
|
|
err = p.Dial(ctx)
|
2022-01-17 10:35:39 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-09 09:40:23 +00:00
|
|
|
t.Cleanup(p.Close)
|
2022-01-17 10:35:39 +00:00
|
|
|
|
|
|
|
anonKey := newPrivateKey(t)
|
2022-04-11 06:30:22 +00:00
|
|
|
|
|
|
|
var anonOwner user.ID
|
|
|
|
user.IDFromKey(&anonOwner, anonKey.PublicKey)
|
2022-01-17 10:35:39 +00:00
|
|
|
|
2022-03-11 13:06:08 +00:00
|
|
|
var prm prmCommon
|
|
|
|
prm.UseKey(anonKey)
|
2022-03-25 08:34:09 +00:00
|
|
|
var prmCtx prmContext
|
|
|
|
prmCtx.useDefaultSession()
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
var cc callContext
|
|
|
|
cc.Context = ctx
|
2022-04-07 16:09:15 +00:00
|
|
|
cc.sessionTarget = func(session.Object) {}
|
2022-03-25 08:34:09 +00:00
|
|
|
err = p.initCallContext(&cc, prm, prmCtx)
|
2022-01-17 10:35:39 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-17 09:53:04 +00:00
|
|
|
err = p.openDefaultSession(&cc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-04-07 16:09:15 +00:00
|
|
|
tkn, _ := p.cache.Get(formCacheKey("peer0", anonKey))
|
|
|
|
require.True(t, tkn.VerifySignature())
|
2022-01-17 10:35:39 +00:00
|
|
|
}
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
func TestWaitPresence(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
mockClient := NewMockClient(ctrl)
|
2022-03-28 09:17:56 +00:00
|
|
|
mockClient.EXPECT().sessionCreate(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().endpointInfo(gomock.Any(), gomock.Any()).Return(nil, 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()
|
2021-07-20 08:02:14 +00:00
|
|
|
|
|
|
|
t.Run("context canceled", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
2022-04-08 06:33:59 +00:00
|
|
|
err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
|
2022-03-28 08:06:23 +00:00
|
|
|
timeout: 120 * time.Second,
|
|
|
|
pollInterval: 5 * time.Second,
|
|
|
|
})
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context canceled")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("context deadline exceeded", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-04-08 06:33:59 +00:00
|
|
|
err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
|
2022-03-15 12:49:14 +00:00
|
|
|
timeout: 500 * time.Millisecond,
|
|
|
|
pollInterval: 5 * time.Second,
|
2021-07-20 08:02:14 +00:00
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context deadline exceeded")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
2022-04-08 06:33:59 +00:00
|
|
|
err := waitForContainerPresence(ctx, mockClient, nil, &WaitParams{
|
2022-03-15 12:49:14 +00:00
|
|
|
timeout: 10 * time.Second,
|
|
|
|
pollInterval: 500 * time.Millisecond,
|
2021-07-20 08:02:14 +00:00
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|