Add sync protocol tests for empty data tree

This commit is contained in:
mcrakhman 2023-04-27 18:43:04 +02:00
parent da0e4f148a
commit 6676480e1d
No known key found for this signature in database
GPG Key ID: DED12CFEF5B8396B
3 changed files with 57 additions and 23 deletions

View File

@ -8,6 +8,7 @@ import (
"github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto" "github.com/anytypeio/any-sync/commonspace/object/tree/treechangeproto"
"github.com/anytypeio/any-sync/commonspace/object/tree/treestorage" "github.com/anytypeio/any-sync/commonspace/object/tree/treestorage"
"github.com/anytypeio/any-sync/util/slice" "github.com/anytypeio/any-sync/util/slice"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"math/rand" "math/rand"
"testing" "testing"
@ -30,7 +31,8 @@ func TestEmptyClientGetsFullHistory(t *testing.T) {
"peer1": []string{"peer2"}, "peer1": []string{"peer2"},
"peer2": []string{"peer1"}, "peer2": []string{"peer1"},
}, },
emptyTrees: []string{"peer2"}, emptyTrees: []string{"peer2"},
treeBuilder: objecttree.BuildTestableTree,
} }
fx := newProtocolFixture(t, spaceId, deps) fx := newProtocolFixture(t, spaceId, deps)
fx.run(t) fx.run(t)
@ -60,6 +62,11 @@ func TestEmptyClientGetsFullHistory(t *testing.T) {
} }
func TestTreeFuzzyMerge(t *testing.T) { func TestTreeFuzzyMerge(t *testing.T) {
testTreeFuzzyMerge(t, true)
testTreeFuzzyMerge(t, false)
}
func testTreeFuzzyMerge(t *testing.T, withData bool) {
var ( var (
rnd = rand.New(rand.NewSource(time.Now().Unix())) rnd = rand.New(rand.NewSource(time.Now().Unix()))
levels = 20 levels = 20
@ -67,20 +74,20 @@ func TestTreeFuzzyMerge(t *testing.T) {
rounds = 10 rounds = 10
) )
for i := 0; i < rounds; i++ { for i := 0; i < rounds; i++ {
testTreeMerge(t, levels, perLevel, func() bool { testTreeMerge(t, levels, perLevel, withData, func() bool {
return true return true
}) })
testTreeMerge(t, levels, perLevel, func() bool { testTreeMerge(t, levels, perLevel, withData, func() bool {
return false return false
}) })
testTreeMerge(t, levels, perLevel, func() bool { testTreeMerge(t, levels, perLevel, withData, func() bool {
return rnd.Intn(10) > 8 return rnd.Intn(10) > 8
}) })
levels += 2 levels += 2
} }
} }
func testTreeMerge(t *testing.T, levels, perlevel int, isSnapshot func() bool) { func testTreeMerge(t *testing.T, levels, perLevel int, hasData bool, isSnapshot func() bool) {
treeId := "treeId" treeId := "treeId"
spaceId := "spaceId" spaceId := "spaceId"
keys, err := accountdata.NewRandom() keys, err := accountdata.NewRandom()
@ -88,15 +95,20 @@ func testTreeMerge(t *testing.T, levels, perlevel int, isSnapshot func() bool) {
aclList, err := list.NewTestDerivedAcl(spaceId, keys) aclList, err := list.NewTestDerivedAcl(spaceId, keys)
storage := createStorage(treeId, aclList) storage := createStorage(treeId, aclList)
changeCreator := objecttree.NewMockChangeCreator() changeCreator := objecttree.NewMockChangeCreator()
builder := objecttree.BuildTestableTree
if hasData {
builder = objecttree.BuildEmptyDataTestableTree
}
params := genParams{ params := genParams{
prefix: "peer1", prefix: "peer1",
aclId: aclList.Id(), aclId: aclList.Id(),
startIdx: 0, startIdx: 0,
levels: levels, levels: levels,
perLevel: perlevel, perLevel: perLevel,
snapshotId: treeId, snapshotId: treeId,
prevHeads: []string{treeId}, prevHeads: []string{treeId},
isSnapshot: isSnapshot, isSnapshot: isSnapshot,
hasData: hasData,
} }
// generating initial tree // generating initial tree
initialRes := genChanges(changeCreator, params) initialRes := genChanges(changeCreator, params)
@ -110,7 +122,8 @@ func testTreeMerge(t *testing.T, levels, perlevel int, isSnapshot func() bool) {
"peer2": []string{"node1"}, "peer2": []string{"node1"},
"node1": []string{"peer1", "peer2"}, "node1": []string{"peer1", "peer2"},
}, },
emptyTrees: []string{"peer2", "node1"}, emptyTrees: []string{"peer2", "node1"},
treeBuilder: builder,
} }
fx := newProtocolFixture(t, spaceId, deps) fx := newProtocolFixture(t, spaceId, deps)
fx.run(t) fx.run(t)
@ -127,11 +140,12 @@ func testTreeMerge(t *testing.T, levels, perlevel int, isSnapshot func() bool) {
aclId: aclList.Id(), aclId: aclList.Id(),
startIdx: levels, startIdx: levels,
levels: levels, levels: levels,
perLevel: perlevel, perLevel: perLevel,
snapshotId: initialRes.snapshotId, snapshotId: initialRes.snapshotId,
prevHeads: initialRes.heads, prevHeads: initialRes.heads,
isSnapshot: isSnapshot, isSnapshot: isSnapshot,
hasData: hasData,
} }
// generating different additions to the tree for different peers // generating different additions to the tree for different peers
peer1Res := genChanges(changeCreator, params) peer1Res := genChanges(changeCreator, params)
@ -156,4 +170,16 @@ func testTreeMerge(t *testing.T, levels, perlevel int, isSnapshot func() bool) {
firstStorage := firstTree.Storage().(*treestorage.InMemoryTreeStorage) firstStorage := firstTree.Storage().(*treestorage.InMemoryTreeStorage)
secondStorage := secondTree.Storage().(*treestorage.InMemoryTreeStorage) secondStorage := secondTree.Storage().(*treestorage.InMemoryTreeStorage)
require.True(t, firstStorage.Equal(secondStorage)) require.True(t, firstStorage.Equal(secondStorage))
if hasData {
for _, ch := range secondStorage.Changes {
if ch.Id == treeId {
continue
}
unmarshallRaw := &treechangeproto.RawTreeChange{}
proto.Unmarshal(ch.RawChange, unmarshallRaw)
treeCh := &treechangeproto.TreeChange{}
proto.Unmarshal(unmarshallRaw.Payload, treeCh)
require.Equal(t, ch.Id, string(treeCh.ChangesData))
}
}
} }

