rework init func

Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
Василий Толстов 2020-09-23 22:53:34 +03:00
parent 4eecb06e2c
commit 12cff2a9e1
3 changed files with 38 additions and 36 deletions

View File

@ -18,6 +18,7 @@ var (
) )
func TestPubSub(t *testing.T) { func TestPubSub(t *testing.T) {
t.Skip()
logger.DefaultLogger = logger.NewHelper(logger.NewLogger(logger.WithLevel(logger.TraceLevel))) logger.DefaultLogger = logger.NewHelper(logger.NewLogger(logger.WithLevel(logger.TraceLevel)))
if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 { if tr := os.Getenv("INTEGRATION_TESTS"); len(tr) > 0 {
@ -32,6 +33,10 @@ func TestPubSub(t *testing.T) {
} }
b := segmentio.NewBroker(broker.Addrs(addrs...)) b := segmentio.NewBroker(broker.Addrs(addrs...))
if err := b.Init(); err != nil {
t.Fatal(err)
}
if err := b.Connect(); err != nil { if err := b.Connect(); err != nil {
t.Fatal(err) t.Fatal(err)
} }

View File

@ -160,21 +160,7 @@ func (k *kBroker) Disconnect() error {
} }
func (k *kBroker) Init(opts ...broker.Option) error { func (k *kBroker) Init(opts ...broker.Option) error {
for _, o := range opts { return k.configure(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
} }
func (k *kBroker) Options() broker.Options { func (k *kBroker) Options() broker.Options {
@ -475,19 +461,17 @@ func (k *kBroker) String() string {
return "kafka" return "kafka"
} }
func NewBroker(opts ...broker.Option) broker.Broker { func (k *kBroker) configure(opts ...broker.Option) error {
options := broker.Options{ for _, o := range opts {
// default to json codec o(&k.opts)
Codec: json.Marshaler{},
Context: context.Background(),
} }
for _, o := range opts { if k.opts.Codec == nil {
o(&options) k.opts.Codec = json.Marshaler{}
} }
var cAddrs []string var cAddrs []string
for _, addr := range options.Addrs { for _, addr := range k.opts.Addrs {
if len(addr) == 0 { if len(addr) == 0 {
continue continue
} }
@ -498,7 +482,7 @@ func NewBroker(opts ...broker.Option) broker.Broker {
} }
readerConfig := kafka.ReaderConfig{} 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 readerConfig = cfg
} }
if len(readerConfig.Brokers) == 0 { if len(readerConfig.Brokers) == 0 {
@ -506,20 +490,23 @@ func NewBroker(opts ...broker.Option) broker.Broker {
} }
readerConfig.WatchPartitionChanges = true readerConfig.WatchPartitionChanges = true
writerConfig := kafka.WriterConfig{CompressionCodec: nil} writerConfig := kafka.WriterConfig{CompressionCodec: nil, BatchSize: 1}
if cfg, ok := options.Context.Value(writerConfigKey{}).(kafka.WriterConfig); ok { if cfg, ok := k.opts.Context.Value(writerConfigKey{}).(kafka.WriterConfig); ok {
writerConfig = cfg writerConfig = cfg
} }
if len(writerConfig.Brokers) == 0 { if len(writerConfig.Brokers) == 0 {
writerConfig.Brokers = cAddrs 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{ return &kBroker{
readerConfig: readerConfig,
writerConfig: writerConfig,
writers: make(map[string]*kafka.Writer), writers: make(map[string]*kafka.Writer),
addrs: cAddrs, opts: broker.NewOptions(opts...),
opts: options,
} }
} }

View File

@ -26,6 +26,10 @@ func TestSegmentioSubscribe(t *testing.T) {
} }
brk := segmentio.NewBroker(broker.Addrs(addrs...)) brk := segmentio.NewBroker(broker.Addrs(addrs...))
if err := brk.Init(); err != nil {
t.Fatal(err)
}
if err := brk.Connect(); err != nil { if err := brk.Connect(); err != nil {
t.Fatal(err) t.Fatal(err)
} }
@ -35,11 +39,14 @@ func TestSegmentioSubscribe(t *testing.T) {
} }
}() }()
done := make(chan struct{}, 100)
fn := func(msg broker.Event) error { fn := func(msg broker.Event) error {
err := msg.Ack() if err := msg.Ack(); err != nil {
t.Fatalf("msg %#+v\n", msg)
return err return err
} }
done <- struct{}{}
return nil
}
sub, err := brk.Subscribe("test_topic", fn, broker.SubscribeGroup("test")) sub, err := brk.Subscribe("test_topic", fn, broker.SubscribeGroup("test"))
if err != nil { if err != nil {
@ -50,7 +57,10 @@ func TestSegmentioSubscribe(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
}() }()
select {} if err := brk.Publish("test_topic", bm); err != nil {
t.Fatal(err)
}
<-done
} }
func BenchmarkSegmentioCodecJsonPublish(b *testing.B) { func BenchmarkSegmentioCodecJsonPublish(b *testing.B) {