forked from ajankovic/smpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
99 lines (94 loc) · 2.15 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 smpp_test
import (
"context"
"log"
"testing"
"time"
"github.com/ajankovic/smpp"
"github.com/ajankovic/smpp/pdu"
)
const (
TestAddr = ":30303"
)
func TestSMPPServer(t *testing.T) {
sessConf := smpp.SessionConf{
Handler: smpp.HandlerFunc(func(ctx *smpp.Context) {
switch ctx.CommandID() {
case pdu.BindTransceiverID:
btrx, err := ctx.BindTRx()
if err != nil {
t.Errorf(err.Error())
}
resp := btrx.Response("TestingServer")
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
t.Errorf(err.Error())
}
}
}),
}
srv := smpp.NewServer(TestAddr, sessConf)
go func() {
err := srv.ListenAndServe()
if err != nil {
t.Errorf("Expected no error on server close %v", err)
}
}()
time.Sleep(time.Millisecond * 10)
sess1 := bindToServer(TestAddr, smpp.HandlerFunc(func(ctx *smpp.Context) {
switch ctx.CommandID() {
case pdu.UnbindID:
ubd, err := ctx.Unbind()
if err != nil {
t.Errorf(err.Error())
}
resp := ubd.Response()
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
t.Errorf(err.Error())
}
}
}))
sess2 := bindToServer(TestAddr, smpp.HandlerFunc(func(ctx *smpp.Context) {
switch ctx.CommandID() {
case pdu.UnbindID:
ubd, err := ctx.Unbind()
if err != nil {
t.Errorf(err.Error())
}
resp := ubd.Response()
if err := ctx.Respond(resp, pdu.StatusOK); err != nil {
t.Errorf(err.Error())
}
}
}))
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
defer cancel()
err := srv.Unbind(ctx)
if err != nil {
t.Error(err.Error())
}
select {
case <-sess1.NotifyClosed():
case <-time.After(100 * time.Millisecond):
t.Errorf("session %s was not closed in time", sess1)
}
select {
case <-sess2.NotifyClosed():
case <-time.After(100 * time.Millisecond):
t.Errorf("session %s was not closed in time", sess2)
}
}
func bindToServer(bind string, hf smpp.HandlerFunc) *smpp.Session {
bc := smpp.BindConf{
Addr: bind,
SystemID: "Client",
Password: "password",
}
sc := smpp.SessionConf{
Handler: hf,
}
sess, err := smpp.BindTRx(sc, bc)
if err != nil {
log.Fatalf("error during bind %v", err)
}
return sess
}