本文整理匯總了Golang中github.com/mozilla-services/heka/pipeline/testsupport.GetTestMessage函數的典型用法代碼示例。如果您正苦於以下問題:Golang GetTestMessage函數的具體用法?Golang GetTestMessage怎麽用?Golang GetTestMessage使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GetTestMessage函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: BenchmarkMultiDecodeProtobuf
func BenchmarkMultiDecodeProtobuf(b *testing.B) {
b.StopTimer()
pConfig := NewPipelineConfig(nil) // initializes Globals
msg := pipeline_ts.GetTestMessage()
msg.SetPayload("This is a test")
pack := NewPipelinePack(pConfig.InputRecycleChan())
pack.MsgBytes, _ = proto.Marshal(msg)
decoder := new(MultiDecoder)
decoder.SetPipelineConfig(pConfig)
conf := decoder.ConfigStruct().(*MultiDecoderConfig)
sub := new(ProtobufDecoder)
sub.SetPipelineConfig(pConfig)
sub.Init(nil)
wrapper0 := NewPluginWrapper("sub", pConfig)
wrapper0.CreateWithError = func() (interface{}, error) {
return sub, nil
}
pConfig.DecoderWrappers["sub"] = wrapper0
conf.CascadeStrategy = "first-wins"
conf.Subs = []string{"sub"}
decoder.Init(conf)
b.StartTimer()
for i := 0; i < b.N; i++ {
decoder.Decode(pack)
}
}
示例2: ProtobufDecoderSpec
func ProtobufDecoderSpec(c gospec.Context) {
t := &ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
msg := ts.GetTestMessage()
config := NewPipelineConfig(nil) // Initializes globals.
c.Specify("A ProtobufDecoder", func() {
encoded, err := proto.Marshal(msg)
c.Assume(err, gs.IsNil)
pack := NewPipelinePack(config.inputRecycleChan)
decoder := new(ProtobufDecoder)
decoder.sampleDenominator = 1000 // Since we don't call decoder.Init().
c.Specify("decodes a protobuf message", func() {
pack.MsgBytes = encoded
_, err := decoder.Decode(pack)
c.Expect(err, gs.IsNil)
c.Expect(pack.Message, gs.Equals, msg)
v, ok := pack.Message.GetFieldValue("foo")
c.Expect(ok, gs.IsTrue)
c.Expect(v, gs.Equals, "bar")
})
c.Specify("returns an error for bunk encoding", func() {
bunk := append([]byte{0, 0, 0}, encoded...)
pack.MsgBytes = bunk
_, err := decoder.Decode(pack)
c.Expect(err, gs.Not(gs.IsNil))
})
})
}
示例3: BenchmarkMultiDecodeProtobuf
func BenchmarkMultiDecodeProtobuf(b *testing.B) {
b.StopTimer()
pConfig := NewPipelineConfig(nil) // initializes Globals
msg := pipeline_ts.GetTestMessage()
msg.SetPayload("This is a test")
pack := NewPipelinePack(pConfig.InputRecycleChan())
pack.MsgBytes, _ = proto.Marshal(msg)
decoder := new(MultiDecoder)
decoder.SetPipelineConfig(pConfig)
conf := decoder.ConfigStruct().(*MultiDecoderConfig)
RegisterPlugin("ProtobufDecoder", func() interface{} {
return &ProtobufDecoder{}
})
defer delete(AvailablePlugins, "ProtobufDecoder")
var section PluginConfig
_, err := toml.Decode("", §ion)
if err != nil {
b.Fatalf("Error decoding empty TOML: %s", err.Error())
}
maker, err := NewPluginMaker("ProtobufDecoder", pConfig, section)
if err != nil {
b.Fatalf("Error decoding empty TOML: %s", err.Error())
}
pConfig.DecoderMakers["ProtobufDecoder"] = maker
conf.CascadeStrategy = "first-wins"
conf.Subs = []string{"sub"}
decoder.Init(conf)
b.StartTimer()
for i := 0; i < b.N; i++ {
decoder.Decode(pack)
}
}
示例4: FilterRunnerSpec
func FilterRunnerSpec(c gs.Context) {
c.Specify("A filterrunner", func() {
pConfig := NewPipelineConfig(nil)
filter := &CounterFilter{}
commonFO := CommonFOConfig{
Matcher: "Type == 'bogus'",
}
chanSize := 10
fRunner, err := NewFORunner("counterFilter", filter, commonFO, "CounterFilter",
chanSize)
fRunner.h = pConfig
c.Assume(err, gs.IsNil)
pack := NewPipelinePack(pConfig.injectRecycleChan)
pConfig.injectRecycleChan <- pack
pack.Message = ts.GetTestMessage()
c.Assume(pack.TrustMsgBytes, gs.IsFalse)
msgEncoding, err := proto.Marshal(pack.Message)
c.Assume(err, gs.IsNil)
c.Specify("puts protobuf encoding into MsgBytes before delivery", func() {
result := fRunner.Inject(pack)
c.Expect(result, gs.IsTrue)
recd := <-pConfig.router.inChan
c.Expect(recd, gs.Equals, pack)
c.Expect(recd.TrustMsgBytes, gs.IsTrue)
c.Expect(bytes.Equal(msgEncoding, recd.MsgBytes), gs.IsTrue)
})
})
}
示例5: StatsdInputSpec
func StatsdInputSpec(c gs.Context) {
t := &pipeline_ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := NewPipelineConfig(nil)
ith := new(plugins_ts.InputTestHelper)
ith.Msg = pipeline_ts.GetTestMessage()
ith.Pack = NewPipelinePack(pConfig.InputRecycleChan())
ith.PackSupply = make(chan *PipelinePack, 1)
// Specify localhost, but we're not really going to use the network
ith.AddrStr = "localhost:55565"
ith.ResolvedAddrStr = "127.0.0.1:55565"
// set up mock helper, input runner, and stat accumulator
ith.MockHelper = NewMockPluginHelper(ctrl)
ith.MockInputRunner = NewMockInputRunner(ctrl)
mockStatAccum := NewMockStatAccumulator(ctrl)
c.Specify("A StatsdInput", func() {
statsdInput := StatsdInput{}
config := statsdInput.ConfigStruct().(*StatsdInputConfig)
config.Address = ith.AddrStr
err := statsdInput.Init(config)
c.Assume(err, gs.IsNil)
realListener := statsdInput.listener
c.Expect(realListener.LocalAddr().String(), gs.Equals, ith.ResolvedAddrStr)
realListener.Close()
mockListener := pipeline_ts.NewMockConn(ctrl)
statsdInput.listener = mockListener
ith.MockHelper.EXPECT().StatAccumulator("StatAccumInput").Return(mockStatAccum, nil)
mockListener.EXPECT().Close()
mockListener.EXPECT().SetReadDeadline(gomock.Any())
c.Specify("sends a Stat to the StatAccumulator", func() {
statName := "sample.count"
statVal := 303
msg := fmt.Sprintf("%s:%d|c\n", statName, statVal)
expected := Stat{statName, strconv.Itoa(statVal), "c", float32(1)}
mockStatAccum.EXPECT().DropStat(expected).Return(true)
readCall := mockListener.EXPECT().Read(make([]byte, 512))
readCall.Return(len(msg), nil)
readCall.Do(func(msgBytes []byte) {
copy(msgBytes, []byte(msg))
statsdInput.Stop()
})
var wg sync.WaitGroup
wg.Add(1)
go func() {
err = statsdInput.Run(ith.MockInputRunner, ith.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
wg.Wait()
})
})
}
示例6: BenchmarkEncodeProtobuf
func BenchmarkEncodeProtobuf(b *testing.B) {
b.StopTimer()
msg := ts.GetTestMessage()
b.StartTimer()
for i := 0; i < b.N; i++ {
proto.Marshal(msg)
}
}
示例7: MessageTemplateSpec
func MessageTemplateSpec(c gs.Context) {
c.Specify("A message template", func() {
mt := make(MessageTemplate)
mt["Logger"] = "test logger"
mt["Payload"] = "test payload"
mt["Hostname"] = "host.example.com"
mt["Pid"] = "123.456"
mt["Type"] = "test type"
mt["Severity"] = "23"
mt["tmplTest|baz"] = "bar"
msg := ts.GetTestMessage()
c.Specify("replaces message values", func() {
err := mt.PopulateMessage(msg, nil)
c.Assume(err, gs.IsNil)
c.Expect(msg.GetLogger(), gs.Equals, mt["Logger"])
c.Expect(msg.GetPayload(), gs.Equals, mt["Payload"])
c.Expect(msg.GetHostname(), gs.Equals, mt["Hostname"])
c.Expect(msg.GetPid(), gs.Equals, int32(123))
c.Expect(msg.GetType(), gs.Equals, mt["Type"])
c.Expect(msg.GetSeverity(), gs.Equals, int32(23))
fields := msg.FindAllFields("tmplTest")
c.Expect(len(fields), gs.Equals, 1)
field := fields[0]
value := field.GetValueString()
c.Expect(len(value), gs.Equals, 1)
c.Expect(value[0], gs.Equals, "bar")
c.Expect(field.GetRepresentation(), gs.Equals, "baz")
})
c.Specify("honors substitutions", func() {
mt["Payload"] = "this is %substitution%"
mt["Hostname"] = "%host%.example.com"
mt["Type"] = "another %substitution%"
mt["tmplTest|baz"] = "%fieldvalue%"
subs := make(map[string]string)
subs["host"] = "otherhost"
subs["substitution"] = "a test"
subs["fieldvalue"] = "wakajawaka"
err := mt.PopulateMessage(msg, subs)
c.Assume(err, gs.IsNil)
c.Expect(msg.GetLogger(), gs.Equals, mt["Logger"])
c.Expect(msg.GetPayload(), gs.Equals, "this is a test")
c.Expect(msg.GetHostname(), gs.Equals, "otherhost.example.com")
c.Expect(msg.GetPid(), gs.Equals, int32(123))
c.Expect(msg.GetType(), gs.Equals, "another a test")
c.Expect(msg.GetSeverity(), gs.Equals, int32(23))
fields := msg.FindAllFields("tmplTest")
c.Expect(len(fields), gs.Equals, 1)
field := fields[0]
value := field.GetValueString()
c.Expect(len(value), gs.Equals, 1)
c.Expect(value[0], gs.Equals, subs["fieldvalue"])
c.Expect(field.GetRepresentation(), gs.Equals, "baz")
})
})
}
示例8: BenchmarkDecodeProtobuf
func BenchmarkDecodeProtobuf(b *testing.B) {
b.StopTimer()
msg := ts.GetTestMessage()
encoded, _ := proto.Marshal(msg)
config := NewPipelineConfig(nil)
pack := NewPipelinePack(config.inputRecycleChan)
decoder := new(ProtobufDecoder)
pack.MsgBytes = encoded
b.StartTimer()
for i := 0; i < b.N; i++ {
decoder.Decode(pack)
}
}
示例9: TcpInputSpec
func TcpInputSpec(c gs.Context) {
t := &pipeline_ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
config := NewPipelineConfig(nil)
ith := new(plugins_ts.InputTestHelper)
ith.Msg = pipeline_ts.GetTestMessage()
ith.Pack = NewPipelinePack(config.InputRecycleChan())
ith.AddrStr = "localhost:55565"
ith.ResolvedAddrStr = "127.0.0.1:55565"
// set up mock helper, decoder set, and packSupply channel
ith.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
ith.MockInputRunner = pipelinemock.NewMockInputRunner(ctrl)
ith.Decoder = pipelinemock.NewMockDecoderRunner(ctrl)
ith.PackSupply = make(chan *PipelinePack, 1)
ith.DecodeChan = make(chan *PipelinePack)
key := "testkey"
signers := map[string]Signer{"test_1": {key}}
signer := "test"
c.Specify("A TcpInput protobuf parser", func() {
ith.MockInputRunner.EXPECT().Name().Return("TcpInput")
tcpInput := TcpInput{}
err := tcpInput.Init(&TcpInputConfig{Net: "tcp", Address: ith.AddrStr,
Signers: signers,
Decoder: "ProtobufDecoder",
ParserType: "message.proto"})
c.Assume(err, gs.IsNil)
realListener := tcpInput.listener
c.Expect(realListener.Addr().String(), gs.Equals, ith.ResolvedAddrStr)
realListener.Close()
mockConnection := pipeline_ts.NewMockConn(ctrl)
mockListener := pipeline_ts.NewMockListener(ctrl)
tcpInput.listener = mockListener
addr := new(address)
addr.str = "123"
mockConnection.EXPECT().RemoteAddr().Return(addr)
mbytes, _ := proto.Marshal(ith.Msg)
header := &message.Header{}
header.SetMessageLength(uint32(len(mbytes)))
err = errors.New("connection closed") // used in the read return(s)
readCall := mockConnection.EXPECT().Read(gomock.Any())
readEnd := mockConnection.EXPECT().Read(gomock.Any()).After(readCall)
readEnd.Return(0, err)
mockConnection.EXPECT().SetReadDeadline(gomock.Any()).Return(nil).AnyTimes()
mockConnection.EXPECT().Close()
neterr := pipeline_ts.NewMockError(ctrl)
neterr.EXPECT().Temporary().Return(false)
acceptCall := mockListener.EXPECT().Accept().Return(mockConnection, nil)
acceptCall.Do(func() {
acceptCall = mockListener.EXPECT().Accept()
acceptCall.Return(nil, neterr)
})
mockDecoderRunner := ith.Decoder.(*pipelinemock.MockDecoderRunner)
mockDecoderRunner.EXPECT().InChan().Return(ith.DecodeChan)
ith.MockInputRunner.EXPECT().InChan().Return(ith.PackSupply)
enccall := ith.MockHelper.EXPECT().DecoderRunner("ProtobufDecoder", "TcpInput-123-ProtobufDecoder").AnyTimes()
enccall.Return(ith.Decoder, true)
ith.MockHelper.EXPECT().StopDecoderRunner(ith.Decoder)
cleanup := func() {
mockListener.EXPECT().Close()
tcpInput.Stop()
tcpInput.wg.Wait()
}
c.Specify("reads a message from its connection", func() {
hbytes, _ := proto.Marshal(header)
buflen := 3 + len(hbytes) + len(mbytes)
readCall.Return(buflen, nil)
readCall.Do(getPayloadBytes(hbytes, mbytes))
go tcpInput.Run(ith.MockInputRunner, ith.MockHelper)
defer cleanup()
ith.PackSupply <- ith.Pack
packRef := <-ith.DecodeChan
c.Expect(ith.Pack, gs.Equals, packRef)
c.Expect(string(ith.Pack.MsgBytes), gs.Equals, string(mbytes))
})
c.Specify("reads a MD5 signed message from its connection", func() {
header.SetHmacHashFunction(message.Header_MD5)
header.SetHmacSigner(signer)
header.SetHmacKeyVersion(uint32(1))
hm := hmac.New(md5.New, []byte(key))
hm.Write(mbytes)
header.SetHmac(hm.Sum(nil))
hbytes, _ := proto.Marshal(header)
buflen := 3 + len(hbytes) + len(mbytes)
readCall.Return(buflen, nil)
readCall.Do(getPayloadBytes(hbytes, mbytes))
go tcpInput.Run(ith.MockInputRunner, ith.MockHelper)
defer cleanup()
//.........這裏部分代碼省略.........
示例10: DashboardOutputSpec
func DashboardOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
pConfig := pipeline.NewPipelineConfig(nil)
dashboardOutput := new(DashboardOutput)
dashboardOutput.pConfig = pConfig
oth := plugins_ts.NewOutputTestHelper(ctrl)
oth.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
oth.MockOutputRunner = pipelinemock.NewMockOutputRunner(ctrl)
errChan := make(chan error, 1)
startOutput := func() {
go func() {
errChan <- dashboardOutput.Run(oth.MockOutputRunner, oth.MockHelper)
}()
}
if runtime.GOOS != "windows" {
c.Specify("A DashboardOutput", func() {
tmpdir, err := ioutil.TempDir("", "dashboard_output_test")
c.Assume(err, gs.IsNil)
config := dashboardOutput.ConfigStruct().(*DashboardOutputConfig)
config.WorkingDirectory = tmpdir
c.Specify("Init halts if basedirectory is not writable", func() {
err := os.MkdirAll(tmpdir, 0400)
c.Assume(err, gs.IsNil)
defer os.RemoveAll(tmpdir)
err = dashboardOutput.Init(config)
c.Assume(err, gs.Not(gs.IsNil))
})
c.Specify("that is running", func() {
startedChan := make(chan bool, 1)
defer close(startedChan)
ts := httptest.NewUnstartedServer(nil)
dashboardOutput.starterFunc = func(hli *DashboardOutput) error {
ts.Start()
startedChan <- true
return nil
}
ticker := make(chan time.Time)
inChan := make(chan *pipeline.PipelinePack, 1)
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message = pipeline_ts.GetTestMessage()
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().Ticker().Return(ticker)
err := os.MkdirAll(tmpdir, 0700)
c.Assume(err, gs.IsNil)
defer os.RemoveAll(tmpdir)
dashboardOutput.handler = http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// Noop
})
c.Specify("sets custom http headers", func() {
config.Headers = http.Header{
"One": []string{"two", "three"},
"Four": []string{"five", "six", "seven"},
}
err = dashboardOutput.Init(config)
c.Assume(err, gs.IsNil)
ts.Config = dashboardOutput.server
startOutput()
inChan <- pack
<-startedChan
resp, err := http.Get(ts.URL)
c.Assume(err, gs.IsNil)
resp.Body.Close()
c.Assume(resp.StatusCode, gs.Equals, 200)
// Verify headers are there
eq := reflect.DeepEqual(resp.Header["One"], config.Headers["One"])
c.Expect(eq, gs.IsTrue)
eq = reflect.DeepEqual(resp.Header["Four"], config.Headers["Four"])
c.Expect(eq, gs.IsTrue)
})
close(inChan)
c.Expect(<-errChan, gs.IsNil)
ts.Close()
})
})
}
}
示例11: LogstreamerInputSpec
func LogstreamerInputSpec(c gs.Context) {
t := &pipeline_ts.SimpleT{}
ctrl := gomock.NewController(t)
defer ctrl.Finish()
here, _ := os.Getwd()
dirPath := filepath.Join(here, "../../logstreamer", "testdir", "filehandling/subdir")
tmpDir, tmpErr := ioutil.TempDir("", "hekad-tests")
c.Expect(tmpErr, gs.Equals, nil)
defer func() {
tmpErr = os.RemoveAll(tmpDir)
c.Expect(tmpErr, gs.IsNil)
}()
globals := DefaultGlobals()
globals.BaseDir = tmpDir
pConfig := NewPipelineConfig(globals)
ith := new(plugins_ts.InputTestHelper)
ith.Msg = pipeline_ts.GetTestMessage()
ith.Pack = NewPipelinePack(pConfig.InputRecycleChan())
// Specify localhost, but we're not really going to use the network.
ith.AddrStr = "localhost:55565"
ith.ResolvedAddrStr = "127.0.0.1:55565"
// Set up mock helper, runner, and pack supply channel.
ith.MockHelper = pipelinemock.NewMockPluginHelper(ctrl)
ith.MockInputRunner = pipelinemock.NewMockInputRunner(ctrl)
ith.MockDeliverer = pipelinemock.NewMockDeliverer(ctrl)
ith.MockSplitterRunner = pipelinemock.NewMockSplitterRunner(ctrl)
ith.PackSupply = make(chan *PipelinePack, 1)
c.Specify("A LogstreamerInput", func() {
lsInput := &LogstreamerInput{pConfig: pConfig}
lsiConfig := lsInput.ConfigStruct().(*LogstreamerInputConfig)
lsiConfig.LogDirectory = dirPath
lsiConfig.FileMatch = `file.log(\.?)(?P<Seq>\d+)?`
lsiConfig.Differentiator = []string{"logfile"}
lsiConfig.Priority = []string{"^Seq"}
c.Specify("w/ no translation map", func() {
err := lsInput.Init(lsiConfig)
c.Expect(err, gs.IsNil)
c.Expect(len(lsInput.plugins), gs.Equals, 1)
// Create pool of packs.
numLines := 5 // # of lines in the log file we're parsing.
packs := make([]*PipelinePack, numLines)
ith.PackSupply = make(chan *PipelinePack, numLines)
for i := 0; i < numLines; i++ {
packs[i] = NewPipelinePack(ith.PackSupply)
ith.PackSupply <- packs[i]
}
c.Specify("reads a log file", func() {
// Expect InputRunner calls to get InChan and inject outgoing msgs.
ith.MockInputRunner.EXPECT().LogError(gomock.Any()).AnyTimes()
ith.MockInputRunner.EXPECT().LogMessage(gomock.Any()).AnyTimes()
ith.MockInputRunner.EXPECT().NewDeliverer("1").Return(ith.MockDeliverer)
ith.MockInputRunner.EXPECT().NewSplitterRunner("1").Return(
ith.MockSplitterRunner)
ith.MockSplitterRunner.EXPECT().UseMsgBytes().Return(false)
ith.MockSplitterRunner.EXPECT().IncompleteFinal().Return(false)
ith.MockSplitterRunner.EXPECT().SetPackDecorator(gomock.Any())
getRecCall := ith.MockSplitterRunner.EXPECT().GetRecordFromStream(
gomock.Any()).Times(numLines)
line := "boo hoo foo foo"
getRecCall.Return(len(line), []byte(line), nil)
getRecCall = ith.MockSplitterRunner.EXPECT().GetRecordFromStream(gomock.Any())
getRecCall.Return(0, make([]byte, 0), io.EOF)
deliverChan := make(chan []byte, 1)
deliverCall := ith.MockSplitterRunner.EXPECT().DeliverRecord(gomock.Any(),
ith.MockDeliverer).Times(numLines)
deliverCall.Do(func(record []byte, del Deliverer) {
deliverChan <- record
})
ith.MockDeliverer.EXPECT().Done()
runOutChan := make(chan error, 1)
go func() {
err = lsInput.Run(ith.MockInputRunner, ith.MockHelper)
runOutChan <- err
}()
dur, _ := time.ParseDuration("5s")
timeout := time.After(dur)
timed := false
for x := 0; x < numLines; x++ {
select {
case record := <-deliverChan:
c.Expect(string(record), gs.Equals, line)
case <-timeout:
timed = true
x += numLines
}
// Free up the scheduler while we wait for the log file lines
//.........這裏部分代碼省略.........
示例12: SmtpOutputSpec
func SmtpOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
oth := plugins_ts.NewOutputTestHelper(ctrl)
var wg sync.WaitGroup
inChan := make(chan *PipelinePack, 1)
pConfig := NewPipelineConfig(nil)
encoder := new(plugins.PayloadEncoder)
econfig := encoder.ConfigStruct().(*plugins.PayloadEncoderConfig)
econfig.AppendNewlines = false
encoder.Init(econfig)
c.Specify("A SmtpOutput", func() {
smtpOutput := new(SmtpOutput)
config := smtpOutput.ConfigStruct().(*SmtpOutputConfig)
config.SendTo = []string{"root"}
msg := pipeline_ts.GetTestMessage()
pack := NewPipelinePack(pConfig.InputRecycleChan())
pack.Message = msg
pack.Decoded = true
inChanCall := oth.MockOutputRunner.EXPECT().InChan().AnyTimes()
inChanCall.Return(inChan)
runnerName := oth.MockOutputRunner.EXPECT().Name().AnyTimes()
runnerName.Return("SmtpOutput")
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder).AnyTimes()
c.Specify("send email payload message", func() {
err := smtpOutput.Init(config)
c.Assume(err, gs.IsNil)
smtpOutput.sendFunction = testSendMail
outStr := "Write me out to the network"
pack.Message.SetPayload(outStr)
go func() {
wg.Add(1)
smtpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
wg.Done()
}()
inChan <- pack
close(inChan)
wg.Wait()
})
})
// Use this test with a real server
// c.Specify("Real SmtpOutput output", func() {
// smtpOutput := new(SmtpOutput)
//
// config := smtpOutput.ConfigStruct().(*SmtpOutputConfig)
// config.SendTo = []string{"root"}
//
// msg := pipeline_ts.GetTestMessage()
// pack := NewPipelinePack(pConfig.InputRecycleChan())
// pack.Message = msg
// pack.Decoded = true
// inChanCall := oth.MockOutputRunner.EXPECT().InChan().AnyTimes()
// inChanCall.Return(inChan)
// runnerName := oth.MockOutputRunner.EXPECT().Name().AnyTimes()
// runnerName.Return("SmtpOutput")
// oth.MockOutputRunner.EXPECT().Encoder().Return(encoder).AnyTimes()
//
// c.Specify("send a real email essage", func() {
//
// err := smtpOutput.Init(config)
// c.Assume(err, gs.IsNil)
//
// outStr := "Write me out to the network"
// pack.Message.SetPayload(outStr)
// go func() {
// wg.Add(1)
// smtpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
// wg.Done()
// }()
// inChan <- pack
// time.Sleep(1000) // allow time for the message output
// close(inChan)
// wg.Wait()
// // manually check the mail
// })
// })
}
示例13: UdpOutputSpec
func UdpOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
udpOutput := new(UdpOutput)
config := udpOutput.ConfigStruct().(*UdpOutputConfig)
oth := plugins_ts.NewOutputTestHelper(ctrl)
encoder := new(plugins.PayloadEncoder)
encoder.Init(new(plugins.PayloadEncoderConfig))
inChan := make(chan *pipeline.PipelinePack, 1)
rChan := make(chan *pipeline.PipelinePack, 1)
var wg sync.WaitGroup
var rAddr net.Addr
c.Specify("A UdpOutput", func() {
msg := pipeline_ts.GetTestMessage()
payload := "Write me out to the network."
msg.SetPayload(payload)
pack := pipeline.NewPipelinePack(rChan)
pack.Message = msg
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
oth.MockOutputRunner.EXPECT().UpdateCursor("").AnyTimes()
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().Encode(pack).Return(encoder.Encode(pack))
c.Specify("using UDP", func() {
addr := "127.0.0.1:45678"
config.Address = addr
ch := make(chan string, 1)
collectData := func() {
conn, err := net.ListenPacket("udp", addr)
if err != nil {
ch <- err.Error()
return
}
ch <- "ready"
b := make([]byte, 1000)
var n int
n, rAddr, _ = conn.ReadFrom(b)
ch <- string(b[:n])
conn.Close()
}
go collectData()
result := <-ch // Wait for server to be ready.
c.Assume(result, gs.Equals, "ready")
c.Specify("writes out to the network", func() {
err := udpOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = udpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
close(inChan)
wg.Wait()
})
c.Specify("uses the specified local address", func() {
config.LocalAddress = "localhost:12345"
err := udpOutput.Init(config)
c.Assume(err, gs.IsNil)
wg.Add(1)
go func() {
err = udpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
c.Expect(err, gs.IsNil)
wg.Done()
}()
inChan <- pack
result = <-ch
c.Expect(result, gs.Equals, payload)
c.Expect(rAddr.Network(), gs.Equals, "udp")
c.Expect(rAddr.String(), gs.Equals, "127.0.0.1:12345")
close(inChan)
wg.Wait()
})
})
c.Specify("using Unix datagrams", func() {
if runtime.GOOS == "windows" {
return
}
testUnixAddr := func() string {
//.........這裏部分代碼省略.........
示例14: CarbonOutputSpec
func CarbonOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
oth := NewCarbonTestHelper(ctrl)
var wg sync.WaitGroup
pConfig := NewPipelineConfig(nil)
// make test data
const count = 5
lines := make([]string, count)
baseTime := time.Now().UTC().Add(-10 * time.Second)
for i := 0; i < count; i++ {
statName := fmt.Sprintf("stats.name.%d", i)
statTime := baseTime.Add(time.Duration(i) * time.Second)
lines[i] = fmt.Sprintf("%s %d %d", statName, i*2, statTime.Unix())
}
submit_data := strings.Join(lines, "\n")
expected_data := submit_data + "\n"
// a helper to make new test packs
newpack := func() *PipelinePack {
msg := pipeline_ts.GetTestMessage()
pack := NewPipelinePack(pConfig.InputRecycleChan())
pack.Message = msg
pack.Decoded = true
pack.Message.SetPayload(submit_data)
return pack
}
// collectDataTCP and collectDataUDP functions; when ready reports its port on
// chPort, or error on chErr; when data is received it is reported on chData
collectDataTCP := func(chPort chan<- int, chData chan<- string, chError chan<- error) {
tcpaddr, err := net.ResolveTCPAddr("tcp", ":0")
if err != nil {
chError <- err
return
}
listener, err := net.ListenTCP("tcp", tcpaddr)
if err != nil {
chError <- err
return
}
chPort <- listener.Addr().(*net.TCPAddr).Port
conn, err := listener.Accept()
if err != nil {
chError <- err
return
}
b := make([]byte, 10000)
n, err := conn.Read(b)
if err != nil {
chError <- err
return
}
chData <- string(b[0:n])
}
collectDataUDP := func(chPort chan<- int, chData chan<- string, chError chan<- error) {
udpaddr, err := net.ResolveUDPAddr("udp", ":0")
if err != nil {
chError <- err
return
}
conn, err := net.ListenUDP("udp", udpaddr)
if err != nil {
chError <- err
return
}
chPort <- conn.LocalAddr().(*net.UDPAddr).Port
b := make([]byte, 10000)
n, _, err := conn.ReadFromUDP(b)
if err != nil {
chError <- err
return
}
chData <- string(b[0:n])
}
doit := func(protocol string, collectData collectFunc) {
c.Specify("A CarbonOutput ", func() {
inChan := make(chan *PipelinePack, 1)
carbonOutput := new(CarbonOutput)
config := carbonOutput.ConfigStruct().(*CarbonOutputConfig)
pack := newpack()
c.Specify("writes "+protocol+" to the network", func() {
inChanCall := oth.MockOutputRunner.EXPECT().InChan().AnyTimes()
inChanCall.Return(inChan)
chError := make(chan error, count)
chPort := make(chan int, count)
chData := make(chan string, count)
//.........這裏部分代碼省略.........
示例15: HttpOutputSpec
func HttpOutputSpec(c gs.Context) {
t := new(pipeline_ts.SimpleT)
ctrl := gomock.NewController(t)
defer ctrl.Finish()
var (
reqMethod string
reqBody string
reqHeader http.Header
handleWg sync.WaitGroup
runWg sync.WaitGroup
delay bool
)
handler := new(_test_handler)
handler.serveHttp = func(rw http.ResponseWriter, req *http.Request) {
defer handleWg.Done()
if delay {
time.Sleep(time.Duration(2) * time.Millisecond)
}
// Capture request body for test assertions.
p := make([]byte, req.ContentLength)
_, err := req.Body.Read(p)
c.Expect(err.Error(), gs.Equals, "EOF")
reqBody = string(p)
// Capture request method and headers for test assertions.
reqMethod = req.Method
reqHeader = req.Header
// Respond with either a response body or a response code.
if len(handler.respBody) > 0 {
rw.Write([]byte(handler.respBody))
} else {
rw.WriteHeader(handler.respCode)
}
flusher := rw.(http.Flusher)
flusher.Flush()
}
oth := ts.NewOutputTestHelper(ctrl)
encoder := new(plugins.PayloadEncoder)
encConfig := new(plugins.PayloadEncoderConfig)
err := encoder.Init(encConfig)
c.Expect(err, gs.IsNil)
inChan := make(chan *pipeline.PipelinePack, 1)
recycleChan := make(chan *pipeline.PipelinePack, 1)
pack := pipeline.NewPipelinePack(recycleChan)
pack.Message = pipeline_ts.GetTestMessage()
c.Specify("An HttpOutput", func() {
httpOutput := new(HttpOutput)
config := httpOutput.ConfigStruct().(*HttpOutputConfig)
c.Specify("barfs on bogus URLs", func() {
config.Address = "one-two-three-four"
err := httpOutput.Init(config)
c.Expect(err, gs.Not(gs.IsNil))
})
c.Specify("that is started", func() {
server := httptest.NewServer(handler)
defer server.Close()
runOutput := func() {
httpOutput.Run(oth.MockOutputRunner, oth.MockHelper)
runWg.Done()
}
oth.MockOutputRunner.EXPECT().Encoder().Return(encoder)
oth.MockOutputRunner.EXPECT().InChan().Return(inChan)
payload := "this is the payload"
pack.Message.SetPayload(payload)
oth.MockOutputRunner.EXPECT().Encode(gomock.Any()).Return(
[]byte(payload), nil)
config.Address = server.URL
handler.respBody = "Response Body"
c.Specify("makes http POST requests by default", func() {
err := httpOutput.Init(config)
c.Expect(err, gs.IsNil)
runWg.Add(1)
go runOutput()
handleWg.Add(1)
inChan <- pack
close(inChan)
handleWg.Wait()
runWg.Wait()
c.Expect(reqBody, gs.Equals, payload)
c.Expect(reqMethod, gs.Equals, "POST")
})
c.Specify("makes http PUT requests", func() {
config.Method = "put"
err := httpOutput.Init(config)
c.Expect(err, gs.IsNil)
runWg.Add(1)
go runOutput()
handleWg.Add(1)
inChan <- pack
close(inChan)
handleWg.Wait()
//.........這裏部分代碼省略.........