1
0
Fork 0
mirror of https://github.com/anyproto/any-sync.git synced 2025-06-09 17:45:03 +09:00

Add acl new protocol and tests

This commit is contained in:
mcrakhman 2024-08-05 20:03:16 +02:00
parent 0884834ad2
commit fdc190c70f
No known key found for this signature in database
GPG key ID: DED12CFEF5B8396B
3 changed files with 53 additions and 5 deletions

View file

@ -11,6 +11,7 @@ import (
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/commonspace/syncstatus"
"github.com/anyproto/any-sync/consensus/consensusproto"
"github.com/anyproto/any-sync/net/secureservice"
)
var (
@ -58,7 +59,18 @@ func (s *syncAclHandler) HandleMessage(ctx context.Context, senderId string, pro
case content.GetFullSyncRequest() != nil:
return ErrMessageIsRequest
case content.GetFullSyncResponse() != nil:
return s.syncProtocol.FullSyncResponse(ctx, senderId, content.GetFullSyncResponse())
err := s.syncProtocol.FullSyncResponse(ctx, senderId, content.GetFullSyncResponse())
if err != nil {
return err
}
if protoVersion <= secureservice.ProtoVersion || s.aclList.Head().Id == head {
return nil
}
req, err := s.syncClient.CreateFullSyncRequest(s.aclList, head)
if err != nil {
return err
}
return s.syncClient.QueueRequest(senderId, req)
}
return
}

View file

@ -10,11 +10,13 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
"github.com/anyproto/any-sync/commonspace/object/acl/list"
"github.com/anyproto/any-sync/commonspace/object/acl/list/mock_list"
"github.com/anyproto/any-sync/commonspace/object/acl/syncacl/mock_syncacl"
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/commonspace/syncstatus"
"github.com/anyproto/any-sync/consensus/consensusproto"
"github.com/anyproto/any-sync/net/secureservice"
)
type testAclMock struct {
@ -182,6 +184,43 @@ func TestSyncAclHandler_HandleMessage(t *testing.T) {
err := fx.syncHandler.HandleMessage(ctx, fx.senderId, 0, objectMsg)
require.NoError(t, err)
})
t.Run("handle full sync response, new protocol, not equal heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
chWithId := &consensusproto.RawRecordWithId{}
fullResponse := &consensusproto.LogFullSyncResponse{
Head: "h1",
Records: []*consensusproto.RawRecordWithId{chWithId},
}
logMessage := consensusproto.WrapFullResponse(fullResponse, chWithId)
objectMsg, _ := spacesyncproto.MarshallSyncMessage(logMessage, fx.spaceId, fx.aclId)
fx.aclMock.EXPECT().Id().AnyTimes().Return(fx.aclId)
fx.aclMock.EXPECT().Head().AnyTimes().Return(&list.AclRecord{Id: "h2"})
fx.syncProtocolMock.EXPECT().FullSyncResponse(ctx, fx.senderId, gomock.Any()).Return(nil)
req := &consensusproto.LogSyncMessage{}
fx.syncClientMock.EXPECT().CreateFullSyncRequest(fx.aclMock, "h1").Return(req, nil)
fx.syncClientMock.EXPECT().QueueRequest(fx.senderId, req).Return(nil)
err := fx.syncHandler.HandleMessage(ctx, fx.senderId, secureservice.NewSyncProtoVersion, objectMsg)
require.NoError(t, err)
})
t.Run("handle full sync response, new protocol, equal heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
chWithId := &consensusproto.RawRecordWithId{}
fullResponse := &consensusproto.LogFullSyncResponse{
Head: "h1",
Records: []*consensusproto.RawRecordWithId{chWithId},
}
logMessage := consensusproto.WrapFullResponse(fullResponse, chWithId)
objectMsg, _ := spacesyncproto.MarshallSyncMessage(logMessage, fx.spaceId, fx.aclId)
fx.aclMock.EXPECT().Id().AnyTimes().Return(fx.aclId)
fx.aclMock.EXPECT().Head().AnyTimes().Return(&list.AclRecord{Id: "h1"})
fx.syncProtocolMock.EXPECT().FullSyncResponse(ctx, fx.senderId, gomock.Any()).Return(nil)
err := fx.syncHandler.HandleMessage(ctx, fx.senderId, secureservice.NewSyncProtoVersion, objectMsg)
require.NoError(t, err)
})
}
func TestSyncAclHandler_HandleRequest(t *testing.T) {

View file

@ -143,11 +143,8 @@ func (s *syncTreeHandler) handleMessage(ctx context.Context, msg *treechangeprot
if err != nil {
return err
}
if protoVersion <= secureservice.ProtoVersion {
return nil
}
cnt := content.GetFullSyncResponse()
if slice.UnsortedEquals(cnt.Heads, s.objTree.Heads()) {
if protoVersion <= secureservice.ProtoVersion || slice.UnsortedEquals(cnt.Heads, s.objTree.Heads()) {
return nil
}
req, err := s.syncClient.CreateFullSyncRequest(s.objTree, cnt.Heads, cnt.SnapshotPath)