This repository has been archived by the owner on Jun 8, 2018. It is now read-only.
forked from chancez/heka-nsq
/
input_test.go
120 lines (94 loc) · 3.35 KB
/
input_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package nsq
import (
"fmt"
"github.com/bitly/go-nsq"
"github.com/mozilla-services/heka/pipeline"
pipeline_ts "github.com/mozilla-services/heka/pipeline/testsupport"
"github.com/mozilla-services/heka/pipelinemock"
"github.com/mozilla-services/heka/plugins"
plugins_ts "github.com/mozilla-services/heka/plugins/testsupport"
"github.com/rafrombrc/gomock/gomock"
gs "github.com/rafrombrc/gospec/src/gospec"
"sync"
)
func NsqInputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := pipeline.NewPipelineConfig(nil)
var wg sync.WaitGroup
errChan := make(chan error, 1)
retPackChan := make(chan *pipeline.PipelinePack, 1)
defer close(errChan)
defer close(retPackChan)
c.Specify("A nsq input", func() {
input := new(NsqInput)
ith := new(plugins_ts.InputTestHelper)
ith.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
ith.MockInputRunner = pipelinemock.NewMockInputRunner(ctrl)
config := input.ConfigStruct().(*NsqInputConfig)
config.Topic = "test_topic"
config.Channel = "test_channel"
startInput := func() {
wg.Add(1)
go func() {
errChan <- input.Run(ith.MockInputRunner, ith.MockHelper)
wg.Done()
}()
}
var mockConsumer *MockConsumer
input.newConsumer = func(topic, channel string, config *nsq.Config) (Consumer, error) {
mockConsumer, _ = NewMockConsumer(topic, channel, config)
return mockConsumer, nil
}
ith.Pack = pipeline.NewPipelinePack(pConfig.InputRecycleChan())
ith.PackSupply = make(chan *pipeline.PipelinePack, 1)
ith.PackSupply <- ith.Pack
inputName := "NsqInput"
ith.MockInputRunner.EXPECT().Name().Return(inputName).AnyTimes()
ith.MockInputRunner.EXPECT().InChan().Return(ith.PackSupply)
c.Specify("that is started", func() {
input.SetPipelineConfig(pConfig)
c.Specify("gets messages its subscribed to", func() {
c.Specify("injects messages into the pipeline when not configured with a decoder", func() {
ith.MockInputRunner.EXPECT().Inject(ith.Pack).Do(func(p *pipeline.PipelinePack) {
retPackChan <- p
}).AnyTimes()
err := input.Init(config)
c.Expect(err, gs.IsNil)
c.Expect(input.DecoderName, gs.Equals, "")
})
c.Specify("injects messages into the decoder when configured", func() {
decoderName := "ScribbleDecoder"
config.DecoderName = decoderName
decoder := new(plugins.ScribbleDecoder)
decoder.Init(&plugins.ScribbleDecoderConfig{})
mockDecoderRunner := pipelinemock.NewMockDecoderRunner(ctrl)
mockDecoderRunner.EXPECT().InChan().Return(retPackChan)
ith.MockHelper.EXPECT().DecoderRunner(decoderName,
fmt.Sprintf("%s-%s", inputName, decoderName),
).Return(mockDecoderRunner, true)
err := input.Init(config)
c.Expect(err, gs.IsNil)
c.Expect(input.DecoderName, gs.Equals, decoderName)
})
startInput()
// Should get two finished conn calls since we connect
// to lookupds and nsqds
c.Expect(<-mockConsumer.finishedConn, gs.IsTrue)
c.Expect(<-mockConsumer.finishedConn, gs.IsTrue)
id := nsq.MessageID{}
body := []byte("test")
msg := nsq.NewMessage(id, body)
for _, handler := range mockConsumer.handlers {
handler.HandleMessage(msg)
}
p := <-retPackChan
c.Expect(p.Message.GetPayload(), gs.Equals, "test")
})
input.Stop()
wg.Wait()
c.Expect(<-errChan, gs.IsNil)
})
})
}