Add head update tests
This commit is contained in:
parent
84356efa86
commit
9c41f8d51f
@ -62,6 +62,9 @@ func (s *syncHandler) handleHeadUpdate(
|
|||||||
|
|
||||||
// isEmptyUpdate is sent when the tree is brought up from cache
|
// isEmptyUpdate is sent when the tree is brought up from cache
|
||||||
if isEmptyUpdate {
|
if isEmptyUpdate {
|
||||||
|
if slice.UnsortedEquals(objTree.Heads(), update.Heads) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
// we need to sync in any case
|
// we need to sync in any case
|
||||||
fullRequest, err = s.syncClient.CreateFullSyncRequest(objTree, update.Heads, update.SnapshotPath, msg.TrackingId)
|
fullRequest, err = s.syncClient.CreateFullSyncRequest(objTree, update.Heads, update.SnapshotPath, msg.TrackingId)
|
||||||
return err
|
return err
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func (t treeContainer) Tree() tree.ObjectTree {
|
|||||||
return t.objTree
|
return t.objTree
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSyncHandler_HandleMessage(t *testing.T) {
|
func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
|
||||||
ctrl := gomock.NewController(t)
|
ctrl := gomock.NewController(t)
|
||||||
defer ctrl.Finish()
|
defer ctrl.Finish()
|
||||||
|
|
||||||
@ -33,29 +33,177 @@ func TestSyncHandler_HandleMessage(t *testing.T) {
|
|||||||
objectTreeMock := mock_tree.NewMockObjectTree(ctrl)
|
objectTreeMock := mock_tree.NewMockObjectTree(ctrl)
|
||||||
|
|
||||||
syncHandler := newSyncHandler(spaceId, cacheMock, syncClientMock)
|
syncHandler := newSyncHandler(spaceId, cacheMock, syncClientMock)
|
||||||
treeId := "treeId"
|
t.Run("head update non empty all heads added", func(t *testing.T) {
|
||||||
senderId := "senderId"
|
treeId := "treeId"
|
||||||
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
senderId := "senderId"
|
||||||
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
||||||
Heads: []string{"h1"},
|
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
||||||
Changes: []*treechangeproto.RawTreeChangeWithId{chWithId},
|
Heads: []string{"h1"},
|
||||||
SnapshotPath: []string{"h1"},
|
Changes: []*treechangeproto.RawTreeChangeWithId{chWithId},
|
||||||
}
|
SnapshotPath: []string{"h1"},
|
||||||
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
}
|
||||||
cacheMock.EXPECT().
|
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
||||||
GetTree(gomock.Any(), spaceId, treeId).
|
cacheMock.EXPECT().
|
||||||
Return(cache.TreeResult{
|
GetTree(gomock.Any(), spaceId, treeId).
|
||||||
Release: func() {},
|
Return(cache.TreeResult{
|
||||||
TreeContainer: treeContainer{objectTreeMock},
|
Release: func() {},
|
||||||
}, nil)
|
TreeContainer: treeContainer{objectTreeMock},
|
||||||
objectTreeMock.EXPECT().
|
}, nil)
|
||||||
Lock()
|
objectTreeMock.EXPECT().
|
||||||
objectTreeMock.EXPECT().
|
Lock()
|
||||||
Heads().Return([]string{"h2"})
|
objectTreeMock.EXPECT().
|
||||||
objectTreeMock.EXPECT().
|
Heads().
|
||||||
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})).
|
Return([]string{"h2"})
|
||||||
Return(tree.AddResult{}, nil)
|
objectTreeMock.EXPECT().
|
||||||
objectTreeMock.EXPECT().Unlock()
|
HasChanges(gomock.Eq([]string{"h1"})).
|
||||||
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
Return(false)
|
||||||
require.NoError(t, err)
|
objectTreeMock.EXPECT().
|
||||||
|
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})).
|
||||||
|
Return(tree.AddResult{}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h2", "h1"})
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
HasChanges(gomock.Eq([]string{"h1"})).
|
||||||
|
Return(true)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Unlock()
|
||||||
|
|
||||||
|
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("head update non empty heads not added", func(t *testing.T) {
|
||||||
|
treeId := "treeId"
|
||||||
|
senderId := "senderId"
|
||||||
|
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
||||||
|
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
||||||
|
Heads: []string{"h1"},
|
||||||
|
Changes: []*treechangeproto.RawTreeChangeWithId{chWithId},
|
||||||
|
SnapshotPath: []string{"h1"},
|
||||||
|
}
|
||||||
|
fullRequest := &spacesyncproto.ObjectSyncMessage{}
|
||||||
|
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
||||||
|
cacheMock.EXPECT().
|
||||||
|
GetTree(gomock.Any(), spaceId, treeId).
|
||||||
|
Return(cache.TreeResult{
|
||||||
|
Release: func() {},
|
||||||
|
TreeContainer: treeContainer{objectTreeMock},
|
||||||
|
}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Lock()
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h2"})
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
HasChanges(gomock.Eq([]string{"h1"})).
|
||||||
|
Return(false)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})).
|
||||||
|
Return(tree.AddResult{}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h2"})
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
HasChanges(gomock.Eq([]string{"h1"})).
|
||||||
|
Return(false)
|
||||||
|
syncClientMock.EXPECT().
|
||||||
|
CreateFullSyncRequest(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"}), gomock.Eq("")).
|
||||||
|
Return(fullRequest, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Unlock()
|
||||||
|
|
||||||
|
syncClientMock.EXPECT().SendAsync(gomock.Eq([]string{senderId}), gomock.Eq(fullRequest))
|
||||||
|
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("head update non empty equal heads", func(t *testing.T) {
|
||||||
|
treeId := "treeId"
|
||||||
|
senderId := "senderId"
|
||||||
|
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
||||||
|
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
||||||
|
Heads: []string{"h1"},
|
||||||
|
Changes: []*treechangeproto.RawTreeChangeWithId{chWithId},
|
||||||
|
SnapshotPath: []string{"h1"},
|
||||||
|
}
|
||||||
|
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
||||||
|
cacheMock.EXPECT().
|
||||||
|
GetTree(gomock.Any(), spaceId, treeId).
|
||||||
|
Return(cache.TreeResult{
|
||||||
|
Release: func() {},
|
||||||
|
TreeContainer: treeContainer{objectTreeMock},
|
||||||
|
}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Lock()
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h1"})
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Unlock()
|
||||||
|
|
||||||
|
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("head update empty", func(t *testing.T) {
|
||||||
|
treeId := "treeId"
|
||||||
|
senderId := "senderId"
|
||||||
|
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
||||||
|
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
||||||
|
Heads: []string{"h1"},
|
||||||
|
Changes: nil,
|
||||||
|
SnapshotPath: []string{"h1"},
|
||||||
|
}
|
||||||
|
fullRequest := &spacesyncproto.ObjectSyncMessage{}
|
||||||
|
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
||||||
|
cacheMock.EXPECT().
|
||||||
|
GetTree(gomock.Any(), spaceId, treeId).
|
||||||
|
Return(cache.TreeResult{
|
||||||
|
Release: func() {},
|
||||||
|
TreeContainer: treeContainer{objectTreeMock},
|
||||||
|
}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Lock()
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h2"})
|
||||||
|
syncClientMock.EXPECT().
|
||||||
|
CreateFullSyncRequest(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"}), gomock.Eq("")).
|
||||||
|
Return(fullRequest, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Unlock()
|
||||||
|
|
||||||
|
syncClientMock.EXPECT().SendAsync(gomock.Eq([]string{senderId}), gomock.Eq(fullRequest))
|
||||||
|
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("head update empty equal heads", func(t *testing.T) {
|
||||||
|
treeId := "treeId"
|
||||||
|
senderId := "senderId"
|
||||||
|
chWithId := &treechangeproto.RawTreeChangeWithId{}
|
||||||
|
headUpdate := &spacesyncproto.ObjectHeadUpdate{
|
||||||
|
Heads: []string{"h1"},
|
||||||
|
Changes: nil,
|
||||||
|
SnapshotPath: []string{"h1"},
|
||||||
|
}
|
||||||
|
msg := spacesyncproto.WrapHeadUpdate(headUpdate, chWithId, treeId, "")
|
||||||
|
cacheMock.EXPECT().
|
||||||
|
GetTree(gomock.Any(), spaceId, treeId).
|
||||||
|
Return(cache.TreeResult{
|
||||||
|
Release: func() {},
|
||||||
|
TreeContainer: treeContainer{objectTreeMock},
|
||||||
|
}, nil)
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Lock()
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Heads().
|
||||||
|
Return([]string{"h1"})
|
||||||
|
objectTreeMock.EXPECT().
|
||||||
|
Unlock()
|
||||||
|
|
||||||
|
err := syncHandler.HandleMessage(ctx, senderId, msg)
|
||||||
|
require.NoError(t, err)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user