-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
98 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package instrument | ||
|
||
// Allow to wrap multiple collectors as one collector | ||
import ( | ||
"time" | ||
|
||
confluent "github.com/confluentinc/confluent-kafka-go/kafka" | ||
) | ||
|
||
// MultiCollector allows you to compose a list of collector | ||
type MultiCollector struct { | ||
collectors []Collector | ||
} | ||
|
||
// NewMultiCollector is a constructor for multi collector | ||
func NewMultiCollector(collectors ...Collector) MultiCollector { | ||
return MultiCollector{ | ||
collectors: collectors, | ||
} | ||
} | ||
|
||
// Before calls all Before method of inner collectors | ||
func (m MultiCollector) Before(message *confluent.Message, action Action, start time.Time) { | ||
for i := 0; i < len(m.collectors); i++ { | ||
m.collectors[i].Before(message, action, start) | ||
} | ||
} | ||
|
||
// After calls all After method of inner collectors | ||
func (m MultiCollector) After(message *confluent.Message, action Action, err error, start time.Time) { | ||
for i := 0; i < len(m.collectors); i++ { | ||
m.collectors[i].After(message, action, err, start) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package instrument_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
confluent "github.com/confluentinc/confluent-kafka-go/kafka" | ||
"github.com/etf1/kafka-transformer/pkg/instrument" | ||
) | ||
|
||
// a dummy collector which appends 1 character eacy time a before/after is called | ||
type dummyCollector struct { | ||
before string | ||
after string | ||
result string | ||
} | ||
|
||
func newDummyCollector(before, after string) *dummyCollector { | ||
return &dummyCollector{ | ||
before: before, | ||
after: after, | ||
} | ||
} | ||
|
||
func (dc *dummyCollector) Before(msg *confluent.Message, action instrument.Action, start time.Time) { | ||
dc.result += dc.before | ||
} | ||
|
||
func (dc *dummyCollector) After(msg *confluent.Message, action instrument.Action, err error, start time.Time) { | ||
dc.result += dc.after | ||
} | ||
|
||
// Rule #1: all collector should be called in sequence as passed in argument | ||
func TestMultiCollector(t *testing.T) { | ||
dc := newDummyCollector("a", "b") | ||
|
||
mc := instrument.NewMultiCollector(dc) | ||
mc.Before(nil, instrument.OverallTime, time.Now()) | ||
mc.After(nil, instrument.OverallTime, nil, time.Now()) | ||
if dc.result != "ab" { | ||
t.Errorf("unexpected result: %q", dc.result) | ||
} | ||
|
||
dc = newDummyCollector("a", "b") | ||
mc = instrument.NewMultiCollector(dc, dc) | ||
mc.Before(nil, instrument.OverallTime, time.Now()) | ||
mc.After(nil, instrument.OverallTime, nil, time.Now()) | ||
if dc.result != "aabb" { | ||
t.Errorf("unexpected result: %q", dc.result) | ||
} | ||
|
||
dc = newDummyCollector("b", "a") | ||
mc = instrument.NewMultiCollector(dc, dc) | ||
mc.Before(nil, instrument.OverallTime, time.Now()) | ||
mc.After(nil, instrument.OverallTime, nil, time.Now()) | ||
if dc.result != "bbaa" { | ||
t.Errorf("unexpected result: %q", dc.result) | ||
} | ||
} |