Skip to content

Commit fbbbba3

Browse files
author
Michael Ng
authored
refac(event): Return success value instead of using callback in dispatcher interface. (#93)
1 parent 52f864a commit fbbbba3

File tree

4 files changed

+18
-18
lines changed

4 files changed

+18
-18
lines changed

optimizely/client/factory_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type MockDispatcher struct {
3030
Events []event.LogEvent
3131
}
3232

33-
func (f *MockDispatcher) DispatchEvent(event event.LogEvent, callback func(success bool)) {
33+
func (f *MockDispatcher) DispatchEvent(event event.LogEvent) (bool, error) {
3434
f.Events = append(f.Events, event)
35-
callback(true)
35+
return true, nil
3636
}
3737

3838
func TestFactoryClientReturnsDefaultClient(t *testing.T) {

optimizely/event/dispatcher.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ const jsonContentType = "application/json"
3030

3131
// Dispatcher dispatches events
3232
type Dispatcher interface {
33-
DispatchEvent(event LogEvent, callback func(success bool))
33+
DispatchEvent(event LogEvent) (bool, error)
3434
}
3535

3636
// HTTPEventDispatcher is the HTTP implementation of the Dispatcher interface
@@ -40,7 +40,7 @@ type HTTPEventDispatcher struct {
4040
var dispatcherLogger = logging.GetLogger("EventDispatcher")
4141

4242
// DispatchEvent dispatches event with callback
43-
func (*HTTPEventDispatcher) DispatchEvent(event LogEvent, callback func(bool)) {
43+
func (*HTTPEventDispatcher) DispatchEvent(event LogEvent) (bool, error) {
4444

4545
jsonValue, _ := json.Marshal(event.Event)
4646
resp, err := http.Post(event.EndPoint, jsonContentType, bytes.NewBuffer(jsonValue))
@@ -54,10 +54,9 @@ func (*HTTPEventDispatcher) DispatchEvent(event LogEvent, callback func(bool)) {
5454
if resp.StatusCode == 204 {
5555
success = true
5656
} else {
57-
fmt.Printf("http.Post invalid response %d", resp.StatusCode)
57+
dispatcherLogger.Error(fmt.Sprintf("http.Post invalid response %d", resp.StatusCode), err)
5858
success = false
5959
}
6060
}
61-
callback(success)
62-
61+
return success, err
6362
}

optimizely/event/processor.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,15 +174,16 @@ func (p *QueueingEventProcessor) FlushEvents() {
174174
}
175175
}
176176
if batchEventCount > 0 {
177-
p.EventDispatcher.DispatchEvent(createLogEvent(batchEvent), func(success bool) {
178-
if success {
179-
p.Remove(batchEventCount)
180-
batchEventCount = 0
181-
batchEvent = Batch{}
182-
} else {
183-
failedToSend = true
184-
}
185-
})
177+
// TODO: figure out what to do with the error
178+
if success, _ := p.EventDispatcher.DispatchEvent(createLogEvent(batchEvent)); success {
179+
pLogger.Debug("Dispatched event successfully")
180+
p.Remove(batchEventCount)
181+
batchEventCount = 0
182+
batchEvent = Batch{}
183+
} else {
184+
pLogger.Warning("Failed to dispatch event successfully")
185+
failedToSend = true
186+
}
186187
}
187188
}
188189
p.Mux.Unlock()

optimizely/event/processor_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ type MockDispatcher struct {
3030
Events []LogEvent
3131
}
3232

33-
func (f *MockDispatcher) DispatchEvent(event LogEvent, callback func(success bool)) {
33+
func (f *MockDispatcher) DispatchEvent(event LogEvent) (bool, error) {
3434
f.Events = append(f.Events, event)
35-
callback(true)
35+
return true, nil
3636
}
3737

3838
func TestDefaultEventProcessor_ProcessBatch(t *testing.T) {

0 commit comments

Comments
 (0)