View File

@ -90,6 +90,7 @@ type testSyncHandler struct {
aclList list.AclList aclList list.AclList
log *messageLog log *messageLog
syncClient objectsync.SyncClient syncClient objectsync.SyncClient
builder objecttree.BuildObjectTreeFunc
} }
// createSyncHandler creates a sync handler when a tree is already created // createSyncHandler creates a sync handler when a tree is already created
@ -105,7 +106,7 @@ func createSyncHandler(peerId, spaceId string, objTree objecttree.ObjectTree, lo
} }
// createEmptySyncHandler creates a sync handler when the tree will be provided later (this emulates the situation when we have no tree) // createEmptySyncHandler creates a sync handler when the tree will be provided later (this emulates the situation when we have no tree)
func createEmptySyncHandler(peerId, spaceId string, aclList list.AclList, log *messageLog) *testSyncHandler { func createEmptySyncHandler(peerId, spaceId string, builder objecttree.BuildObjectTreeFunc, aclList list.AclList, log *messageLog) *testSyncHandler {
factory := objectsync.NewRequestFactory() factory := objectsync.NewRequestFactory()
syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory) syncClient := objectsync.NewSyncClient(spaceId, newTestMessagePool(peerId, log), factory)
@ -116,6 +117,7 @@ func createEmptySyncHandler(peerId, spaceId string, aclList list.AclList, log *m
aclList: aclList, aclList: aclList,
log: log, log: log,
syncClient: syncClient, syncClient: syncClient,
builder: builder,
} }
} }
@ -148,7 +150,7 @@ func (h *testSyncHandler) HandleMessage(ctx context.Context, senderId string, re
} }
fullSyncResponse := unmarshalled.Content.GetFullSyncResponse() fullSyncResponse := unmarshalled.Content.GetFullSyncResponse()
treeStorage, _ := treestorage.NewInMemoryTreeStorage(unmarshalled.RootChange, []string{unmarshalled.RootChange.Id}, nil) treeStorage, _ := treestorage.NewInMemoryTreeStorage(unmarshalled.RootChange, []string{unmarshalled.RootChange.Id}, nil)
tree, err := createTestTree(h.aclList, treeStorage) tree, err := h.builder(treeStorage, h.aclList)
if err != nil { if err != nil {
return return
} }
@ -304,6 +306,7 @@ type fixtureDeps struct {
initStorage *treestorage.InMemoryTreeStorage initStorage *treestorage.InMemoryTreeStorage
connectionMap map[string][]string connectionMap map[string][]string
emptyTrees []string emptyTrees []string
treeBuilder objecttree.BuildObjectTreeFunc
} }
// protocolFixture is the test environment for sync protocol tests // protocolFixture is the test environment for sync protocol tests
@ -326,10 +329,10 @@ func newProtocolFixture(t *testing.T, spaceId string, deps fixtureDeps) *protoco
for peerId := range deps.connectionMap { for peerId := range deps.connectionMap {
var handler *testSyncHandler var handler *testSyncHandler
if slices.Contains(deps.emptyTrees, peerId) { if slices.Contains(deps.emptyTrees, peerId) {
handler = createEmptySyncHandler(peerId, spaceId, deps.aclList, log) handler = createEmptySyncHandler(peerId, spaceId, deps.treeBuilder, deps.aclList, log)
} else { } else {
stCopy := deps.initStorage.Copy() stCopy := deps.initStorage.Copy()
testTree, err := createTestTree(deps.aclList, stCopy) testTree, err := deps.treeBuilder(stCopy, deps.aclList)
require.NoError(t, err) require.NoError(t, err)
handler = createSyncHandler(peerId, spaceId, testTree, log) handler = createSyncHandler(peerId, spaceId, testTree, log)
} }
@ -373,6 +376,7 @@ type genParams struct {
snapshotId string snapshotId string
prevHeads []string prevHeads []string
isSnapshot func() bool isSnapshot func() bool
hasData bool
} }
// genResult is the result of genChanges // genResult is the result of genChanges
@ -399,7 +403,11 @@ func genChanges(creator *objecttree.MockChangeCreator, params genParams) (res ge
) )
newChange := func(isSnapshot bool, idx int, prevIds []string) string { newChange := func(isSnapshot bool, idx int, prevIds []string) string {
newId := fmt.Sprintf("%s.%d.%d", params.prefix, params.startIdx+i, idx) newId := fmt.Sprintf("%s.%d.%d", params.prefix, params.startIdx+i, idx)
newCh := creator.CreateRaw(newId, params.aclId, snapshotId, isSnapshot, prevIds...) var data []byte
if params.hasData {
data = []byte(newId)
}
newCh := creator.CreateRawWithData(newId, params.aclId, snapshotId, isSnapshot, data, prevIds...)
res.changes = append(res.changes, newCh) res.changes = append(res.changes, newCh)
return newId return newId
} }

