Skip to content

Commit c136b2e

Browse files
authored
Merge pull request #8 from violetpay-org/feature/direct-streamer
Feature/direct streamer
2 parents 4894a96 + daa0ebb commit c136b2e

File tree

5 files changed

+240
-23
lines changed

5 files changed

+240
-23
lines changed

direct.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package qstreamer
2+
3+
import (
4+
"github.com/violetpay-org/queue-streamer/common"
5+
"github.com/violetpay-org/queue-streamer/internal"
6+
)
7+
8+
// DirectStreamer is a streamer that streams messages from a topic to a topic.
9+
type DirectStreamer struct {
10+
ts TopicStreamer
11+
}
12+
13+
// NewDirectStreamer creates a new topic streamer that streams messages from a topic to a topic.
14+
// The streamer is configured with a list of brokers, a topic to stream from and a consumer group id .
15+
// If you want to override the default configuration of the sarama consumer and producer, you can pass additional arguments.
16+
// - ds := NewDirectStreamer(brokers, topic, groupId)
17+
// - ds := NewDirectStreamer(brokers, topic, groupId, consumerConfig, producerConfig)
18+
// - ds := NewDirectStreamer(brokers, topic, groupId, nil, producerConfig)
19+
func NewDirectStreamer(brokers []string, src common.Topic, groupId string, args ...interface{}) *DirectStreamer {
20+
ts := NewTopicStreamer(brokers, src, groupId, args...)
21+
return &DirectStreamer{
22+
ts: *ts,
23+
}
24+
}
25+
26+
func (ds *DirectStreamer) Config() (bool, StreamConfig) {
27+
if len(ds.ts.configs) == 0 {
28+
return false, StreamConfig{}
29+
}
30+
31+
return true, ds.ts.configs[0]
32+
}
33+
34+
func (ds *DirectStreamer) SetConfig(config StreamConfig) {
35+
if len(ds.ts.configs) == 0 {
36+
ds.ts.configs = append(ds.ts.configs, config)
37+
} else {
38+
ds.ts.configs[0] = config
39+
}
40+
}
41+
42+
func (ds *DirectStreamer) Topic() common.Topic {
43+
return ds.ts.Topic()
44+
}
45+
46+
func (ds *DirectStreamer) Consumer() *internal.StreamConsumer {
47+
return ds.ts.Consumer()
48+
}
49+
50+
func (ds *DirectStreamer) GroupId() string {
51+
return ds.ts.GroupId()
52+
}
53+
54+
func (ds *DirectStreamer) Run() {
55+
ds.ts.Run()
56+
}
57+
58+
func (ds *DirectStreamer) Stop() error {
59+
return ds.ts.Stop()
60+
}

direct_test.go

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
package qstreamer_test
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
qstreamer "github.com/violetpay-org/queue-streamer"
6+
"testing"
7+
"time"
8+
)
9+
10+
var dbrokers = []string{"localhost:9093"}
11+
var dTopic = qstreamer.Topic("test", 1)
12+
var dTopic2 = qstreamer.Topic("test2", 2)
13+
14+
func TestNewDirectStreamer(t *testing.T) {
15+
var ds *qstreamer.DirectStreamer
16+
17+
t.Cleanup(func() {
18+
ds = nil
19+
})
20+
21+
t.Run("NewDirectStreamer", func(t *testing.T) {
22+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
23+
assert.NotNil(t, ds)
24+
})
25+
}
26+
27+
func TestDirectStreamer_Config(t *testing.T) {
28+
var ds *qstreamer.DirectStreamer
29+
30+
t.Cleanup(func() {
31+
ds = nil
32+
})
33+
34+
t.Run("No Config set", func(t *testing.T) {
35+
t.Cleanup(func() {
36+
ds = nil
37+
})
38+
39+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
40+
ok, _ := ds.Config()
41+
42+
assert.False(t, ok)
43+
})
44+
45+
t.Run("Set Config", func(t *testing.T) {
46+
t.Cleanup(func() {
47+
ds = nil
48+
})
49+
50+
ms := qstreamer.NewPassThroughSerializer()
51+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
52+
cfg := qstreamer.NewStreamConfig(ms, dTopic)
53+
ds.SetConfig(cfg)
54+
55+
ok, cfg := ds.Config()
56+
assert.True(t, ok)
57+
58+
assert.Equal(t, dTopic, cfg.Topic())
59+
assert.Equal(t, ms, cfg.MessageSerializer())
60+
})
61+
62+
t.Run("Set Config multiple times", func(t *testing.T) {
63+
t.Cleanup(func() {
64+
ds = nil
65+
})
66+
67+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
68+
69+
ms1 := qstreamer.NewPassThroughSerializer()
70+
ms2 := qstreamer.NewPassThroughSerializer()
71+
72+
cfg1 := qstreamer.NewStreamConfig(ms1, dTopic)
73+
cfg2 := qstreamer.NewStreamConfig(ms2, dTopic2)
74+
75+
ds.SetConfig(cfg1)
76+
ds.SetConfig(cfg2)
77+
78+
ok, dsCfg := ds.Config()
79+
assert.True(t, ok)
80+
assert.Equal(t, dTopic2, dsCfg.Topic())
81+
assert.Equal(t, ms2, dsCfg.MessageSerializer())
82+
})
83+
}
84+
85+
func TestDirectStreamer_Consumer(t *testing.T) {
86+
var ds *qstreamer.DirectStreamer
87+
88+
t.Cleanup(func() {
89+
ds = nil
90+
})
91+
92+
t.Run("Consumer", func(t *testing.T) {
93+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
94+
assert.NotNil(t, ds.Consumer())
95+
})
96+
}
97+
98+
func TestDirectStreamer_Topic(t *testing.T) {
99+
var ds *qstreamer.DirectStreamer
100+
101+
t.Cleanup(func() {
102+
ds = nil
103+
})
104+
105+
t.Run("Topic", func(t *testing.T) {
106+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
107+
assert.Equal(t, dTopic, ds.Topic())
108+
})
109+
}
110+
111+
func TestDirectStreamer_GroupId(t *testing.T) {
112+
var ds *qstreamer.DirectStreamer
113+
114+
t.Cleanup(func() {
115+
ds = nil
116+
})
117+
118+
t.Run("GroupID", func(t *testing.T) {
119+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "testGroupId")
120+
assert.Equal(t, "testGroupId", ds.GroupId())
121+
})
122+
}
123+
124+
func TestDirectStreamer_Run(t *testing.T) {
125+
var ds *qstreamer.DirectStreamer
126+
127+
t.Cleanup(func() {
128+
ds = nil
129+
})
130+
131+
t.Run("Run", func(t *testing.T) {
132+
t.Cleanup(func() {
133+
err := ds.Stop()
134+
assert.Nil(t, err)
135+
ds = nil
136+
})
137+
138+
ds = qstreamer.NewDirectStreamer(dbrokers, dTopic, "")
139+
cfg := qstreamer.NewStreamConfig(qstreamer.NewPassThroughSerializer(), dTopic)
140+
ds.SetConfig(cfg)
141+
142+
assert.NotPanics(t, func() {
143+
go ds.Run()
144+
time.Sleep(1 * time.Second)
145+
})
146+
})
147+
}

