2021-07-20 08:02:14 +00:00
|
|
|
package pool
|
|
|
|
|
2021-10-26 12:54:07 +00:00
|
|
|
//go:generate mockgen -destination mock_test.go -package pool . Client
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
import (
|
|
|
|
"context"
|
2021-11-22 12:38:15 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
crand "crypto/rand"
|
2021-07-20 08:02:14 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/mock/gomock"
|
|
|
|
"github.com/google/uuid"
|
2021-11-09 08:20:09 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/client"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/netmap"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/session"
|
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) {
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
_, err := pb.Build(context.TODO(), opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuildPoolCreateSessionFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
ni := &netmap.NodeInfo{}
|
|
|
|
ni.SetAddresses("addr1", "addr2")
|
|
|
|
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
mockClient := NewMockClient(ctrl)
|
2021-09-07 14:34:20 +00:00
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error session")).AnyTimes()
|
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(&client.EndpointInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
_, err := pb.Build(context.TODO(), opts)
|
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 {
|
|
|
|
p, err := ecdsa.GenerateKey(elliptic.P256(), crand.Reader)
|
|
|
|
require.NoError(t, err)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2021-09-07 14:34:20 +00:00
|
|
|
func TestBuildPoolOneNodeFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
ctrl2 := gomock.NewController(t)
|
|
|
|
|
|
|
|
ni := &netmap.NodeInfo{}
|
|
|
|
ni.SetAddresses("addr1", "addr2")
|
|
|
|
|
|
|
|
var expectedToken *session.Token
|
|
|
|
clientCount := -1
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
clientCount++
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockInvokes := 0
|
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*session.Token, error) {
|
|
|
|
mockInvokes++
|
|
|
|
if mockInvokes == 1 {
|
|
|
|
expectedToken = newToken(t)
|
|
|
|
return nil, fmt.Errorf("error session")
|
|
|
|
}
|
|
|
|
return expectedToken, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
|
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
|
|
|
|
mockClient2 := NewMockClient(ctrl2)
|
|
|
|
mockClient2.EXPECT().CreateSession(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient2.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
|
|
|
|
if clientCount == 0 {
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
return mockClient2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 9)
|
|
|
|
pb.AddNode("peer1", 1)
|
|
|
|
|
|
|
|
log, err := zap.NewProduction()
|
|
|
|
require.NoError(t, err)
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-09-07 14:34:20 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
ClientRebalanceInterval: 1000 * time.Millisecond,
|
|
|
|
Logger: log,
|
|
|
|
}
|
|
|
|
|
|
|
|
clientPool, err := pb.Build(context.TODO(), opts)
|
|
|
|
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 {
|
|
|
|
_, st, err := clientPool.Connection()
|
|
|
|
return err == nil && st == expectedToken
|
|
|
|
}
|
|
|
|
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) {
|
|
|
|
pb := new(Builder)
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
}
|
2021-11-22 12:38:15 +00:00
|
|
|
_, err := pb.Build(context.TODO(), opts)
|
2021-07-20 08:02:14 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneNode(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
tok := session.NewToken()
|
|
|
|
uid, err := uuid.New().MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tok.SetID(uid)
|
|
|
|
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any()).Return(tok, nil)
|
2021-10-26 12:55:07 +00:00
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(&client.EndpointInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
[#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
|
|
|
pool, err := pb.Build(context.Background(), opts)
|
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
|
|
|
|
|
|
|
_, st, err := pool.Connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tok, st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwoNodes(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
var tokens []*session.Token
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockClient.EXPECT().CreateSession(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
|
|
|
|
})
|
2021-10-26 12:55:07 +00:00
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(&client.EndpointInfo{}, nil).AnyTimes()
|
2021-07-20 08:02:14 +00:00
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
pb.AddNode("peer1", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
}
|
|
|
|
|
[#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
|
|
|
pool, err := pb.Build(context.Background(), opts)
|
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
|
|
|
|
|
|
|
_, st, err := pool.Connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Contains(t, tokens, st)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOneOfTwoFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
ctrl2 := gomock.NewController(t)
|
|
|
|
|
|
|
|
var tokens []*session.Token
|
|
|
|
clientCount := -1
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
clientCount++
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*session.Token, error) {
|
|
|
|
tok := newToken(t)
|
|
|
|
tokens = append(tokens, tok)
|
|
|
|
return tok, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
|
|
|
|
mockClient2 := NewMockClient(ctrl2)
|
|
|
|
mockClient2.EXPECT().CreateSession(gomock.Any(), gomock.Any()).DoAndReturn(func(_, _ interface{}, _ ...interface{}) (*session.Token, error) {
|
|
|
|
tok := newToken(t)
|
|
|
|
tokens = append(tokens, tok)
|
|
|
|
return tok, nil
|
|
|
|
}).AnyTimes()
|
|
|
|
mockClient2.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).DoAndReturn(func(_ interface{}, _ ...interface{}) (*client.EndpointInfo, error) {
|
|
|
|
return nil, fmt.Errorf("error")
|
|
|
|
}).AnyTimes()
|
|
|
|
|
|
|
|
if clientCount == 0 {
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
return mockClient2, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
pb.AddNode("peer1", 9)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
ClientRebalanceInterval: 200 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
[#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
|
|
|
pool, err := pb.Build(context.Background(), opts)
|
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++ {
|
|
|
|
_, st, err := pool.Connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, tokens[0], st)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTwoFailed(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
clientBuilder := func(opts ...client.Option) (client.Client, error) {
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("error")).AnyTimes()
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
pb.AddNode("peer1", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-07-20 08:02:14 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
ClientRebalanceInterval: 200 * time.Millisecond,
|
|
|
|
}
|
|
|
|
|
[#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
|
|
|
pool, err := pb.Build(context.Background(), opts)
|
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)
|
|
|
|
|
|
|
|
_, _, err = pool.Connection()
|
|
|
|
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
|
|
|
|
2021-10-25 13:24:43 +00:00
|
|
|
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
|
2021-11-03 14:34:02 +00:00
|
|
|
}).MaxTimes(3)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
mockClient.EXPECT().GetObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, fmt.Errorf("session token does not exist"))
|
2021-11-15 06:35:30 +00:00
|
|
|
mockClient.EXPECT().PutObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil)
|
2021-10-25 13:24:43 +00:00
|
|
|
|
|
|
|
return mockClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-11-08 13:28:28 +00:00
|
|
|
clientBuilder: clientBuilder,
|
|
|
|
ClientRebalanceInterval: 30 * time.Second,
|
2021-10-25 13:24:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
_, err = pool.GetObject(ctx, nil, retry())
|
2021-10-25 13:24:43 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
// cache must not contain session token
|
|
|
|
_, st, err = pool.Connection()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Nil(t, st)
|
|
|
|
|
2021-11-15 06:35:30 +00:00
|
|
|
_, err = pool.PutObject(ctx, nil)
|
2021-10-25 13:24:43 +00:00
|
|
|
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)
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
pb := new(Builder)
|
|
|
|
pb.AddNode("peer0", 1)
|
|
|
|
|
|
|
|
opts := &BuilderOptions{
|
2021-11-22 12:38:15 +00:00
|
|
|
Key: newPrivateKey(t),
|
2021-10-25 13:24:43 +00:00
|
|
|
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)
|
|
|
|
|
2021-11-22 12:38:15 +00:00
|
|
|
_, err = pool.GetObject(ctx, nil, WithKey(newPrivateKey(t)))
|
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
|
|
|
}
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
func newToken(t *testing.T) *session.Token {
|
|
|
|
tok := session.NewToken()
|
|
|
|
uid, err := uuid.New().MarshalBinary()
|
|
|
|
require.NoError(t, err)
|
|
|
|
tok.SetID(uid)
|
|
|
|
|
|
|
|
return tok
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitPresence(t *testing.T) {
|
|
|
|
ctrl := gomock.NewController(t)
|
|
|
|
|
|
|
|
mockClient := NewMockClient(ctrl)
|
|
|
|
mockClient.EXPECT().CreateSession(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().EndpointInfo(gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
mockClient.EXPECT().GetContainer(gomock.Any(), gomock.Any(), gomock.Any()).Return(nil, nil).AnyTimes()
|
|
|
|
|
2021-11-08 13:28:28 +00:00
|
|
|
cache, err := NewCache()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-07-20 08:02:14 +00:00
|
|
|
p := &pool{
|
|
|
|
sampler: NewSampler([]float64{1}, rand.NewSource(0)),
|
|
|
|
clientPacks: []*clientPack{{
|
|
|
|
client: mockClient,
|
|
|
|
healthy: true,
|
|
|
|
}},
|
2021-11-22 12:38:15 +00:00
|
|
|
key: newPrivateKey(t),
|
2021-11-08 13:28:28 +00:00
|
|
|
cache: cache,
|
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()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := p.WaitForContainerPresence(ctx, nil, DefaultPollingParams())
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context canceled")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("context deadline exceeded", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
err := p.WaitForContainerPresence(ctx, nil, &ContainerPollingParams{
|
|
|
|
CreationTimeout: 500 * time.Millisecond,
|
|
|
|
PollInterval: 5 * time.Second,
|
|
|
|
})
|
|
|
|
require.Error(t, err)
|
|
|
|
require.Contains(t, err.Error(), "context deadline exceeded")
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("ok", func(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
err := p.WaitForContainerPresence(ctx, nil, &ContainerPollingParams{
|
|
|
|
CreationTimeout: 10 * time.Second,
|
|
|
|
PollInterval: 500 * time.Millisecond,
|
|
|
|
})
|
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
|
|
|
}
|