forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_writer_test.go
197 lines (153 loc) · 4.57 KB
/
socket_writer_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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package socket_writer
import (
"bufio"
"bytes"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"testing"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestSocketWriter_tcp(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "tcp://" + listener.Addr().String()
err = sw.Connect()
require.NoError(t, err)
lconn, err := listener.Accept()
require.NoError(t, err)
testSocketWriter_stream(t, sw, lconn)
}
func TestSocketWriter_udp(t *testing.T) {
listener, err := net.ListenPacket("udp", "127.0.0.1:0")
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "udp://" + listener.LocalAddr().String()
err = sw.Connect()
require.NoError(t, err)
testSocketWriter_packet(t, sw, listener)
}
func TestSocketWriter_unix(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "sw.TestSocketWriter_unix.sock")
listener, err := net.Listen("unix", sock)
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "unix://" + sock
err = sw.Connect()
require.NoError(t, err)
lconn, err := listener.Accept()
require.NoError(t, err)
testSocketWriter_stream(t, sw, lconn)
}
func TestSocketWriter_unixgram(t *testing.T) {
tmpdir, err := ioutil.TempDir("", "telegraf")
require.NoError(t, err)
defer os.RemoveAll(tmpdir)
sock := filepath.Join(tmpdir, "sw.TSW_unixgram.sock")
listener, err := net.ListenPacket("unixgram", sock)
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "unixgram://" + sock
err = sw.Connect()
require.NoError(t, err)
testSocketWriter_packet(t, sw, listener)
}
func testSocketWriter_stream(t *testing.T, sw *SocketWriter, lconn net.Conn) {
metrics := []telegraf.Metric{}
metrics = append(metrics, testutil.TestMetric(1, "test"))
mbs1out, _ := sw.Serialize(metrics[0])
metrics = append(metrics, testutil.TestMetric(2, "test"))
mbs2out, _ := sw.Serialize(metrics[1])
err := sw.Write(metrics)
require.NoError(t, err)
scnr := bufio.NewScanner(lconn)
require.True(t, scnr.Scan())
mstr1in := scnr.Text() + "\n"
require.True(t, scnr.Scan())
mstr2in := scnr.Text() + "\n"
assert.Equal(t, string(mbs1out), mstr1in)
assert.Equal(t, string(mbs2out), mstr2in)
}
func testSocketWriter_packet(t *testing.T, sw *SocketWriter, lconn net.PacketConn) {
metrics := []telegraf.Metric{}
metrics = append(metrics, testutil.TestMetric(1, "test"))
mbs1out, _ := sw.Serialize(metrics[0])
metrics = append(metrics, testutil.TestMetric(2, "test"))
mbs2out, _ := sw.Serialize(metrics[1])
err := sw.Write(metrics)
require.NoError(t, err)
buf := make([]byte, 256)
var mstrins []string
for len(mstrins) < 2 {
n, _, err := lconn.ReadFrom(buf)
require.NoError(t, err)
for _, bs := range bytes.Split(buf[:n], []byte{'\n'}) {
if len(bs) == 0 {
continue
}
mstrins = append(mstrins, string(bs)+"\n")
}
}
require.Len(t, mstrins, 2)
assert.Equal(t, string(mbs1out), mstrins[0])
assert.Equal(t, string(mbs2out), mstrins[1])
}
func TestSocketWriter_Write_err(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "tcp://" + listener.Addr().String()
err = sw.Connect()
require.NoError(t, err)
sw.Conn.(*net.TCPConn).SetReadBuffer(256)
lconn, err := listener.Accept()
require.NoError(t, err)
lconn.(*net.TCPConn).SetWriteBuffer(256)
metrics := []telegraf.Metric{testutil.TestMetric(1, "testerr")}
// close the socket to generate an error
lconn.Close()
sw.Conn.Close()
err = sw.Write(metrics)
require.Error(t, err)
assert.Nil(t, sw.Conn)
}
func TestSocketWriter_Write_reconnect(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
sw := newSocketWriter()
sw.Address = "tcp://" + listener.Addr().String()
err = sw.Connect()
require.NoError(t, err)
sw.Conn.(*net.TCPConn).SetReadBuffer(256)
lconn, err := listener.Accept()
require.NoError(t, err)
lconn.(*net.TCPConn).SetWriteBuffer(256)
lconn.Close()
sw.Conn = nil
wg := sync.WaitGroup{}
wg.Add(1)
var lerr error
go func() {
lconn, lerr = listener.Accept()
wg.Done()
}()
metrics := []telegraf.Metric{testutil.TestMetric(1, "testerr")}
err = sw.Write(metrics)
require.NoError(t, err)
wg.Wait()
assert.NoError(t, lerr)
mbsout, _ := sw.Serialize(metrics[0])
buf := make([]byte, 256)
n, err := lconn.Read(buf)
require.NoError(t, err)
assert.Equal(t, string(mbsout), string(buf[:n]))
}