-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocal_test.go
163 lines (158 loc) · 5.63 KB
/
local_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
161
162
163
package synchronizer
import (
"errors"
"sync/atomic"
"testing"
"time"
"github.com/splitio/go-split-commons/v6/dtos"
hcMock "github.com/splitio/go-split-commons/v6/healthcheck/mocks"
"github.com/splitio/go-split-commons/v6/service"
"github.com/splitio/go-split-commons/v6/service/api"
httpMocks "github.com/splitio/go-split-commons/v6/service/mocks"
"github.com/splitio/go-split-commons/v6/storage/mocks"
"github.com/splitio/go-toolkit/v5/logging"
)
func TestLocalSyncAllError(t *testing.T) {
var splitFetchCalled int64
var notifyEventCalled int64
logger := logging.NewLogger(&logging.LoggerOptions{})
splitAPI := api.SplitAPI{
SplitFetcher: httpMocks.MockSplitFetcher{
FetchCall: func(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
atomic.AddInt64(&splitFetchCalled, 1)
if fetchOptions.ChangeNumber() != -1 {
t.Error("Wrong changenumber passed")
}
return nil, errors.New("Some")
},
},
}
splitMockStorage := mocks.MockSplitStorage{
ChangeNumberCall: func() (int64, error) { return -1, nil },
}
segmentMockStorage := mocks.MockSegmentStorage{}
telemetryMockStorage := mocks.MockTelemetryStorage{}
appMonitorMock := hcMock.MockApplicationMonitor{
NotifyEventCall: func(counterType int) {
atomic.AddInt64(¬ifyEventCalled, 1)
},
}
syncForTest := NewLocal(&LocalConfig{}, &splitAPI, splitMockStorage, segmentMockStorage, logger, telemetryMockStorage, appMonitorMock)
err := syncForTest.SyncAll()
if err == nil {
t.Error("It should return error")
}
if atomic.LoadInt64(&splitFetchCalled) != 1 {
t.Error("It should be called once")
}
if atomic.LoadInt64(¬ifyEventCalled) != 1 {
t.Errorf("It should be called once. Actual %d", notifyEventCalled)
}
}
func TestLocalSyncAllOk(t *testing.T) {
var splitFetchCalled int64
mockedSplit1 := dtos.SplitDTO{Name: "split1", Killed: false, Status: "ACTIVE", TrafficTypeName: "one"}
mockedSplit2 := dtos.SplitDTO{Name: "split2", Killed: true, Status: "ACTIVE", TrafficTypeName: "two"}
logger := logging.NewLogger(&logging.LoggerOptions{})
splitAPI := api.SplitAPI{
SplitFetcher: httpMocks.MockSplitFetcher{
FetchCall: func(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
atomic.AddInt64(&splitFetchCalled, 1)
if fetchOptions.ChangeNumber() != -1 {
t.Error("Wrong changenumber passed")
}
return &dtos.SplitChangesDTO{
Splits: []dtos.SplitDTO{mockedSplit1, mockedSplit2},
Since: 3,
Till: 3,
}, nil
},
},
}
splitMockStorage := mocks.MockSplitStorage{
ChangeNumberCall: func() (int64, error) { return -1, nil },
UpdateCall: func(toAdd []dtos.SplitDTO, toRemove []dtos.SplitDTO, changeNumber int64) {
if changeNumber != 3 {
t.Error("Wrong changenumber")
}
if len(toAdd) != 2 {
t.Error("Wrong length of passed splits")
}
},
}
var notifyEventCalled int64
segmentMockStorage := mocks.MockSegmentStorage{}
telemetryMockStorage := mocks.MockTelemetryStorage{
RecordSyncLatencyCall: func(resource int, latency time.Duration) {},
RecordSuccessfulSyncCall: func(resource int, when time.Time) {},
}
appMonitorMock := hcMock.MockApplicationMonitor{
NotifyEventCall: func(counterType int) {
atomic.AddInt64(¬ifyEventCalled, 1)
},
}
syncForTest := NewLocal(&LocalConfig{}, &splitAPI, splitMockStorage, segmentMockStorage, logger, telemetryMockStorage, appMonitorMock)
err := syncForTest.SyncAll()
if err != nil {
t.Error("It should not return error")
}
if splitFetchCalled != 1 {
t.Error("It should be called once")
}
if atomic.LoadInt64(¬ifyEventCalled) != 1 {
t.Errorf("It should be called once. Actual %d", notifyEventCalled)
}
}
func TestLocalPeriodicFetching(t *testing.T) {
var splitFetchCalled int64
mockedSplit1 := dtos.SplitDTO{Name: "split1", Killed: false, Status: "ACTIVE", TrafficTypeName: "one"}
mockedSplit2 := dtos.SplitDTO{Name: "split2", Killed: true, Status: "ACTIVE", TrafficTypeName: "two"}
logger := logging.NewLogger(&logging.LoggerOptions{})
splitAPI := api.SplitAPI{
SplitFetcher: httpMocks.MockSplitFetcher{
FetchCall: func(fetchOptions *service.FlagRequestParams) (*dtos.SplitChangesDTO, error) {
atomic.AddInt64(&splitFetchCalled, 1)
if fetchOptions.ChangeNumber() != -1 {
t.Error("Wrong changenumber passed")
}
return &dtos.SplitChangesDTO{
Splits: []dtos.SplitDTO{mockedSplit1, mockedSplit2},
Since: 3,
Till: 3,
}, nil
},
},
}
splitMockStorage := mocks.MockSplitStorage{
ChangeNumberCall: func() (int64, error) { return -1, nil },
UpdateCall: func(toAdd []dtos.SplitDTO, toRemove []dtos.SplitDTO, changeNumber int64) {
if changeNumber != 3 {
t.Error("Wrong changenumber")
}
if len(toAdd) != 2 {
t.Error("Wrong length of passed splits")
}
},
}
segmentMockStorage := mocks.MockSegmentStorage{}
telemetryMockStorage := mocks.MockTelemetryStorage{
RecordSyncLatencyCall: func(resource int, latency time.Duration) {},
RecordSuccessfulSyncCall: func(resource int, when time.Time) {},
}
var notifyEventCalled int64
appMonitorMock := hcMock.MockApplicationMonitor{
NotifyEventCall: func(counterType int) {
atomic.AddInt64(¬ifyEventCalled, 1)
},
}
syncForTest := NewLocal(&LocalConfig{RefreshEnabled: true, SplitPeriod: 1}, &splitAPI, splitMockStorage, segmentMockStorage, logger, telemetryMockStorage, appMonitorMock)
syncForTest.StartPeriodicFetching()
time.Sleep(time.Millisecond * 1500)
if atomic.LoadInt64(&splitFetchCalled) != 1 {
t.Error("It should be called once", splitFetchCalled)
}
syncForTest.StopPeriodicFetching()
if atomic.LoadInt64(¬ifyEventCalled) != 1 {
t.Errorf("It should be called once. Actual %d", notifyEventCalled)
}
}