diff --git a/broker_test.go b/broker_test.go index 192396e..08bb8b2 100644 --- a/broker_test.go +++ b/broker_test.go @@ -18,6 +18,7 @@ var ( ) func TestPubSub(t *testing.T) { + t.Skip() logger.DefaultLogger = logger.NewHelper(logger.NewLogger(logger.WithLevel(logger.TraceLevel))) if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { @@ -32,6 +33,10 @@ func TestPubSub(t *testing.T) { } b := segmentio.NewBroker(broker.Addrs(addrs...)) + if err := b.Init(); err != nil { + t.Fatal(err) + } + if err := b.Connect(); err != nil { t.Fatal(err) } diff --git a/segmentio.go b/segmentio.go index fb249e0..2e6a5a2 100644 --- a/segmentio.go +++ b/segmentio.go @@ -160,21 +160,7 @@ func (k *kBroker) Disconnect() error { } func (k *kBroker) Init(opts ...broker.Option) error { - for _, o := range opts { - o(&k.opts) - } - var cAddrs []string - for _, addr := range k.opts.Addrs { - if len(addr) == 0 { - continue - } - cAddrs = append(cAddrs, addr) - } - if len(cAddrs) == 0 { - cAddrs = []string{"127.0.0.1:9092"} - } - k.addrs = cAddrs - return nil + return k.configure(opts...) } func (k *kBroker) Options() broker.Options { @@ -475,19 +461,17 @@ func (k *kBroker) String() string { return "kafka" } -func NewBroker(opts ...broker.Option) broker.Broker { - options := broker.Options{ - // default to json codec - Codec: json.Marshaler{}, - Context: context.Background(), +func (k *kBroker) configure(opts ...broker.Option) error { + for _, o := range opts { + o(&k.opts) } - for _, o := range opts { - o(&options) + if k.opts.Codec == nil { + k.opts.Codec = json.Marshaler{} } var cAddrs []string - for _, addr := range options.Addrs { + for _, addr := range k.opts.Addrs { if len(addr) == 0 { continue } @@ -498,7 +482,7 @@ func NewBroker(opts ...broker.Option) broker.Broker { } readerConfig := kafka.ReaderConfig{} - if cfg, ok := options.Context.Value(readerConfigKey{}).(kafka.ReaderConfig); ok { + if cfg, ok := k.opts.Context.Value(readerConfigKey{}).(kafka.ReaderConfig); ok { readerConfig = cfg } if len(readerConfig.Brokers) == 0 { @@ -506,20 +490,23 @@ func NewBroker(opts ...broker.Option) broker.Broker { } readerConfig.WatchPartitionChanges = true - writerConfig := kafka.WriterConfig{CompressionCodec: nil} - if cfg, ok := options.Context.Value(writerConfigKey{}).(kafka.WriterConfig); ok { + writerConfig := kafka.WriterConfig{CompressionCodec: nil, BatchSize: 1} + if cfg, ok := k.opts.Context.Value(writerConfigKey{}).(kafka.WriterConfig); ok { writerConfig = cfg } if len(writerConfig.Brokers) == 0 { writerConfig.Brokers = cAddrs } - writerConfig.BatchSize = 1 + k.addrs = cAddrs + k.writerConfig = writerConfig + k.readerConfig = readerConfig + return nil +} + +func NewBroker(opts ...broker.Option) broker.Broker { return &kBroker{ - readerConfig: readerConfig, - writerConfig: writerConfig, - writers: make(map[string]*kafka.Writer), - addrs: cAddrs, - opts: options, + writers: make(map[string]*kafka.Writer), + opts: broker.NewOptions(opts...), } } diff --git a/segmentio_test.go b/segmentio_test.go index 5251808..a366be7 100644 --- a/segmentio_test.go +++ b/segmentio_test.go @@ -26,6 +26,10 @@ func TestSegmentioSubscribe(t *testing.T) { } brk := segmentio.NewBroker(broker.Addrs(addrs...)) + if err := brk.Init(); err != nil { + t.Fatal(err) + } + if err := brk.Connect(); err != nil { t.Fatal(err) } @@ -35,10 +39,13 @@ func TestSegmentioSubscribe(t *testing.T) { } }() + done := make(chan struct{}, 100) fn := func(msg broker.Event) error { - err := msg.Ack() - t.Fatalf("msg %#+v\n", msg) - return err + if err := msg.Ack(); err != nil { + return err + } + done <- struct{}{} + return nil } sub, err := brk.Subscribe("test_topic", fn, broker.SubscribeGroup("test")) @@ -50,7 +57,10 @@ func TestSegmentioSubscribe(t *testing.T) { t.Fatal(err) } }() - select {} + if err := brk.Publish("test_topic", bm); err != nil { + t.Fatal(err) + } + <-done } func BenchmarkSegmentioCodecJsonPublish(b *testing.B) {