-
Notifications
You must be signed in to change notification settings - Fork 23
/
handler_socket_test.go
52 lines (48 loc) · 1.19 KB
/
handler_socket_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
package logging
import (
"container/list"
"encoding/gob"
"fmt"
"github.com/hhkbp2/testify/require"
"net"
"testing"
)
func _testSetupSocketServer(
t *testing.T, host string, port uint16, received *list.List, ch chan int) {
address := fmt.Sprintf("%s:%d", host, port)
listener, err := net.Listen("tcp", address)
require.Nil(t, err)
go func() {
conn, err := listener.Accept()
require.Nil(t, err)
defer listener.Close()
go func(conn net.Conn) {
defer conn.Close()
decoder := gob.NewDecoder(conn)
var record SocketLogRecord
err := decoder.Decode(&record)
require.Nil(t, err)
received.PushBack(record.Message)
ch <- 1
}(conn)
}()
}
func TestSocketHandler(t *testing.T) {
host := "127.0.0.1"
port := uint16(8082)
serverReceived := list.New()
ch := make(chan int)
_testSetupSocketServer(t, host, port, serverReceived, ch)
require.Equal(t, 0, serverReceived.Len())
handler := NewSocketHandler(host, port)
logger := GetLogger("socket")
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)
}