Skip to content

Commit f50ff81

Browse files
committed
sn/policer: Add unit tests
Signed-off-by: Leonard Lyubich <[email protected]>
1 parent ae06899 commit f50ff81

File tree

8 files changed

+681
-8
lines changed

8 files changed

+681
-8
lines changed

internal/slices/slices.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,12 @@ func AllZeros(s []byte) bool {
4747
}
4848
return true
4949
}
50+
51+
// RepeatElement returns slice of n shallow copies of e.
52+
func RepeatElement[E any, S []E](n int, e E) S {
53+
s := make(S, n)
54+
for i := range s {
55+
s[i] = e
56+
}
57+
return s
58+
}

internal/slices/slices_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package slices_test
22

33
import (
4+
"errors"
45
"slices"
6+
"strconv"
57
"testing"
68

79
islices "github.com/nspcc-dev/neofs-node/internal/slices"
@@ -57,3 +59,37 @@ func TestAllZeros(t *testing.T) {
5759
require.False(t, islices.AllZeros(sc), i)
5860
}
5961
}
62+
63+
func TestRepeatElements(t *testing.T) {
64+
tcs := []struct {
65+
name string
66+
e any
67+
}{
68+
{name: "int", e: 1},
69+
{name: "bool", e: true},
70+
{name: "error", e: errors.New("some error")},
71+
{name: "nil", e: nil},
72+
{name: "struct", e: struct {
73+
i int
74+
s string
75+
}{
76+
i: 1,
77+
s: "foo",
78+
}},
79+
{name: "slice", e: []string{"foo", "bar"}},
80+
}
81+
82+
for _, tc := range tcs {
83+
t.Run(tc.name, func(t *testing.T) {
84+
for _, n := range []int{0, 1, 10} {
85+
t.Run(strconv.Itoa(n), func(t *testing.T) {
86+
s := islices.RepeatElement(n, tc.e)
87+
require.Len(t, s, n)
88+
for i := range s {
89+
require.Equal(t, tc.e, s[i])
90+
}
91+
})
92+
}
93+
})
94+
}
95+
}

internal/testutil/neofs.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package testutil
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/nspcc-dev/neofs-sdk-go/netmap"
7+
)
8+
9+
// Nodes returns n [netmap.NodeInfo] elements with unique public keys.
10+
func Nodes(n int) []netmap.NodeInfo {
11+
nodes := make([]netmap.NodeInfo, n)
12+
for i := range nodes {
13+
nodes[i].SetPublicKey([]byte("public_key_" + strconv.Itoa(i)))
14+
}
15+
return nodes
16+
}

internal/testutil/neofs_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package testutil_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/nspcc-dev/neofs-node/internal/testutil"
7+
"github.com/stretchr/testify/require"
8+
)
9+
10+
func TestNodes(t *testing.T) {
11+
t.Run("empty", func(t *testing.T) {
12+
require.Empty(t, testutil.Nodes(0))
13+
})
14+
15+
s := testutil.Nodes(10)
16+
17+
m := make(map[string]struct{})
18+
for i := range s {
19+
m[string(s[i].PublicKey())] = struct{}{}
20+
}
21+
require.Len(t, m, len(s))
22+
}

pkg/services/policer/check.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66

77
"github.com/nspcc-dev/neofs-node/pkg/core/container"
88
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
9-
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
109
"github.com/nspcc-dev/neofs-node/pkg/services/replicator"
1110
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
1211
"github.com/nspcc-dev/neofs-sdk-go/netmap"
@@ -193,8 +192,6 @@ type processPlacementContext struct {
193192
}
194193

195194
func (p *Policer) processNodes(ctx context.Context, plc *processPlacementContext, nodes []netmap.NodeInfo, shortage uint32) {
196-
prm := new(headsvc.RemoteHeadPrm).WithObjectAddress(plc.object.Address)
197-
198195
p.cfg.RLock()
199196
headTimeout := p.headTimeout
200197
p.cfg.RUnlock()
@@ -261,7 +258,7 @@ func (p *Policer) processNodes(ctx context.Context, plc *processPlacementContext
261258

262259
callCtx, cancel := context.WithTimeout(ctx, headTimeout)
263260

264-
_, err := p.remoteHeader.Head(callCtx, prm.WithNodeInfo(nodes[i]))
261+
_, err := p.apiConns.headObject(callCtx, nodes[i], plc.object.Address)
265262

266263
cancel()
267264

pkg/services/policer/policer.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package policer
22

33
import (
4+
"context"
45
"sync"
56
"time"
67

78
"github.com/nspcc-dev/neofs-node/pkg/core/container"
89
"github.com/nspcc-dev/neofs-node/pkg/core/netmap"
10+
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
911
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
1012
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
1113
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
1214
"github.com/nspcc-dev/neofs-node/pkg/services/replicator"
15+
netmapsdk "github.com/nspcc-dev/neofs-sdk-go/netmap"
16+
"github.com/nspcc-dev/neofs-sdk-go/object"
1317
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
1418
"github.com/panjf2000/ants/v2"
1519
"go.uber.org/zap"
@@ -21,6 +25,22 @@ type nodeLoader interface {
2125
ObjectServiceLoad() float64
2226
}
2327

28+
// interface of [replicator.Replicator] used by [Policer] for overriding in tests.
29+
type replicatorIface interface {
30+
HandleTask(context.Context, replicator.Task, replicator.TaskResult)
31+
}
32+
33+
// interface of [engine.StorageEngine] used by [Policer] for overriding in tests.
34+
type localStorage interface {
35+
ListWithCursor(uint32, *engine.Cursor) ([]objectcore.AddressWithType, *engine.Cursor, error)
36+
Delete(oid.Address) error
37+
}
38+
39+
// interface of [headsvc.RemoteHeader] used by [Policer] for overriding in tests.
40+
type apiConnections interface {
41+
headObject(context.Context, netmapsdk.NodeInfo, oid.Address) (object.Object, error)
42+
}
43+
2444
type objectsInWork struct {
2545
m sync.RWMutex
2646
objs map[oid.Address]struct{}
@@ -85,11 +105,11 @@ type cfg struct {
85105

86106
placementBuilder placement.Builder
87107

88-
remoteHeader *headsvc.RemoteHeader
108+
apiConns apiConnections
89109

90110
netmapKeys netmap.AnnouncedKeys
91111

92-
replicator *replicator.Replicator
112+
replicator replicatorIface
93113

94114
cbRedundantCopy RedundantCopyCallback
95115

@@ -164,10 +184,24 @@ func WithPlacementBuilder(v placement.Builder) Option {
164184
}
165185
}
166186

187+
type remoteHeader headsvc.RemoteHeader
188+
189+
func (x *remoteHeader) headObject(ctx context.Context, node netmapsdk.NodeInfo, addr oid.Address) (object.Object, error) {
190+
var p headsvc.RemoteHeadPrm
191+
p.WithNodeInfo(node)
192+
p.WithObjectAddress(addr)
193+
hdr, err := (*headsvc.RemoteHeader)(x).Head(ctx, &p)
194+
if err != nil {
195+
return object.Object{}, err
196+
}
197+
198+
return *hdr, nil
199+
}
200+
167201
// WithRemoteHeader returns option to set object header receiver of Policer.
168202
func WithRemoteHeader(v *headsvc.RemoteHeader) Option {
169203
return func(c *cfg) {
170-
c.remoteHeader = v
204+
c.apiConns = (*remoteHeader)(v)
171205
}
172206
}
173207

0 commit comments

Comments
 (0)