Skip to content

Commit 2fa0959

Browse files
corrected tests and move wg.Add to StartTicker
1 parent 5cc7ef3 commit 2fa0959

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

optimizely/event/factory_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package event
33
import (
44
"context"
55
"math/rand"
6+
"sync"
67
"testing"
78
"time"
89

@@ -90,10 +91,11 @@ func BuildTestConversionEvent() UserEvent {
9091

9192
func TestCreateAndSendImpressionEvent(t *testing.T) {
9293
ctx := context.Background()
94+
var wg sync.WaitGroup
9395

9496
impressionUserEvent := BuildTestImpressionEvent()
9597

96-
processor := NewEventProcessor(ctx, 10, 100, 100)
98+
processor := NewEventProcessor(ctx, 10, 100, 100, &wg)
9799

98100
processor.ProcessEvent(impressionUserEvent)
99101

@@ -106,10 +108,11 @@ func TestCreateAndSendImpressionEvent(t *testing.T) {
106108

107109
func TestCreateAndSendConversionEvent(t *testing.T) {
108110
ctx := context.Background()
111+
var wg sync.WaitGroup
109112

110113
conversionUserEvent := BuildTestConversionEvent()
111114

112-
processor := NewEventProcessor(ctx, 10, 100, 100)
115+
processor := NewEventProcessor(ctx, 10, 100, 100, &wg)
113116

114117
processor.ProcessEvent(conversionUserEvent)
115118

optimizely/event/processor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ func NewEventProcessor(ctx context.Context, batchSize, queueSize int, flushInter
7070
p.BatchSize = batchSize
7171
}
7272

73-
p.wg.Add(1)
7473
p.StartTicker(ctx)
7574
return p
7675
}
@@ -107,6 +106,7 @@ func (p *QueueingEventProcessor) StartTicker(ctx context.Context) {
107106
return
108107
}
109108
p.Ticker = time.NewTicker(p.FlushInterval * time.Millisecond)
109+
p.wg.Add(1)
110110
go func() {
111111

112112
defer p.wg.Done()

optimizely/event/processor_test.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package event
22

33
import (
44
"context"
5+
"sync"
56
"testing"
67
"time"
78

@@ -10,8 +11,8 @@ import (
1011

1112
func TestDefaultEventProcessor_ProcessImpression(t *testing.T) {
1213
ctx := context.Background()
13-
14-
processor := NewEventProcessor(ctx, 10, 100, 100)
14+
var wg sync.WaitGroup
15+
processor := NewEventProcessor(ctx, 10, 100, 100, &wg)
1516

1617
impression := BuildTestImpressionEvent()
1718

@@ -36,11 +37,13 @@ func (f *MockDispatcher) DispatchEvent(event LogEvent, callback func(success boo
3637
}
3738

3839
func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
40+
var wg sync.WaitGroup
3941
processor := &QueueingEventProcessor{
4042
MaxQueueSize: 100,
4143
FlushInterval: 100,
4244
Q: NewInMemoryQueue(100),
4345
EventDispatcher: &MockDispatcher{},
46+
wg: &wg,
4447
}
4548
processor.BatchSize = 10
4649
processor.StartTicker(context.TODO())
@@ -72,11 +75,13 @@ func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {
7275

7376
func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
7477
ctx, cancelFn := context.WithCancel(context.Background())
78+
var wg sync.WaitGroup
7579
processor := &QueueingEventProcessor{
7680
MaxQueueSize: 100,
7781
FlushInterval: 30 * time.Second,
7882
Q: NewInMemoryQueue(100),
7983
EventDispatcher: &MockDispatcher{},
84+
wg: &wg,
8085
}
8186
processor.BatchSize = 10
8287
processor.StartTicker(ctx)
@@ -102,11 +107,13 @@ func TestBatchEventProcessor_FlushesOnClose(t *testing.T) {
102107
}
103108

104109
func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
110+
var wg sync.WaitGroup
105111
processor := &QueueingEventProcessor{
106112
MaxQueueSize: 100,
107113
FlushInterval: 100,
108114
Q: NewInMemoryQueue(100),
109115
EventDispatcher: &MockDispatcher{},
116+
wg: &wg,
110117
}
111118
processor.BatchSize = 10
112119
processor.StartTicker(context.TODO())
@@ -138,11 +145,13 @@ func TestDefaultEventProcessor_ProcessBatchRevisionMismatch(t *testing.T) {
138145
}
139146

140147
func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
148+
var wg sync.WaitGroup
141149
processor := &QueueingEventProcessor{
142150
MaxQueueSize: 100,
143151
FlushInterval: 100,
144152
Q: NewInMemoryQueue(100),
145153
EventDispatcher: &MockDispatcher{},
154+
wg: &wg,
146155
}
147156
processor.BatchSize = 10
148157
processor.StartTicker(context.TODO())
@@ -174,11 +183,13 @@ func TestDefaultEventProcessor_ProcessBatchProjectMismatch(t *testing.T) {
174183
}
175184

176185
func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
177-
processor:= &QueueingEventProcessor{
186+
var wg sync.WaitGroup
187+
processor := &QueueingEventProcessor{
178188
MaxQueueSize: 100,
179189
FlushInterval: 100,
180190
Q: NewChanQueue(100),
181191
EventDispatcher: &HTTPEventDispatcher{},
192+
wg: &wg,
182193
}
183194
processor.BatchSize = 10
184195
processor.StartTicker(context.TODO())
@@ -197,7 +208,8 @@ func TestChanQueueEventProcessor_ProcessImpression(t *testing.T) {
197208
}
198209

199210
func TestChanQueueEventProcessor_ProcessBatch(t *testing.T) {
200-
processor := &QueueingEventProcessor{MaxQueueSize: 100, FlushInterval: 100, Q: NewChanQueue(100), EventDispatcher: &MockDispatcher{}}
211+
var wg sync.WaitGroup
212+
processor := &QueueingEventProcessor{MaxQueueSize: 100, FlushInterval: 100, Q: NewChanQueue(100), EventDispatcher: &MockDispatcher{}, wg: &wg}
201213
processor.BatchSize = 10
202214
processor.StartTicker(context.TODO())
203215

0 commit comments

Comments
 (0)