1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-07 21:47:02 +09:00

Add tests for nodeclient

This commit is contained in:
mcrakhman 2024-03-26 16:16:27 +01:00
parent 3f625dc874
commit 9aa7486623
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
2 changed files with 100 additions and 2 deletions

View file

@ -15,6 +15,10 @@ import (
const CName = "common.node.nodeclient"
func New() NodeClient {
return &nodeClient{}
}
type NodeClient interface {
app.Component
AclGetRecords(ctx context.Context, spaceId, aclHead string) (recs []*consensusproto.RawRecordWithId, err error)
@ -37,7 +41,7 @@ func (c *nodeClient) Name() (name string) {
}
func (c *nodeClient) AclGetRecords(ctx context.Context, spaceId, aclHead string) (recs []*consensusproto.RawRecordWithId, err error) {
err = c.doClient(ctx, spaceId, func(cl spacesyncproto.DRPCSpaceSyncClient) error {
err = clientDo(c, ctx, spaceId, func(cl spacesyncproto.DRPCSpaceSyncClient) error {
resp, err := cl.AclGetRecords(ctx, &spacesyncproto.AclGetRecordsRequest{
SpaceId: spaceId,
AclHead: aclHead,
@ -62,7 +66,7 @@ func (c *nodeClient) AclAddRecord(ctx context.Context, spaceId string, rec *cons
if err != nil {
return
}
err = c.doClient(ctx, spaceId, func(cl spacesyncproto.DRPCSpaceSyncClient) error {
err = clientDo(c, ctx, spaceId, func(cl spacesyncproto.DRPCSpaceSyncClient) error {
res, err := cl.AclAddRecord(ctx, &spacesyncproto.AclAddRecordRequest{
SpaceId: spaceId,
Payload: data,
@ -79,6 +83,8 @@ func (c *nodeClient) AclAddRecord(ctx context.Context, spaceId string, rec *cons
return
}
var clientDo = (*nodeClient).doClient
func (c *nodeClient) doClient(ctx context.Context, spaceId string, f func(cl spacesyncproto.DRPCSpaceSyncClient) error) error {
p, err := c.pool.GetOneOf(ctx, c.nodeConf.NodeIds(spaceId))
if err != nil {

View file

@ -0,0 +1,92 @@
package nodeclient
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/commonspace/spacesyncproto/mock_spacesyncproto"
"github.com/anyproto/any-sync/consensus/consensusproto"
)
var ctx = context.Background()
type fixture struct {
*nodeClient
ctrl *gomock.Controller
}
func (f *fixture) finish() {
f.ctrl.Finish()
}
func newFixture(t *testing.T) *fixture {
ctrl := gomock.NewController(t)
return &fixture{
nodeClient: New().(*nodeClient),
ctrl: ctrl,
}
}
func TestNodeClient_AclGetRecords(t *testing.T) {
f := newFixture(t)
defer f.finish()
spaceId := "spaceId"
aclHead := "aclHead"
cl := mock_spacesyncproto.NewMockDRPCSpaceSyncClient(f.ctrl)
clientDo = func(client *nodeClient, ctx context.Context, s string, f func(cl spacesyncproto.DRPCSpaceSyncClient) error) error {
return f(cl)
}
var (
expectedRecs []*consensusproto.RawRecordWithId
expectedByteRecs [][]byte
total = 5
)
for i := 0; i < total; i++ {
expectedRecs = append(expectedRecs, &consensusproto.RawRecordWithId{
Id: fmt.Sprint(i),
})
marshalled, err := expectedRecs[i].Marshal()
require.NoError(t, err)
expectedByteRecs = append(expectedByteRecs, marshalled)
}
cl.EXPECT().AclGetRecords(ctx, &spacesyncproto.AclGetRecordsRequest{
SpaceId: spaceId,
AclHead: aclHead,
}).Return(&spacesyncproto.AclGetRecordsResponse{Records: expectedByteRecs}, nil)
recs, err := f.AclGetRecords(ctx, spaceId, aclHead)
require.NoError(t, err)
require.Equal(t, expectedRecs, recs)
}
func TestNodeClient_AclAddRecords(t *testing.T) {
f := newFixture(t)
defer f.finish()
spaceId := "spaceId"
cl := mock_spacesyncproto.NewMockDRPCSpaceSyncClient(f.ctrl)
clientDo = func(client *nodeClient, ctx context.Context, s string, f func(cl spacesyncproto.DRPCSpaceSyncClient) error) error {
return f(cl)
}
sendRec := &consensusproto.RawRecord{
AcceptorTimestamp: 10,
}
data, err := sendRec.Marshal()
require.NoError(t, err)
expectedRec := &consensusproto.RawRecordWithId{
Id: "recId",
Payload: data,
}
cl.EXPECT().AclAddRecord(ctx, &spacesyncproto.AclAddRecordRequest{
SpaceId: spaceId,
Payload: data,
}).Return(&spacesyncproto.AclAddRecordResponse{RecordId: expectedRec.Id, Payload: expectedRec.Payload}, nil)
rec, err := f.AclAddRecord(ctx, spaceId, sendRec)
require.NoError(t, err)
require.Equal(t, expectedRec, rec)
}