View File

@ -12,7 +12,7 @@ type InMemoryTreeStorage struct {
id string id string
root *treechangeproto.RawTreeChangeWithId root *treechangeproto.RawTreeChangeWithId
heads []string heads []string
changes map[string]*treechangeproto.RawTreeChangeWithId Changes map[string]*treechangeproto.RawTreeChangeWithId
sync.RWMutex sync.RWMutex
} }
@ -22,7 +22,7 @@ func (t *InMemoryTreeStorage) TransactionAdd(changes []*treechangeproto.RawTreeC
defer t.RUnlock() defer t.RUnlock()
for _, ch := range changes { for _, ch := range changes {
t.changes[ch.Id] = ch t.Changes[ch.Id] = ch
} }
t.heads = append(t.heads[:0], heads...) t.heads = append(t.heads[:0], heads...)
return nil return nil
@ -42,13 +42,13 @@ func NewInMemoryTreeStorage(
id: root.Id, id: root.Id,
root: root, root: root,
heads: append([]string(nil), heads...), heads: append([]string(nil), heads...),
changes: allChanges, Changes: allChanges,
RWMutex: sync.RWMutex{}, RWMutex: sync.RWMutex{},
}, nil }, nil
} }
func (t *InMemoryTreeStorage) HasChange(ctx context.Context, id string) (bool, error) { func (t *InMemoryTreeStorage) HasChange(ctx context.Context, id string) (bool, error) {
_, exists := t.changes[id] _, exists := t.Changes[id]
return exists, nil return exists, nil
} }
@ -81,14 +81,14 @@ func (t *InMemoryTreeStorage) AddRawChange(change *treechangeproto.RawTreeChange
t.Lock() t.Lock()
defer t.Unlock() defer t.Unlock()
// TODO: better to do deep copy // TODO: better to do deep copy
t.changes[change.Id] = change t.Changes[change.Id] = change
return nil return nil
} }
func (t *InMemoryTreeStorage) GetRawChange(ctx context.Context, changeId string) (*treechangeproto.RawTreeChangeWithId, error) { func (t *InMemoryTreeStorage) GetRawChange(ctx context.Context, changeId string) (*treechangeproto.RawTreeChangeWithId, error) {
t.RLock() t.RLock()
defer t.RUnlock() defer t.RUnlock()
if res, exists := t.changes[changeId]; exists { if res, exists := t.Changes[changeId]; exists {
return res, nil return res, nil
} }
return nil, fmt.Errorf("could not get change with id: %s", changeId) return nil, fmt.Errorf("could not get change with id: %s", changeId)
@ -100,7 +100,7 @@ func (t *InMemoryTreeStorage) Delete() error {
func (t *InMemoryTreeStorage) Copy() *InMemoryTreeStorage { func (t *InMemoryTreeStorage) Copy() *InMemoryTreeStorage {
var changes []*treechangeproto.RawTreeChangeWithId var changes []*treechangeproto.RawTreeChangeWithId
for _, ch := range t.changes { for _, ch := range t.Changes {
changes = append(changes, ch) changes = append(changes, ch)
} }
other, _ := NewInMemoryTreeStorage(t.root, t.heads, changes) other, _ := NewInMemoryTreeStorage(t.root, t.heads, changes)
@ -111,11 +111,11 @@ func (t *InMemoryTreeStorage) Equal(other *InMemoryTreeStorage) bool {
if !slice.UnsortedEquals(t.heads, other.heads) { if !slice.UnsortedEquals(t.heads, other.heads) {
return false return false
} }
if len(t.changes) != len(other.changes) { if len(t.Changes) != len(other.Changes) {
return false return false
} }
for k, v := range t.changes { for k, v := range t.Changes {
if otherV, exists := other.changes[k]; exists { if otherV, exists := other.Changes[k]; exists {
if otherV.Id == v.Id { if otherV.Id == v.Id {
continue continue
} }