Skip to content

Commit 1778cf1

Browse files
(fix): update for tests to run 5 times faster (#145)
* update for tests to run 5 times faster * fix broken test * increase code coverage * fix queue test
1 parent 92fa240 commit 1778cf1

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

pkg/client/factory_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ func TestClientWithDecisionServiceAndEventProcessorInOptions(t *testing.T) {
111111
MaxQueueSize: 100,
112112
FlushInterval: 100,
113113
Q: event.NewInMemoryQueue(100),
114-
EventDispatcher: &MockDispatcher{},
114+
EventDispatcher: &MockDispatcher{Events:[]event.LogEvent{}},
115115
}
116116

117117
optimizelyClient, err := factory.Client(WithConfigManager(configManager), WithDecisionService(decisionService), WithEventProcessor(processor))

pkg/event/chan_queue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestChanQueue_Add_Size_Remove(t *testing.T) {
3333
q.Add(impression)
3434
q.Add(conversion)
3535

36-
time.Sleep(2000 * time.Millisecond)
36+
time.Sleep(100 * time.Millisecond)
3737

3838
items1 := q.Get(2)
3939

pkg/event/dispatcher.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929

3030
const maxRetries = 3
3131
const defaultQueueSize = 1000
32-
const sleepTime = 5 * time.Second
32+
const sleepTime = 1 * time.Second
3333

3434
var dispatcherLogger = logging.GetLogger("EventDispatcher")
3535

pkg/event/factory_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,17 @@ func TestCreateAndSendImpressionEvent(t *testing.T) {
121121

122122
impressionUserEvent := BuildTestImpressionEvent()
123123

124-
processor := NewBatchEventProcessor(WithBatchSize(10), WithQueueSize(100), WithFlushInterval(100))
124+
processor := NewBatchEventProcessor(WithBatchSize(10), WithQueueSize(100),
125+
WithFlushInterval(10),
126+
WithEventDispatcher(&MockDispatcher{Events:NewInMemoryQueue(100)}))
125127

126128
processor.Start(utils.NewCancelableExecutionCtx())
127129

128130
processor.ProcessEvent(impressionUserEvent)
129131

130132
assert.Equal(t, 1, processor.EventsCount())
131133

132-
time.Sleep(2000 * time.Millisecond)
134+
time.Sleep(100 * time.Millisecond)
133135

134136
assert.Equal(t, 0, processor.EventsCount())
135137
}
@@ -138,15 +140,16 @@ func TestCreateAndSendConversionEvent(t *testing.T) {
138140

139141
conversionUserEvent := BuildTestConversionEvent()
140142

141-
processor := NewBatchEventProcessor(WithFlushInterval(100))
143+
processor := NewBatchEventProcessor(WithFlushInterval(10),
144+
WithEventDispatcher(&MockDispatcher{Events:NewInMemoryQueue(100)}))
142145

143146
processor.Start(utils.NewCancelableExecutionCtx())
144147

145148
processor.ProcessEvent(conversionUserEvent)
146149

147150
assert.Equal(t, 1, processor.EventsCount())
148151

149-
time.Sleep(2000 * time.Millisecond)
152+
time.Sleep(100 * time.Millisecond)
150153

151154
assert.Equal(t, 0, processor.EventsCount())
152155
}

pkg/event/processor_test.go

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,15 @@ func (f *MockDispatcher) DispatchEvent(event LogEvent) (bool, error) {
4141
return true, nil
4242
}
4343

44+
func NewMockDispatcher(queueSize int, shouldFail bool) Dispatcher {
45+
return &MockDispatcher{Events:NewInMemoryQueue(queueSize), ShouldFail:shouldFail}
46+
}
47+
4448
func TestDefaultEventProcessor_ProcessImpression(t *testing.T) {
4549
exeCtx := utils.NewCancelableExecutionCtx()
46-
processor := NewBatchEventProcessor(WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}), WithFlushInterval(100))
50+
processor := NewBatchEventProcessor()
51+
processor.EventDispatcher = nil
52+
processor.Start(exeCtx)
4753
processor.Start(exeCtx)
4854

4955
impression := BuildTestImpressionEvent()
@@ -61,7 +67,7 @@ func TestDefaultEventProcessor_ProcessImpression(t *testing.T) {
6167

6268
func TestCustomEventProcessor_Create(t *testing.T) {
6369
exeCtx := utils.NewCancelableExecutionCtx()
64-
processor := NewBatchEventProcessor(WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}), WithQueueSize(10), WithFlushInterval(100))
70+
processor := NewBatchEventProcessor(WithEventDispatcher(NewMockDispatcher(100, false)), WithQueueSize(10), WithFlushInterval(100))
6571
processor.Start(exeCtx)
6672

6773
impression := BuildTestImpressionEvent()
@@ -80,7 +86,7 @@ func TestCustomEventProcessor_Create(t *testing.T) {
8086
func TestDefaultEventProcessor_LogEventNotification(t *testing.T) {
8187
exeCtx := utils.NewCancelableExecutionCtx()
8288
processor := NewBatchEventProcessor(WithFlushInterval(100), WithQueueSize(100),
83-
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}),
89+
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(NewMockDispatcher(100, false)),
8490
WithSDKKey("fakeSDKKey"))
8591

8692
var logEvent LogEvent
@@ -108,13 +114,12 @@ func TestDefaultEventProcessor_LogEventNotification(t *testing.T) {
108114
err := processor.RemoveOnEventDispatch(id)
109115

110116
assert.Nil(t, err)
111-
112117
}
113118

114119
func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
115120
exeCtx := utils.NewCancelableExecutionCtx()
116121
processor := NewBatchEventProcessor(WithFlushInterval(100), WithQueueSize(100),
117-
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
122+
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(NewMockDispatcher(100, false)))
118123
processor.Start(exeCtx)
119124

120125
impression := BuildTestImpressionEvent()
@@ -145,8 +150,8 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
145150

146151
func TestDefaultEventProcessor_QSizeMet(t *testing.T) {
147152
exeCtx := utils.NewCancelableExecutionCtx()
148-
processor := NewBatchEventProcessor(WithQueueSize(2), WithFlushInterval(100),
149-
WithQueue(NewInMemoryQueue(2)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
153+
processor := NewBatchEventProcessor(WithQueueSize(2), WithFlushInterval(1000),
154+
WithQueue(NewInMemoryQueue(2)), WithEventDispatcher(NewMockDispatcher(100, false)))
150155
processor.Start(exeCtx)
151156

152157
impression := BuildTestImpressionEvent()
@@ -217,7 +222,7 @@ func TestDefaultEventProcessor_FailedDispatch(t *testing.T) {
217222
func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
218223
exeCtx := utils.NewCancelableExecutionCtx()
219224
processor := NewBatchEventProcessor(WithQueueSize(100), WithQueue(NewInMemoryQueue(100)),
220-
WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
225+
WithEventDispatcher(NewMockDispatcher(100, false)))
221226
processor.Start(exeCtx)
222227

223228
impression := BuildTestImpressionEvent()
@@ -239,7 +244,7 @@ func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
239244
func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
240245
exeCtx := utils.NewCancelableExecutionCtx()
241246
processor := NewBatchEventProcessor(WithQueueSize(100), WithFlushInterval(100),
242-
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
247+
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(NewMockDispatcher(100, false)))
243248

244249
processor.Start(exeCtx)
245250

@@ -273,7 +278,7 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
273278
func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
274279
exeCtx := utils.NewCancelableExecutionCtx()
275280
processor := NewBatchEventProcessor(WithQueueSize(100), WithFlushInterval(100),
276-
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
281+
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(NewMockDispatcher(100, false)))
277282

278283
processor.Start(exeCtx)
279284

@@ -326,7 +331,7 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
326331
func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
327332
exeCtx := utils.NewCancelableExecutionCtx()
328333
processor := NewBatchEventProcessor(WithQueueSize(100), WithFlushInterval(100),
329-
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(&MockDispatcher{Events: NewInMemoryQueue(100)}))
334+
WithQueue(NewInMemoryQueue(100)), WithEventDispatcher(NewMockDispatcher(100, false)))
330335
processor.Start(exeCtx)
331336

332337
impression := BuildTestImpressionEvent()

0 commit comments

Comments
 (0)