From 098120da84a952e77c44730473d140d0b64d5f4e Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Tue, 11 Jul 2023 13:58:59 +0200 Subject: [PATCH] Update headsync tests --- commonspace/headsync/diffsyncer.go | 2 +- commonspace/headsync/diffsyncer_test.go | 2 +- commonspace/headsync/headsync.go | 4 +-- commonspace/headsync/headsync_test.go | 13 ++++++- .../object/acl/syncacl/aclsyncprotocol.go | 1 + commonspace/object/acl/syncacl/syncacl.go | 35 ++++++++++++------- commonspace/objectmanager/objectmanager.go | 2 +- commonspace/objecttreebuilder/treebuilder.go | 2 +- 8 files changed, 41 insertions(+), 20 deletions(-) diff --git a/commonspace/headsync/diffsyncer.go b/commonspace/headsync/diffsyncer.go index 98dfa74b..810ca5db 100644 --- a/commonspace/headsync/diffsyncer.go +++ b/commonspace/headsync/diffsyncer.go @@ -57,7 +57,7 @@ type diffSyncer struct { credentialProvider credentialprovider.CredentialProvider syncStatus syncstatus.StatusUpdater treeSyncer treemanager.TreeSyncer - syncAcl *syncacl.SyncAcl + syncAcl syncacl.SyncAcl } func (d *diffSyncer) Init() { diff --git a/commonspace/headsync/diffsyncer_test.go b/commonspace/headsync/diffsyncer_test.go index 6603dd98..fece9e63 100644 --- a/commonspace/headsync/diffsyncer_test.go +++ b/commonspace/headsync/diffsyncer_test.go @@ -14,8 +14,8 @@ import ( "github.com/anyproto/any-sync/commonspace/spacesyncproto" "github.com/anyproto/any-sync/consensus/consensusproto" "github.com/anyproto/any-sync/net/peer" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" "storj.io/drpc" ) diff --git a/commonspace/headsync/headsync.go b/commonspace/headsync/headsync.go index 1cc0c485..d7a1d74f 100644 --- a/commonspace/headsync/headsync.go +++ b/commonspace/headsync/headsync.go @@ -62,7 +62,7 @@ type headSync struct { credentialProvider credentialprovider.CredentialProvider syncStatus syncstatus.StatusService deletionState deletionstate.ObjectDeletionState - syncAcl *syncacl.SyncAcl + syncAcl syncacl.SyncAcl } func New() HeadSync { @@ -74,7 +74,7 @@ var createDiffSyncer = newDiffSyncer func (h *headSync) Init(a *app.App) (err error) { shared := a.MustComponent(spacestate.CName).(*spacestate.SpaceState) cfg := a.MustComponent("config").(config2.ConfigGetter) - h.syncAcl = a.MustComponent(syncacl.CName).(*syncacl.SyncAcl) + h.syncAcl = a.MustComponent(syncacl.CName).(syncacl.SyncAcl) h.spaceId = shared.SpaceId h.spaceIsDeleted = shared.SpaceIsDeleted h.syncPeriod = cfg.GetSpace().SyncPeriod diff --git a/commonspace/headsync/headsync_test.go b/commonspace/headsync/headsync_test.go index 4f14d084..1f37419a 100644 --- a/commonspace/headsync/headsync_test.go +++ b/commonspace/headsync/headsync_test.go @@ -11,6 +11,9 @@ import ( "github.com/anyproto/any-sync/commonspace/deletionstate" "github.com/anyproto/any-sync/commonspace/deletionstate/mock_deletionstate" "github.com/anyproto/any-sync/commonspace/headsync/mock_headsync" + "github.com/anyproto/any-sync/commonspace/object/acl/list" + "github.com/anyproto/any-sync/commonspace/object/acl/syncacl" + "github.com/anyproto/any-sync/commonspace/object/acl/syncacl/mock_syncacl" "github.com/anyproto/any-sync/commonspace/object/tree/treestorage/mock_treestorage" "github.com/anyproto/any-sync/commonspace/object/treemanager" "github.com/anyproto/any-sync/commonspace/object/treemanager/mock_treemanager" @@ -23,8 +26,8 @@ import ( "github.com/anyproto/any-sync/commonspace/syncstatus" "github.com/anyproto/any-sync/nodeconf" "github.com/anyproto/any-sync/nodeconf/mock_nodeconf" - "github.com/golang/mock/gomock" "github.com/stretchr/testify/require" + "go.uber.org/mock/gomock" "sync/atomic" "testing" ) @@ -60,6 +63,7 @@ type headSyncFixture struct { treeSyncerMock *mock_treemanager.MockTreeSyncer diffMock *mock_ldiff.MockDiff clientMock *mock_spacesyncproto.MockDRPCSpaceSyncClient + aclMock *mock_syncacl.MockSyncAcl headSync *headSync diffSyncer *diffSyncer } @@ -87,9 +91,13 @@ func newHeadSyncFixture(t *testing.T) *headSyncFixture { treeSyncerMock := mock_treemanager.NewMockTreeSyncer(ctrl) diffMock := mock_ldiff.NewMockDiff(ctrl) clientMock := mock_spacesyncproto.NewMockDRPCSpaceSyncClient(ctrl) + aclMock := mock_syncacl.NewMockSyncAcl(ctrl) + aclMock.EXPECT().Name().AnyTimes().Return(syncacl.CName) + aclMock.EXPECT().SetHeadUpdater(gomock.Any()).AnyTimes() hs := &headSync{} a := &app.App{} a.Register(spaceState). + Register(aclMock). Register(mockConfig{}). Register(configurationMock). Register(storageMock). @@ -115,6 +123,7 @@ func newHeadSyncFixture(t *testing.T) *headSyncFixture { treeSyncerMock: treeSyncerMock, diffMock: diffMock, clientMock: clientMock, + aclMock: aclMock, } } @@ -144,6 +153,8 @@ func TestHeadSync(t *testing.T) { treeMock := mock_treestorage.NewMockTreeStorage(fx.ctrl) fx.storageMock.EXPECT().StoredIds().Return(ids, nil) fx.storageMock.EXPECT().TreeStorage(ids[0]).Return(treeMock, nil) + fx.aclMock.EXPECT().Id().AnyTimes().Return("aclId") + fx.aclMock.EXPECT().Head().AnyTimes().Return(&list.AclRecord{Id: "headId"}) treeMock.EXPECT().Heads().Return([]string{"h1", "h2"}, nil) fx.diffMock.EXPECT().Set(ldiff.Element{ Id: "id1", diff --git a/commonspace/object/acl/syncacl/aclsyncprotocol.go b/commonspace/object/acl/syncacl/aclsyncprotocol.go index 68ebeeae..0b856b49 100644 --- a/commonspace/object/acl/syncacl/aclsyncprotocol.go +++ b/commonspace/object/acl/syncacl/aclsyncprotocol.go @@ -1,3 +1,4 @@ +//go:generate mockgen -destination mock_syncacl/mock_syncacl.go github.com/anyproto/any-sync/commonspace/object/acl/syncacl SyncAcl,SyncClient,RequestFactory,AclSyncProtocol package syncacl import ( diff --git a/commonspace/object/acl/syncacl/syncacl.go b/commonspace/object/acl/syncacl/syncacl.go index 98e61127..a612e5c5 100644 --- a/commonspace/object/acl/syncacl/syncacl.go +++ b/commonspace/object/acl/syncacl/syncacl.go @@ -3,6 +3,7 @@ package syncacl import ( "context" "errors" + "github.com/anyproto/any-sync/commonspace/object/syncobjectgetter" "github.com/anyproto/any-sync/accountservice" "github.com/anyproto/any-sync/app" @@ -25,15 +26,23 @@ var ( ErrSyncAclClosed = errors.New("sync acl is closed") ) -func New() *SyncAcl { - return &SyncAcl{} +type SyncAcl interface { + app.ComponentRunnable + list.AclList + syncobjectgetter.SyncObject + SetHeadUpdater(updater HeadUpdater) + SyncWithPeer(ctx context.Context, peerId string) (err error) +} + +func New() SyncAcl { + return &syncAcl{} } type HeadUpdater interface { UpdateHeads(id string, heads []string) } -type SyncAcl struct { +type syncAcl struct { list.AclList syncClient SyncClient syncHandler synchandler.SyncHandler @@ -41,23 +50,23 @@ type SyncAcl struct { isClosed bool } -func (s *SyncAcl) Run(ctx context.Context) (err error) { +func (s *syncAcl) Run(ctx context.Context) (err error) { return } -func (s *SyncAcl) HandleRequest(ctx context.Context, senderId string, request *spacesyncproto.ObjectSyncMessage) (response *spacesyncproto.ObjectSyncMessage, err error) { +func (s *syncAcl) HandleRequest(ctx context.Context, senderId string, request *spacesyncproto.ObjectSyncMessage) (response *spacesyncproto.ObjectSyncMessage, err error) { return s.syncHandler.HandleRequest(ctx, senderId, request) } -func (s *SyncAcl) SetHeadUpdater(updater HeadUpdater) { +func (s *syncAcl) SetHeadUpdater(updater HeadUpdater) { s.headUpdater = updater } -func (s *SyncAcl) HandleMessage(ctx context.Context, senderId string, request *spacesyncproto.ObjectSyncMessage) (err error) { +func (s *syncAcl) HandleMessage(ctx context.Context, senderId string, request *spacesyncproto.ObjectSyncMessage) (err error) { return s.syncHandler.HandleMessage(ctx, senderId, request) } -func (s *SyncAcl) Init(a *app.App) (err error) { +func (s *syncAcl) Init(a *app.App) (err error) { storage := a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorage) aclStorage, err := storage.AclStorage() if err != nil { @@ -77,7 +86,7 @@ func (s *SyncAcl) Init(a *app.App) (err error) { return err } -func (s *SyncAcl) AddRawRecord(rawRec *consensusproto.RawRecordWithId) (err error) { +func (s *syncAcl) AddRawRecord(rawRec *consensusproto.RawRecordWithId) (err error) { if s.isClosed { return ErrSyncAclClosed } @@ -91,7 +100,7 @@ func (s *SyncAcl) AddRawRecord(rawRec *consensusproto.RawRecordWithId) (err erro return } -func (s *SyncAcl) AddRawRecords(rawRecords []*consensusproto.RawRecordWithId) (err error) { +func (s *syncAcl) AddRawRecords(rawRecords []*consensusproto.RawRecordWithId) (err error) { if s.isClosed { return ErrSyncAclClosed } @@ -105,20 +114,20 @@ func (s *SyncAcl) AddRawRecords(rawRecords []*consensusproto.RawRecordWithId) (e return } -func (s *SyncAcl) SyncWithPeer(ctx context.Context, peerId string) (err error) { +func (s *syncAcl) SyncWithPeer(ctx context.Context, peerId string) (err error) { s.Lock() defer s.Unlock() headUpdate := s.syncClient.CreateHeadUpdate(s, nil) return s.syncClient.SendUpdate(peerId, headUpdate) } -func (s *SyncAcl) Close(ctx context.Context) (err error) { +func (s *syncAcl) Close(ctx context.Context) (err error) { s.Lock() defer s.Unlock() s.isClosed = true return } -func (s *SyncAcl) Name() (name string) { +func (s *syncAcl) Name() (name string) { return CName } diff --git a/commonspace/objectmanager/objectmanager.go b/commonspace/objectmanager/objectmanager.go index 49637818..3ebef3d5 100644 --- a/commonspace/objectmanager/objectmanager.go +++ b/commonspace/objectmanager/objectmanager.go @@ -41,7 +41,7 @@ func (o *objectManager) Init(a *app.App) (err error) { o.spaceId = state.SpaceId o.spaceIsClosed = state.SpaceIsClosed settingsObject := a.MustComponent(settings.CName).(settings.Settings).SettingsObject() - acl := a.MustComponent(syncacl.CName).(*syncacl.SyncAcl) + acl := a.MustComponent(syncacl.CName).(syncacl.SyncAcl) o.AddObject(settingsObject) o.AddObject(acl) return nil diff --git a/commonspace/objecttreebuilder/treebuilder.go b/commonspace/objecttreebuilder/treebuilder.go index 640416a7..8f3d09d8 100644 --- a/commonspace/objecttreebuilder/treebuilder.go +++ b/commonspace/objecttreebuilder/treebuilder.go @@ -82,7 +82,7 @@ func (t *treeBuilder) Init(a *app.App) (err error) { t.isClosed = state.SpaceIsClosed t.treesUsed = state.TreesUsed t.builder = state.TreeBuilderFunc - t.aclList = a.MustComponent(syncacl.CName).(*syncacl.SyncAcl) + t.aclList = a.MustComponent(syncacl.CName).(syncacl.SyncAcl) t.spaceStorage = a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorage) t.configuration = a.MustComponent(nodeconf.CName).(nodeconf.NodeConf) t.headsNotifiable = a.MustComponent(headsync.CName).(headsync.HeadSync)