internal/utils_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
)
88

99
func TestCopy(t *testing.T) {
10-
t.Parallel()
11-
1210
t.Run("Copy pointer", func(t *testing.T) {
1311
type TestStruct struct {
1412
Name string

streamer.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,24 @@ import (
99
"sync"
1010
)
1111

12+
// TopicStreamer is a streamer that streams messages from a topic to other topics.
1213
type TopicStreamer struct {
1314
topic common.Topic
1415
configs []StreamConfig
1516
cancel context.CancelFunc
1617
mutex *sync.Mutex
18+
groupId string
1719

1820
consumer *internal.StreamConsumer
1921
}
2022

2123
// NewTopicStreamer creates a new topic streamer that streams messages from a topic to other topics.
22-
// The streamer is configured with a list of brokers and a topic to stream from.
24+
// The streamer is configured with a list of brokers, a topic to stream from and a consumer group id .
2325
// If you want to override the default configuration of the sarama consumer and producer, you can pass additional arguments.
24-
// - ts := NewTopicStreamer(brokers, topic)
25-
// - ts := NewTopicStreamer(brokers, topic, consumerConfig, producerConfig)
26-
// - ts := NewTopicStreamer(brokers, topic, nil, producerConfig)
27-
func NewTopicStreamer(brokers []string, topic common.Topic, args ...interface{}) *TopicStreamer {
26+
// - ts := NewTopicStreamer(brokers, topic, groupId)
27+
// - ts := NewTopicStreamer(brokers, topic, groupId, consumerConfig, producerConfig)
28+
// - ts := NewTopicStreamer(brokers, topic, groupId, nil, producerConfig)
29+
func NewTopicStreamer(brokers []string, topic common.Topic, groupId string, args ...interface{}) *TopicStreamer {
2830
var ccfg *sarama.Config
2931
var pcfg *sarama.Config
3032

@@ -44,9 +46,13 @@ func NewTopicStreamer(brokers []string, topic common.Topic, args ...interface{})
4446
panic("Invalid number of arguments")
4547
}
4648

49+
if groupId == "" {
50+
groupId = "queue-streamer-default-group"
51+
}
52+
4753
consumer := internal.NewStreamConsumer(
4854
topic,
49-
"groupId",
55+
groupId,
5056
brokers,
5157
ccfg,
5258
pcfg,
@@ -58,6 +64,7 @@ func NewTopicStreamer(brokers []string, topic common.Topic, args ...interface{})
5864
cancel: nil,
5965
consumer: consumer,
6066
mutex: &sync.Mutex{},
67+
groupId: groupId,
6168
}
6269
}
6370

@@ -77,6 +84,10 @@ func (ts *TopicStreamer) AddConfig(config StreamConfig) {
7784
ts.configs = append(ts.configs, config)
7885
}
7986

87+
func (ts *TopicStreamer) GroupId() string {
88+
return ts.groupId
89+
}
90+
8091
func (ts *TopicStreamer) Run() {
8192
dests := make([]common.Topic, 0)
8293
mss := make([]common.MessageSerializer, 0)

0 commit comments

Comments
 (0)