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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
package gelf
import (
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"strings"
"testing"
"time"
)
func TestNewTCPWriter(t *testing.T) {
w, err := NewTCPWriter("")
if err == nil && w != nil {
t.Error("New didn't fail")
return
}
}
func TestNewTCPWriterConfig(t *testing.T) {
r, _, _, err := newTCPReader("127.0.0.1:0")
if err != nil {
t.Error("Could not open TCPReader")
return
}
w, err := NewTCPWriter(r.addr())
if err != nil {
t.Errorf("NewTCPWriter: %s", err)
return
}
if w.MaxReconnect != 3 {
t.Errorf("Default MaxReconnect: expected %d, got %d", 3, w.MaxReconnect)
return
}
w.MaxReconnect = 5
if w.MaxReconnect != 5 {
t.Errorf("Custom MaxReconnect: expected %d, got %d", 5, w.MaxReconnect)
return
}
if w.ReconnectDelay != 1 {
t.Errorf("Default ReconnectDelay: expected %d, got %d", 1, w.ReconnectDelay)
return
}
w.ReconnectDelay = 5
if w.ReconnectDelay != 5 {
t.Errorf("Custom ReconnectDelay: expected %d, got %d", 5, w.ReconnectDelay)
return
}
}
func assertMessages(msg *Message, short, full string, t *testing.T) {
if msg.Short != short {
t.Errorf("msg.Short: expected %s, got %s", short, msg.Short)
return
}
if msg.Full != full {
t.Errorf("msg.Full: expected %s, got %s", full, msg.Full)
}
}
func TestWriteSmallMultiLineTCP(t *testing.T) {
msgData := "awesomesauce\nbananas"
msg, err := sendAndRecvTCP(msgData)
if err != nil {
t.Errorf("sendAndRecvTCP: %s", err)
return
}
assertMessages(msg, "awesomesauce", msgData, t)
}
func TestWriteSmallOneLineTCP(t *testing.T) {
msgData := "some awesome thing\n"
msgDataTrunc := msgData[:len(msgData)-1]
msg, err := sendAndRecvTCP(msgData)
if err != nil {
t.Errorf("sendAndRecvTCP: %s", err)
return
}
assertMessages(msg, msgDataTrunc, "", t)
fileExpected := "/go-gelf/gelf/tcpwriter_test.go"
if !strings.HasSuffix(msg.Extra["_file"].(string), fileExpected) {
t.Errorf("msg.File: expected %s, got %s", fileExpected,
msg.Extra["_file"].(string))
return
}
if len(msg.Extra) != 2 {
t.Errorf("extra fields in %v (expect only file and line)", msg.Extra)
return
}
}
func TestWriteBigMessageTCP(t *testing.T) {
randData := make([]byte, 4096)
if _, err := rand.Read(randData); err != nil {
t.Errorf("cannot get random data: %s", err)
return
}
msgData := "awesomesauce\n" + base64.StdEncoding.EncodeToString(randData)
msg, err := sendAndRecvTCP(msgData)
if err != nil {
t.Errorf("sendAndRecv: %s", err)
return
}
assertMessages(msg, "awesomesauce", msgData, t)
}
func TestWriteMultiPacketMessageTCP(t *testing.T) {
randData := make([]byte, 150000)
if _, err := rand.Read(randData); err != nil {
t.Errorf("cannot get random data: %s", err)
return
}
msgData := "awesomesauce\n" + base64.StdEncoding.EncodeToString(randData)
msg, err := sendAndRecvTCP(msgData)
if err != nil {
t.Errorf("sendAndRecv: %s", err)
return
}
assertMessages(msg, "awesomesauce", msgData, t)
}
func TestExtraDataTCP(t *testing.T) {
// time.Now().Unix() seems fine, UnixNano() won't roundtrip
// through string -> float64 -> int64
extra := map[string]interface{}{
"_a": 10 * time.Now().Unix(),
"C": 9,
"_file": "writer_test.go",
"_line": 186,
}
short := "quick"
full := short + "\nwith more detail"
m := Message{
Version: "1.0",
Host: "fake-host",
Short: string(short),
Full: string(full),
TimeUnix: float64(time.Now().UnixNano()) / float64(time.Second),
Level: 6, // info
Facility: "writer_test",
Extra: extra,
RawExtra: []byte(`{"woo": "hoo"}`),
}
msg, err := sendAndRecvMsgTCP(&m)
if err != nil {
t.Errorf("sendAndRecvMsgTCP: %s", err)
return
}
assertMessages(msg, short, full, t)
if len(msg.Extra) != 3 {
t.Errorf("extra extra fields in %v", msg.Extra)
return
}
if int64(msg.Extra["_a"].(float64)) != extra["_a"].(int64) {
t.Errorf("_a didn't roundtrip (%v != %v)", int64(msg.Extra["_a"].(float64)), extra["_a"].(int64))
return
}
if string(msg.Extra["_file"].(string)) != extra["_file"] {
t.Errorf("_file didn't roundtrip (%v != %v)", msg.Extra["_file"].(string), extra["_file"].(string))
return
}
if int(msg.Extra["_line"].(float64)) != extra["_line"].(int) {
t.Errorf("_line didn't roundtrip (%v != %v)", int(msg.Extra["_line"].(float64)), extra["_line"].(int))
return
}
}
func TestWrite2MessagesWithConnectionDropTCP(t *testing.T) {
msgData1 := "First message\nThis happens before the connection drops"
msgData2 := "Second message\nThis happens after the connection drops"
msg1, msg2, err := sendAndRecv2MessagesWithDropTCP(msgData1, msgData2)
if err != nil {
t.Errorf("sendAndRecv2MessagesWithDropTCP: %s", err)
return
}
assertMessages(msg1, "First message", msgData1, t)
assertMessages(msg2, "Second message", msgData2, t)
}
func TestWrite2MessagesWithServerDropTCP(t *testing.T) {
msgData1 := "First message\nThis happens before the server drops"
msgData2 := "Second message\nThis happens after the server drops"
msg1, err := sendAndRecv2MessagesWithServerDropTCP(msgData1, msgData2)
if err != nil {
t.Errorf("sendAndRecv2MessagesWithDropTCP: %s", err)
return
}
assertMessages(msg1, "First message", msgData1, t)
}
func setupConnections() (*TCPReader, chan string, chan string, *TCPWriter, error) {
r, closeSignal, doneSignal, err := newTCPReader("127.0.0.1:0")
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("newTCPReader: %s", err)
}
w, err := NewTCPWriter(r.addr())
if err != nil {
return nil, nil, nil, nil, fmt.Errorf("NewTCPWriter: %s", err)
}
return r, closeSignal, doneSignal, w, nil
}
func sendAndRecvTCP(msgData string) (*Message, error) {
r, closeSignal, doneSignal, w, err := setupConnections()
if err != nil {
return nil, err
}
if _, err = w.Write([]byte(msgData)); err != nil {
return nil, fmt.Errorf("w.Write: %s", err)
}
closeSignal <- "stop"
done := <-doneSignal
if done != "done" {
return nil, errors.New("Wrong signal received")
}
message, err := r.readMessage()
if err != nil {
return nil, fmt.Errorf("r.readMessage: %s", err)
}
return message, nil
}
func sendAndRecvMsgTCP(msg *Message) (*Message, error) {
r, closeSignal, doneSignal, w, err := setupConnections()
if err != nil {
return nil, err
}
if err = w.WriteMessage(msg); err != nil {
return nil, fmt.Errorf("w.Write: %s", err)
}
closeSignal <- "stop"
done := <-doneSignal
if done != "done" {
return nil, errors.New("Wrong signal received")
}
w.Close()
message, err := r.readMessage()
if err != nil {
return nil, fmt.Errorf("r.readMessage: %s", err)
}
return message, nil
}
func sendAndRecv2MessagesWithDropTCP(msgData1 string, msgData2 string) (*Message, *Message, error) {
r, closeSignal, doneSignal, w, err := setupConnections()
if err != nil {
return nil, nil, err
}
if _, err = w.Write([]byte(msgData1)); err != nil {
return nil, nil, fmt.Errorf("w.Write: %s", err)
}
time.Sleep(200 * time.Millisecond)
closeSignal <- "drop"
done := <-doneSignal
if done != "done" {
return nil, nil, fmt.Errorf("Wrong signal received: %s", done)
}
message1, err := r.readMessage()
if err != nil {
return nil, nil, fmt.Errorf("readmessage: %s", err)
}
// Need to write twice to force the detection of the dropped connection
if _, err = w.Write([]byte(msgData2)); err != nil {
return nil, nil, fmt.Errorf("write 1 w.Write: %s", err)
}
time.Sleep(200 * time.Millisecond)
if _, err = w.Write([]byte(msgData2)); err != nil {
return nil, nil, fmt.Errorf("write 2 w.Write: %s", err)
}
time.Sleep(200 * time.Millisecond)
closeSignal <- "stop"
done = <-doneSignal
if done != "done" {
return nil, nil, errors.New("Wrong signal received")
}
message2, err := r.readMessage()
if err != nil {
return nil, nil, fmt.Errorf("readmessage: %s", err)
}
w.Close()
return message1, message2, nil
}
func sendAndRecv2MessagesWithServerDropTCP(msgData1 string, msgData2 string) (*Message, error) {
r, closeSignal, doneSignal, w, err := setupConnections()
if err != nil {
return nil, err
}
if _, err = w.Write([]byte(msgData1)); err != nil {
return nil, fmt.Errorf("w.Write: %s", err)
}
closeSignal <- "stop"
done := <-doneSignal
if done != "done" {
return nil, fmt.Errorf("Wrong signal received: %s", done)
}
message1, err := r.readMessage()
if err != nil {
return nil, fmt.Errorf("readmessage: %s", err)
}
// Need to write twice to force the detection of the dropped connection
// The first write will not cause an error, but the subsequent ones will
for {
_, err = w.Write([]byte(msgData2))
if err != nil {
break
}
}
w.Close()
return message1, nil
}
|