-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
99 lines (89 loc) · 1.95 KB
/
server_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
package sylph_test
import (
"fmt"
"testing"
"time"
"github.com/tkmn0/sylph"
"github.com/tkmn0/sylph/pkg/channel"
)
func TestServerConnection(test *testing.T) {
fmt.Println("TestServerConnection")
closeCh := make(chan bool)
s := sylph.NewServer("127.0.0.1", 4444)
defer s.Close()
s.OnTransport = func(t sylph.Transport) {
fmt.Println("server on transport")
t.OnChannel(func(c channel.Channel) {
fmt.Println("server on channel")
c.OnMessage(func(m string) {
fmt.Println("server on message", m)
})
})
}
go s.Run()
c := sylph.NewClient()
c.OnTransport = func(t sylph.Transport) {
fmt.Println("client on transport")
t.OnChannel(func(c channel.Channel) {
fmt.Println("client on channel")
counter := 0
loop:
for {
time.Sleep(time.Millisecond)
c.SendMessage("hello")
counter++
if counter > 3 {
close(closeCh)
break loop
}
}
})
t.OpenChannel()
}
c.Connect("127.0.0.1", 4444)
{
<-closeCh
s.Close()
time.Sleep(time.Second * 5)
fmt.Println("closed test")
}
}
func TestChannelClose(test *testing.T) {
fmt.Println("TestChannelClose")
s := sylph.NewServer("127.0.0.1", 4444)
s.OnTransport = func(t sylph.Transport) {
fmt.Println("server on transport")
t.OnChannel(func(c channel.Channel) {
fmt.Println("server on channel")
c.OnMessage(func(m string) {
fmt.Println("server on message", m)
})
})
}
go s.Run()
c := sylph.NewClient()
c.OnTransport = func(t sylph.Transport) {
fmt.Println("client on transport")
t.OnChannel(func(c channel.Channel) {
fmt.Println("client on channel")
counter := 0
loop:
for {
time.Sleep(time.Millisecond)
c.SendMessage("hello")
counter++
if counter > 3 {
c.Close()
break loop
}
}
})
if err := t.OpenChannel(); err != nil {
fmt.Println("open first channel erorr")
}
if err := t.OpenChannel(); err != nil {
fmt.Println("open second channel erorr", err)
}
}
c.Connect("127.0.0.1", 4444)
}