-
Notifications
You must be signed in to change notification settings - Fork 535
/
collector_state_manager_test.go
160 lines (153 loc) · 6.94 KB
/
collector_state_manager_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package api
import (
"testing"
"time"
"github.com/apache/incubator-devlake/core/errors"
"github.com/apache/incubator-devlake/core/models"
"github.com/apache/incubator-devlake/helpers/unithelper"
mockcontext "github.com/apache/incubator-devlake/mocks/core/context"
mockdal "github.com/apache/incubator-devlake/mocks/core/dal"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
)
func TestCollectorStateManager(t *testing.T) {
time0 := errors.Must1(time.Parse(time.RFC3339, "2020-01-01T00:00:00Z"))
time1 := errors.Must1(time.Parse(time.RFC3339, "2021-01-01T00:00:00Z"))
time2 := errors.Must1(time.Parse(time.RFC3339, "2022-01-01T00:00:00Z"))
for _, tc := range []struct {
name string
state *models.CollectorLatestState
syncPolicy *models.SyncPolicy
expectedIsIncremental bool
expectedSince *time.Time
expectedNewStateTimeAfter *time.Time
}{
{
name: "syncPolicy has no timeAfter - First run",
state: &models.CollectorLatestState{LatestSuccessStart: nil},
syncPolicy: &models.SyncPolicy{TimeAfter: nil},
expectedIsIncremental: false,
expectedSince: nil,
expectedNewStateTimeAfter: nil,
},
{
name: "syncPolicy has no timeAfter - Second run",
state: &models.CollectorLatestState{LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TimeAfter: nil},
expectedIsIncremental: true,
expectedSince: &time1,
expectedNewStateTimeAfter: nil,
},
{
name: "syncPolicy has no timeAfter - Third run with timeAfter specified",
state: &models.CollectorLatestState{LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TimeAfter: &time1},
expectedIsIncremental: true,
expectedSince: &time1,
expectedNewStateTimeAfter: nil,
},
{
name: "syncPolicy has timeAfter - First run",
state: &models.CollectorLatestState{LatestSuccessStart: nil},
syncPolicy: &models.SyncPolicy{TimeAfter: &time1},
expectedIsIncremental: false,
expectedSince: &time1,
expectedNewStateTimeAfter: &time1,
},
{
name: "syncPolicy has timeAfter - Second run with a later timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time2},
syncPolicy: &models.SyncPolicy{TimeAfter: &time2},
expectedIsIncremental: true,
expectedSince: &time2,
expectedNewStateTimeAfter: &time1,
},
{
name: "syncPolicy has timeAfter - Third run with a earlier timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TimeAfter: &time0},
expectedIsIncremental: false,
expectedSince: &time0,
expectedNewStateTimeAfter: &time0,
},
{
name: "syncPolicy has timeAfter - Fourth run with a same timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time2},
syncPolicy: &models.SyncPolicy{TimeAfter: &time1},
expectedIsIncremental: true,
expectedSince: &time2,
expectedNewStateTimeAfter: &time1,
},
{
name: "Full sync - with timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TriggerSyncPolicy: models.TriggerSyncPolicy{FullSync: true}},
expectedIsIncremental: false,
expectedSince: &time1,
expectedNewStateTimeAfter: &time1,
},
{
name: "Full sync - with newer timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TimeAfter: &time2, TriggerSyncPolicy: models.TriggerSyncPolicy{FullSync: true}},
expectedIsIncremental: false,
expectedSince: &time2,
expectedNewStateTimeAfter: &time2,
},
{
name: "Full sync - with older timeAfter",
state: &models.CollectorLatestState{TimeAfter: &time1, LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TimeAfter: &time0, TriggerSyncPolicy: models.TriggerSyncPolicy{FullSync: true}},
expectedIsIncremental: false,
expectedSince: &time0,
expectedNewStateTimeAfter: &time0,
},
{
name: "Full sync - without timeAfter",
state: &models.CollectorLatestState{TimeAfter: nil, LatestSuccessStart: &time1},
syncPolicy: &models.SyncPolicy{TriggerSyncPolicy: models.TriggerSyncPolicy{FullSync: true}},
expectedIsIncremental: false,
expectedSince: nil,
expectedNewStateTimeAfter: nil,
},
} {
started := time.Now()
t.Run(tc.name, func(t *testing.T) {
mockBasicRes := newMockBasicRes(tc.state)
stateManager, err := NewCollectorStateManager(mockBasicRes, tc.syncPolicy, "table", "params")
assert.Nil(t, err)
assert.Equal(t, tc.expectedSince, stateManager.since)
assert.Equal(t, tc.expectedIsIncremental, stateManager.isIncremental)
assert.Nil(t, stateManager.Close())
assert.Equal(t, tc.expectedNewStateTimeAfter, stateManager.state.TimeAfter)
// LatestSuccessStart should be updated
assert.GreaterOrEqual(t, stateManager.state.LatestSuccessStart.Unix(), started.Unix())
// First and update should both be called once
mockBasicRes.AssertExpectations(t)
})
}
}
func newMockBasicRes(state *models.CollectorLatestState) *mockcontext.BasicRes {
// Refresh Global Variables and set the sql mock
return unithelper.DummyBasicRes(func(mockDal *mockdal.Dal) {
mockDal.On("First", mock.Anything, mock.Anything).Run(func(args mock.Arguments) {
dst := args.Get(0).(*models.CollectorLatestState)
*dst = *state
}).Return(nil).Once()
mockDal.On("Update", mock.Anything, mock.Anything).Return(nil).Once()
})
}