Skip to content

Commit cd144ab

Browse files
committed
Adding server tests
1 parent 8eee8b5 commit cd144ab

File tree

3 files changed

+56
-36
lines changed

3 files changed

+56
-36
lines changed

client_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func (n *Nooper) OnDisconnect(con *Connection) {
2424

2525
func (n *Nooper) OnEvent(con *Connection, evt *Event) {
2626
fmt.Println("event", evt.EventBody.Get("Event-Name"))
27+
con.Send("quit")
2728
}
2829

2930
func (n *Nooper) OnClose(con *Connection) {
@@ -43,7 +44,9 @@ func Test_Client(t *testing.T) {
4344
if err != nil {
4445
fmt.Println("Something went wrong ", err)
4546
}
47+
if client != nil {
48+
}
4649

47-
client.Loop()
50+
// client.Loop()
4851

4952
}

net.go

+11-15
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package eventsocket
33
import (
44
"bufio"
55
"bytes"
6-
"fmt"
6+
_ "fmt"
77
"net"
88
"sync"
99
"time"
@@ -43,7 +43,7 @@ type Connection struct {
4343
}
4444

4545
type Server struct {
46-
Settings *ServerSettings
46+
Settings ServerSettings
4747
NetListener net.Listener
4848
EvListener ServerListener
4949
}
@@ -91,7 +91,7 @@ func (con *Connection) tryConnect(client *Client) bool {
9191
return true
9292
}
9393

94-
func (con *Connection) setupConnect() {
94+
func (con *Connection) setupConnect() bool {
9595
//authenticate socket
9696
cBuf := bytes.NewBufferString("connect")
9797
cBuf.Write(doubleLine)
@@ -100,25 +100,25 @@ func (con *Connection) setupConnect() {
100100

101101
if err != nil {
102102
con.eslCon.Close()
103-
return
103+
return false
104104
}
105105

106106
channelData := readMessage(con.rw)
107107

108108
if channelData.Type != EventReply {
109109
con.eslCon.Close()
110-
return
110+
return false
111111
}
112112

113113
con.ChannelData = channelData.Headers
114114
con.Connected = true
115+
return true
115116
}
116117

117118
func (con *Connection) Loop() {
118119
//spin this on a separate goroutine so we can start handling api events
119120
//people will likely subscribe to events here
120121
go con.Listener.OnConnect(con)
121-
fmt.Println("Connection Loop")
122122
for con.Connected {
123123
message := readMessage(con.rw)
124124

@@ -140,12 +140,11 @@ func (con *Connection) Loop() {
140140
}
141141

142142
}
143-
fmt.Println("Close Connection Loop")
144143
}
145144

146145
/* Server */
147146

148-
func CreateServer(settings *ServerSettings) (*Server, error) {
147+
func CreateServer(settings ServerSettings) (*Server, error) {
149148
var err error
150149
retServer := new(Server)
151150
retServer.Settings = settings
@@ -155,10 +154,9 @@ func CreateServer(settings *ServerSettings) (*Server, error) {
155154
if err != nil {
156155
return nil, err
157156
}
158-
go retServer.loop()
159157
return retServer, nil
160158
}
161-
func (server *Server) loop() {
159+
func (server *Server) Loop() {
162160
for {
163161
conn, err := server.NetListener.Accept()
164162
if err != nil {
@@ -172,7 +170,9 @@ func (server *Server) loop() {
172170
scon.rw = bufio.NewReadWriter(
173171
bufio.NewReaderSize(scon.eslCon, BufferSize),
174172
bufio.NewWriter(scon.eslCon))
175-
server.EvListener.OnNewConnection(scon)
173+
if scon.setupConnect() {
174+
server.EvListener.OnNewConnection(scon)
175+
}
176176
}
177177
}
178178

@@ -201,17 +201,13 @@ func CreateClient(settings ClientSettings) (*Client, error) {
201201
func (client *Client) Loop() {
202202
for {
203203
if !client.Connected {
204-
fmt.Println("Trying to reconnect")
205204
client.Connected = client.connection.tryConnect(client)
206205
if !client.Connected {
207206
time.Sleep(client.Settings.Timeout)
208207
}
209-
fmt.Println("Finished trying to reconnect", client.Connected)
210208
continue
211209
}
212-
fmt.Println("Client Loop")
213210
client.connection.Loop()
214-
fmt.Println("Client Loop Finished")
215211
client.Connected = false
216212
}
217213
}

server_test.go

+41-20
Original file line numberDiff line numberDiff line change
@@ -5,33 +5,54 @@ import (
55
"testing"
66
)
77

8-
func Test_Server(t *testing.T) {
9-
t.Log("Test Basic Server")
10-
11-
server, err := CreateServer(&ServerSettings{
12-
Address: "localhost:8086",
13-
})
8+
type ServerNooper struct{}
149

10+
func (n *ServerNooper) OnConnect(con *Connection) {
11+
fmt.Println("Connected")
12+
evt, err := con.Send("event", "plain", "HEARTBEAT")
1513
if err != nil {
16-
fmt.Println("Something went wrong ", err)
14+
fmt.Println("ahn?")
15+
return
1716
}
17+
fmt.Println(evt.Success)
18+
19+
con.Execute(&Command{
20+
App: "answer",
21+
Args: "",
22+
})
23+
}
1824

19-
fmt.Println("Listening on", server.Settings.Address)
25+
func (n *ServerNooper) OnDisconnect(con *Connection) {
26+
fmt.Println("disconnect")
27+
}
2028

21-
for loop := true; loop; {
22-
msg := <-server.EventsChannel
23-
switch {
24-
case msg.Type == EventState && !msg.Success:
25-
fmt.Println("Disconnected")
26-
loop = false
27-
case msg.Type == EventState && msg.Success:
28-
fmt.Println("Connected")
29-
fmt.Println("Answering")
30-
msg.Connection.Answer()
29+
func (n *ServerNooper) OnEvent(con *Connection, evt *Event) {
30+
fmt.Println("event", evt.EventBody.Get("Event-Name"))
31+
}
3132

32-
case msg.Type == EventGeneric:
33+
func (n *ServerNooper) OnClose(con *Connection) {
34+
fmt.Println("close")
35+
}
3336

34-
}
37+
func (n *ServerNooper) OnNewConnection(con *Connection) {
38+
fmt.Println("Got new connection")
39+
con.Listener = n
40+
go con.Loop()
41+
}
42+
43+
func Test_Server(t *testing.T) {
44+
fmt.Println("Test Basic Server")
45+
46+
server, err := CreateServer(ServerSettings{
47+
Address: "localhost:8087",
48+
Listener: new(ServerNooper),
49+
})
50+
51+
if err != nil {
52+
fmt.Println("Something went wrong ", err)
3553
}
54+
fmt.Println("Listening")
55+
56+
server.Loop()
3657

3758
}

0 commit comments

Comments
 (0)