-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_datagram_test.go
53 lines (49 loc) · 1.25 KB
/
handler_datagram_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
package logging
import (
"bytes"
"container/list"
"encoding/gob"
"fmt"
"github.com/hhkbp2/testify/require"
"net"
"testing"
)
func _testSetupDatagramServer(
t *testing.T, host string, port uint16, received *list.List, ch chan int) {
address, err := net.ResolveUDPAddr("udp", fmt.Sprintf("%s:%d", host, port))
require.Nil(t, err)
conn, err := net.ListenUDP("udp", address)
require.Nil(t, err)
go func() {
bin := make([]byte, 1024)
n, _, err := conn.ReadFromUDP(bin)
require.Nil(t, err)
defer conn.Close()
buf := bytes.NewBuffer(bin[:n])
decoder := gob.NewDecoder(buf)
var record SocketLogRecord
err = decoder.Decode(&record)
require.Nil(t, err)
received.PushBack(record.Message)
ch <- 1
}()
}
func TestDatagramHandler(t *testing.T) {
host := "127.0.0.1"
port := uint16(8082)
serverReceived := list.New()
ch := make(chan int)
_testSetupDatagramServer(t, host, port, serverReceived, ch)
require.Equal(t, 0, serverReceived.Len())
handler := NewDatagramHandler(host, port)
logger := GetLogger("datagram")
logger.AddHandler(handler)
message := "test"
logger.Errorf(message)
handler.Close()
<-ch
require.Equal(t, 1, serverReceived.Len())
m, ok := serverReceived.Front().Value.(string)
require.True(t, ok)
require.Equal(t, message, m)
}