This repository has been archived by the owner on Apr 8, 2024. It is now read-only.
forked from elodina/go_kafka_client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_test.go
79 lines (66 loc) · 2.75 KB
/
syslog_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
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package go_kafka_client
import (
"fmt"
"net"
"testing"
"time"
)
var tcpAddr = "0.0.0.0:5140"
var rfc5424message = "<34>1 2003-10-11T22:14:15.003Z localhost.stealth.ly su - ID23 - a simple message"
var rfc3164message = `<34>Jan 12 06:30:00 1.2.3.4 some_server: 1.2.3.4 - - [12/Jan/2011:06:29:59 +0100] "GET /foo/bar.html HTTP/1.1" 301 96 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12 ( .NET CLR 3.5.30729)" PID 18904 Time Taken 0`
//TODO any parser for this? what kind of RFC log/syslog is using at all?
//<PRI>TIMESTAMP HOSTNAME TAG[PID]: MSG
//<132>2014-12-30T11:51:47+02:00 localhost go_kafka_client[13128]: woohoo!
func TestSyslogProducer(t *testing.T) {
Logger = NewDefaultLogger(TraceLevel)
topic := fmt.Sprintf("syslog-producer-%d", time.Now().Unix())
consumeMessages := 100
timeout := 1 * time.Minute
consumeStatus := make(chan int)
CreateMultiplePartitionsTopic(localZk, topic, 1)
EnsureHasLeader(localZk, topic)
config := NewSyslogProducerConfig()
config.ProducerConfig = DefaultProducerConfig()
config.BrokerList = localBroker
config.ChannelSize = 5
config.NumProducers = 1
config.TCPAddr = tcpAddr
config.Topic = topic
syslogProducer := NewSyslogProducer(config)
go syslogProducer.Start()
time.Sleep(2 * time.Second)
for i := 0; i < 100; i++ {
logMessage()
}
consumerConfig := testConsumerConfig()
consumerConfig.Strategy = newCountingStrategy(t, consumeMessages, timeout, consumeStatus)
consumer := NewConsumer(consumerConfig)
go consumer.StartStatic(map[string]int{topic: 1})
if actual := <-consumeStatus; actual != consumeMessages {
t.Errorf("Failed to consume %d messages within %s. Actual messages = %d", consumeMessages, timeout, actual)
}
closeWithin(t, 10*time.Second, consumer)
syslogProducer.Stop()
}
//TODO use log/syslog somehow?
func logMessage() {
serverAddr, _ := net.ResolveTCPAddr("tcp", tcpAddr)
con, err := net.DialTCP("tcp", nil, serverAddr)
if err != nil {
panic(err)
}
con.Write([]byte(rfc5424message))
con.Close()
}