/
unixlistener_test.go
112 lines (89 loc) · 2.76 KB
/
unixlistener_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
package acomm_test
import (
"fmt"
"io/ioutil"
"net"
"os"
"testing"
"github.com/mistifyio/acomm"
"github.com/stretchr/testify/suite"
)
type UnixListenerTestSuite struct {
suite.Suite
Listener *acomm.UnixListener
Socket string
}
func (s *UnixListenerTestSuite) SetupTest() {
f, err := ioutil.TempFile("", "acommTest-")
s.Require().NoError(err, "failed to create temp socket")
s.Require().NoError(f.Close(), "failed to close temp socket file")
s.Require().NoError(os.Remove(f.Name()), "failed to remove temp socket file")
s.Socket = fmt.Sprintf("%s.sock", f.Name())
s.Listener = acomm.NewUnixListener(s.Socket, 0)
}
func (s *UnixListenerTestSuite) TearDownTest() {
s.Listener.Stop(0)
sConn := s.Listener.NextConn()
s.Nil(sConn)
}
func TestUnixListenerTestSuite(t *testing.T) {
suite.Run(t, new(UnixListenerTestSuite))
}
func (s *UnixListenerTestSuite) TestNewUnixListener() {
s.NotNil(acomm.NewUnixListener("foobar", 0), "should have returned a new UnixListener")
_ = s.Listener.Start()
}
func (s *UnixListenerTestSuite) TestAddr() {
s.Equal(s.Socket, s.Listener.Addr(), "should be the same addr")
_ = s.Listener.Start()
}
func (s *UnixListenerTestSuite) TestURL() {
s.Equal(fmt.Sprintf("unix://%s", s.Socket), s.Listener.URL().String(), "should be the same URL")
_ = s.Listener.Start()
}
func (s *UnixListenerTestSuite) TestStart() {
s.NoError(s.Listener.Start(), "should start successfully")
s.Error(s.Listener.Start(), "should error calling start again")
bad := acomm.NewUnixListener(s.Listener.Addr(), 0)
s.Error(bad.Start(), "should not be able to start on a used socket")
}
func (s *UnixListenerTestSuite) TestNextAndDoneConn() {
if !s.NoError(s.Listener.Start(), "should start successfully") {
return
}
addr, _ := net.ResolveUnixAddr("unix", s.Listener.Addr())
conn, err := net.DialUnix("unix", nil, addr)
if !s.NoError(err, "failed to dial listener") {
return
}
_, _ = conn.Write([]byte("foobar"))
_ = conn.Close()
lConn := s.Listener.NextConn()
if !s.NotNil(lConn, "connection should not be nil") {
return
}
s.Listener.DoneConn(lConn)
}
func (s *UnixListenerTestSuite) TestSendAndUnmarshalConnData() {
if !s.NoError(s.Listener.Start(), "should start successfully") {
return
}
in := map[string]string{
"foo": "bar",
}
addr, _ := net.ResolveUnixAddr("unix", s.Listener.Addr())
conn, err := net.DialUnix("unix", nil, addr)
if !s.NoError(err, "failed to dial listener") {
return
}
_ = acomm.SendConnData(conn, in)
_ = conn.Close()
lConn := s.Listener.NextConn()
if !s.NotNil(lConn, "connection should not be nil") {
return
}
out := map[string]string{}
s.NoError(acomm.UnmarshalConnData(lConn, &out), "should succeed unmarshalling")
s.Equal(in, out, "should have unmarshaled the correct data")
s.Listener.DoneConn(lConn)
}