Fix tests

This commit is contained in:
mcrakhman 2022-12-17 14:00:39 +01:00 committed by Mikhail Iudin
parent 791ecc56b0
commit a510fa21b7
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
3 changed files with 178 additions and 147 deletions

View File

@ -174,12 +174,11 @@ func Test_BuildSyncTree(t *testing.T) {
NewHeads: nil, NewHeads: nil,
RawChanges: changes, RawChanges: changes,
} }
expectedRes := tree.AddResult{ expectedRes := tree.AddResult{
Added: changes, Added: changes,
Mode: tree.Nothing, Mode: tree.Nothing,
} }
objTreeMock.EXPECT().AddRawChanges(gomock.Any(), gomock.Eq(changes)). objTreeMock.EXPECT().AddRawChanges(gomock.Any(), gomock.Eq(payload)).
Return(expectedRes, nil) Return(expectedRes, nil)
res, err := tr.AddRawChanges(ctx, payload) res, err := tr.AddRawChanges(ctx, payload)

View File

@ -33,11 +33,17 @@ func (t *testObjTreeMock) Unlock() {
t.m.Unlock() t.m.Unlock()
} }
func TestSyncHandler_HandleHeadUpdate(t *testing.T) { type syncHandlerFixture struct {
ctrl := gomock.NewController(t) ctrl *gomock.Controller
defer ctrl.Finish() syncClientMock *mock_synctree.MockSyncClient
objectTreeMock *testObjTreeMock
receiveQueueMock *mock_synctree.MockReceiveQueue
ctx := context.Background() syncHandler *syncTreeHandler
}
func newSyncHandlerFixture(t *testing.T) *syncHandlerFixture {
ctrl := gomock.NewController(t)
syncClientMock := mock_synctree.NewMockSyncClient(ctrl) syncClientMock := mock_synctree.NewMockSyncClient(ctrl)
objectTreeMock := newTestObjMock(mock_tree.NewMockObjectTree(ctrl)) objectTreeMock := newTestObjMock(mock_tree.NewMockObjectTree(ctrl))
receiveQueueMock := mock_synctree.NewMockReceiveQueue(ctrl) receiveQueueMock := mock_synctree.NewMockReceiveQueue(ctrl)
@ -47,9 +53,26 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
syncClient: syncClientMock, syncClient: syncClientMock,
queue: receiveQueueMock, queue: receiveQueueMock,
} }
return &syncHandlerFixture{
ctrl: ctrl,
syncClientMock: syncClientMock,
objectTreeMock: objectTreeMock,
receiveQueueMock: receiveQueueMock,
syncHandler: syncHandler,
}
}
func (fx *syncHandlerFixture) stop() {
fx.ctrl.Finish()
}
func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
ctx := context.Background()
log = zap.NewNop().Sugar() log = zap.NewNop().Sugar()
t.Run("head update non empty all heads added", func(t *testing.T) { t.Run("head update non empty all heads added", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -61,36 +84,29 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId) treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), ""). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
Return(false) fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
ID().AnyTimes().Return(treeId) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h2"}).Times(2)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().HasChanges(gomock.Eq([]string{"h1"})).Return(false)
Heads(). fx.objectTreeMock.EXPECT().
Return([]string{"h2"}).Times(2)
objectTreeMock.EXPECT().
HasChanges(gomock.Eq([]string{"h1"})).
Return(false)
objectTreeMock.EXPECT().
AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{ AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{
NewHeads: []string{"h1"}, NewHeads: []string{"h1"},
RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId}, RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId},
})). })).
Return(tree.AddResult{}, nil) Return(tree.AddResult{}, nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().Heads().Return([]string{"h2", "h1"})
Heads(). fx.objectTreeMock.EXPECT().HasChanges(gomock.Eq([]string{"h1"})).Return(true)
Return([]string{"h2", "h1"})
objectTreeMock.EXPECT().
HasChanges(gomock.Eq([]string{"h1"})).
Return(true)
receiveQueueMock.EXPECT().ClearQueue(senderId)
err := syncHandler.HandleMessage(ctx, senderId, objectMsg) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("head update non empty heads not added", func(t *testing.T) { t.Run("head update non empty heads not added", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -102,39 +118,32 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId) treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fullRequest := &treechangeproto.TreeSyncMessage{} fullRequest := &treechangeproto.TreeSyncMessage{}
receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), ""). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
Return(false) fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
ID().AnyTimes().Return(treeId) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h2"}).AnyTimes()
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().HasChanges(gomock.Eq([]string{"h1"})).Return(false)
Heads(). fx.objectTreeMock.EXPECT().
Return([]string{"h2"}).AnyTimes()
objectTreeMock.EXPECT().
HasChanges(gomock.Eq([]string{"h1"})).
Return(false)
objectTreeMock.EXPECT().
AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{ AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{
NewHeads: []string{"h1"}, NewHeads: []string{"h1"},
RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId}, RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId},
})). })).
Return(tree.AddResult{}, nil) Return(tree.AddResult{}, nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().HasChanges(gomock.Eq([]string{"h1"})).Return(false)
HasChanges(gomock.Eq([]string{"h1"})). fx.syncClientMock.EXPECT().
Return(false) CreateFullSyncRequest(gomock.Eq(fx.objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
syncClientMock.EXPECT().
CreateFullSyncRequest(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return(fullRequest, nil) Return(fullRequest, nil)
fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullRequest), gomock.Eq(""))
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullRequest), gomock.Eq("")) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
receiveQueueMock.EXPECT().ClearQueue(senderId) err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("head update non empty equal heads", func(t *testing.T) { t.Run("head update non empty equal heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -145,18 +154,20 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
} }
treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId) treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
ID().AnyTimes().Return(treeId) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h1"}).AnyTimes()
objectTreeMock.EXPECT().
Heads().
Return([]string{"h1"})
err := syncHandler.HandleMessage(ctx, senderId, objectMsg) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("head update empty", func(t *testing.T) { t.Run("head update empty", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -168,26 +179,24 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId) treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fullRequest := &treechangeproto.TreeSyncMessage{} fullRequest := &treechangeproto.TreeSyncMessage{}
fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
ID().AnyTimes().Return(treeId) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h2"}).AnyTimes()
objectTreeMock.EXPECT(). fx.syncClientMock.EXPECT().
Heads(). CreateFullSyncRequest(gomock.Eq(fx.objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return([]string{"h2"})
syncClientMock.EXPECT().
CreateFullSyncRequest(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return(fullRequest, nil) Return(fullRequest, nil)
objectTreeMock.EXPECT(). fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullRequest), gomock.Eq(""))
Heads().
Return([]string{"h2"})
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullRequest), gomock.Eq("")) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("head update empty equal heads", func(t *testing.T) { t.Run("head update empty equal heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -198,34 +207,25 @@ func TestSyncHandler_HandleHeadUpdate(t *testing.T) {
} }
treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId) treeMsg := treechangeproto.WrapHeadUpdate(headUpdate, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
ID().AnyTimes().Return(treeId) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h1"}).AnyTimes()
objectTreeMock.EXPECT().
Heads().
Return([]string{"h1"})
err := syncHandler.HandleMessage(ctx, senderId, objectMsg) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
} }
func TestSyncHandler_HandleFullSyncRequest(t *testing.T) { func TestSyncHandler_HandleFullSyncRequest(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background() ctx := context.Background()
syncClientMock := mock_synctree.NewMockSyncClient(ctrl)
objectTreeMock := newTestObjMock(mock_tree.NewMockObjectTree(ctrl))
receiveQueueMock := mock_synctree.NewMockReceiveQueue(ctrl)
syncHandler := &syncTreeHandler{
objTree: objectTreeMock,
syncClient: syncClientMock,
queue: receiveQueueMock,
}
log = zap.NewNop().Sugar() log = zap.NewNop().Sugar()
t.Run("full sync request with change", func(t *testing.T) { t.Run("full sync request with change", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -237,27 +237,32 @@ func TestSyncHandler_HandleFullSyncRequest(t *testing.T) {
treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId) treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fullResponse := &treechangeproto.TreeSyncMessage{} fullResponse := &treechangeproto.TreeSyncMessage{}
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
ID().AnyTimes().Return(treeId) fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
objectTreeMock.EXPECT().Header().Return(nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
Heads(). fx.objectTreeMock.EXPECT().Header().Return(nil)
Return([]string{"h2"}) fx.objectTreeMock.EXPECT().Heads().Return([]string{"h2"}).AnyTimes()
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().HasChanges(gomock.Eq([]string{"h1"})).Return(false)
HasChanges(gomock.Eq([]string{"h1"})). fx.objectTreeMock.EXPECT().
Return(false) AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{
objectTreeMock.EXPECT(). NewHeads: []string{"h1"},
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})). RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId},
})).
Return(tree.AddResult{}, nil) Return(tree.AddResult{}, nil)
syncClientMock.EXPECT(). fx.syncClientMock.EXPECT().
CreateFullSyncResponse(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})). CreateFullSyncResponse(gomock.Eq(fx.objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return(fullResponse, nil) Return(fullResponse, nil)
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq("")) fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq(""))
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("full sync request with change same heads", func(t *testing.T) { t.Run("full sync request with change same heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -269,21 +274,28 @@ func TestSyncHandler_HandleFullSyncRequest(t *testing.T) {
treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId) treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
fullResponse := &treechangeproto.TreeSyncMessage{} fullResponse := &treechangeproto.TreeSyncMessage{}
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
fx.objectTreeMock.EXPECT().
ID().AnyTimes().Return(treeId) ID().AnyTimes().Return(treeId)
objectTreeMock.EXPECT().Header().Return(nil) fx.objectTreeMock.EXPECT().Header().Return(nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
Heads(). Heads().
Return([]string{"h1"}) Return([]string{"h1"}).AnyTimes()
syncClientMock.EXPECT(). fx.syncClientMock.EXPECT().
CreateFullSyncResponse(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})). CreateFullSyncResponse(gomock.Eq(fx.objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return(fullResponse, nil) Return(fullResponse, nil)
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq("")) fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq(""))
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("full sync request without change but with reply id", func(t *testing.T) { t.Run("full sync request without change but with reply id", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
replyId := "replyId" replyId := "replyId"
@ -295,18 +307,25 @@ func TestSyncHandler_HandleFullSyncRequest(t *testing.T) {
treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId) treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId) objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId)
fullResponse := &treechangeproto.TreeSyncMessage{} fullResponse := &treechangeproto.TreeSyncMessage{}
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), replyId).Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, replyId, nil)
fx.objectTreeMock.EXPECT().
ID().AnyTimes().Return(treeId) ID().AnyTimes().Return(treeId)
objectTreeMock.EXPECT().Header().Return(nil) fx.objectTreeMock.EXPECT().Header().Return(nil)
syncClientMock.EXPECT(). fx.syncClientMock.EXPECT().
CreateFullSyncResponse(gomock.Eq(objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})). CreateFullSyncResponse(gomock.Eq(fx.objectTreeMock), gomock.Eq([]string{"h1"}), gomock.Eq([]string{"h1"})).
Return(fullResponse, nil) Return(fullResponse, nil)
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq(replyId)) fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Eq(fullResponse), gomock.Eq(replyId))
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("full sync request with add raw changes error", func(t *testing.T) { t.Run("full sync request with add raw changes error", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
chWithId := &treechangeproto.RawTreeChangeWithId{} chWithId := &treechangeproto.RawTreeChangeWithId{}
@ -317,41 +336,39 @@ func TestSyncHandler_HandleFullSyncRequest(t *testing.T) {
} }
treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId) treeMsg := treechangeproto.WrapFullRequest(fullSyncRequest, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "") objectMsg, _ := marshallTreeMessage(treeMsg, treeId, "")
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), "").Return(false)
fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, "", nil)
fx.objectTreeMock.EXPECT().
ID().AnyTimes().Return(treeId) ID().AnyTimes().Return(treeId)
objectTreeMock.EXPECT().Header().Return(nil) fx.objectTreeMock.EXPECT().Header().Return(nil)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
Heads(). Heads().
Return([]string{"h2"}) Return([]string{"h2"})
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
HasChanges(gomock.Eq([]string{"h1"})). HasChanges(gomock.Eq([]string{"h1"})).
Return(false) Return(false)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})). AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{
NewHeads: []string{"h1"},
RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId},
})).
Return(tree.AddResult{}, fmt.Errorf("")) Return(tree.AddResult{}, fmt.Errorf(""))
syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Any(), gomock.Eq("")) fx.syncClientMock.EXPECT().SendAsync(gomock.Eq(senderId), gomock.Any(), gomock.Eq(""))
err := syncHandler.HandleMessage(ctx, senderId, objectMsg)
fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.Error(t, err) require.Error(t, err)
}) })
} }
func TestSyncHandler_HandleFullSyncResponse(t *testing.T) { func TestSyncHandler_HandleFullSyncResponse(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
ctx := context.Background() ctx := context.Background()
syncClientMock := mock_synctree.NewMockSyncClient(ctrl)
objectTreeMock := newTestObjMock(mock_tree.NewMockObjectTree(ctrl))
receiveQueueMock := mock_synctree.NewMockReceiveQueue(ctrl)
syncHandler := &syncTreeHandler{
objTree: objectTreeMock,
syncClient: syncClientMock,
queue: receiveQueueMock,
}
log = zap.NewNop().Sugar() log = zap.NewNop().Sugar()
t.Run("full sync response with change", func(t *testing.T) { t.Run("full sync response with change", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
replyId := "replyId" replyId := "replyId"
@ -363,22 +380,31 @@ func TestSyncHandler_HandleFullSyncResponse(t *testing.T) {
} }
treeMsg := treechangeproto.WrapFullResponse(fullSyncResponse, chWithId) treeMsg := treechangeproto.WrapFullResponse(fullSyncResponse, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId) objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId)
objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId) fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), replyId).Return(false)
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, replyId, nil)
fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
fx.objectTreeMock.EXPECT().
Heads(). Heads().
Return([]string{"h2"}) Return([]string{"h2"}).AnyTimes()
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
HasChanges(gomock.Eq([]string{"h1"})). HasChanges(gomock.Eq([]string{"h1"})).
Return(false) Return(false)
objectTreeMock.EXPECT(). fx.objectTreeMock.EXPECT().
AddRawChanges(gomock.Any(), gomock.Eq([]*treechangeproto.RawTreeChangeWithId{chWithId})). AddRawChanges(gomock.Any(), gomock.Eq(tree.RawChangesPayload{
NewHeads: []string{"h1"},
RawChanges: []*treechangeproto.RawTreeChangeWithId{chWithId},
})).
Return(tree.AddResult{}, nil) Return(tree.AddResult{}, nil)
err := syncHandler.HandleMessage(ctx, senderId, objectMsg) fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
t.Run("full sync response with same heads", func(t *testing.T) { t.Run("full sync response with same heads", func(t *testing.T) {
fx := newSyncHandlerFixture(t)
defer fx.stop()
treeId := "treeId" treeId := "treeId"
senderId := "senderId" senderId := "senderId"
replyId := "replyId" replyId := "replyId"
@ -390,12 +416,16 @@ func TestSyncHandler_HandleFullSyncResponse(t *testing.T) {
} }
treeMsg := treechangeproto.WrapFullResponse(fullSyncResponse, chWithId) treeMsg := treechangeproto.WrapFullResponse(fullSyncResponse, chWithId)
objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId) objectMsg, _ := marshallTreeMessage(treeMsg, treeId, replyId)
objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId) fx.receiveQueueMock.EXPECT().AddMessage(senderId, gomock.Eq(treeMsg), replyId).Return(false)
objectTreeMock.EXPECT(). fx.receiveQueueMock.EXPECT().GetMessage(senderId).Return(treeMsg, replyId, nil)
Heads().
Return([]string{"h1"})
err := syncHandler.HandleMessage(ctx, senderId, objectMsg) fx.objectTreeMock.EXPECT().ID().AnyTimes().Return(treeId)
fx.objectTreeMock.EXPECT().
Heads().
Return([]string{"h1"}).AnyTimes()
fx.receiveQueueMock.EXPECT().ClearQueue(senderId)
err := fx.syncHandler.HandleMessage(ctx, senderId, objectMsg)
require.NoError(t, err) require.NoError(t, err)
}) })
} }

View File

@ -48,8 +48,10 @@ func (tb *treeBuilder) Build(theirHeads []string, newChanges []*Change) (*Tree,
return nil, err return nil, err
} }
// TODO: we can actually get this from database // TODO: we can actually get this from tree (though not sure, that there would always be
// getting old common snapshot // an invariant where the tree has the closest common snapshot of heads)
// so if optimization is critical we can change this to inject from tree directly
// but then we have to be sure that invariant stays true
oldBreakpoint, err := tb.findBreakpoint(heads, true) oldBreakpoint, err := tb.findBreakpoint(heads, true)
if err != nil { if err != nil {
// this should never error out, because otherwise we have broken data // this should never error out, because otherwise we have broken data