forked from bit4bit/gami
-
Notifications
You must be signed in to change notification settings - Fork 2
/
gami_test.go
181 lines (159 loc) · 3.49 KB
/
gami_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package gami
import (
"bytes"
"fmt"
"math/rand"
"net"
"net/textproto"
"sync"
"testing"
"time"
)
type amiMockAction func(params textproto.MIMEHeader) map[string]string
//amiServer for mocking Asterisk AMI
type amiServer struct {
Addr string
actionsMocked map[string]amiMockAction
listener net.Listener
}
func TestLogin(t *testing.T) {
srv := newAmiServer()
defer srv.Close()
ami, err := Dial(srv.Addr)
if err != nil {
t.Fatal(err)
}
go ami.Run()
defer ami.Close()
defaultInstaller(t, ami)
//example mocking login of asterisk
srv.Mock("Login", func(params textproto.MIMEHeader) map[string]string {
return map[string]string{
"Response": "OK",
"ActionID": params.Get("Actionid"),
}
})
ami.Login("admin", "admin")
}
func TestMultiAsyncActions(t *testing.T) {
srv := newAmiServer()
defer srv.Close()
ami, err := Dial(srv.Addr)
if err != nil {
t.Fatal(err)
}
go ami.Run()
defer ami.Close()
defaultInstaller(t, ami)
tests := 10
workers := 5
wg := &sync.WaitGroup{}
for ti := tests; ti > 0; ti-- {
resWorkers := make(chan (<-chan *AMIResponse), workers)
for i := 0; i < workers; i++ {
wg.Add(1)
go func() {
chres, err := ami.AsyncAction("Test", nil)
if err != nil {
t.Error(err)
}
resWorkers <- chres
wg.Done()
}()
}
go func() {
wg.Wait()
close(resWorkers)
}()
for resp := range resWorkers {
select {
case <-time.After(time.Second * 5):
t.Fatal("asyncAction locked")
case <-resp:
}
}
}
}
func defaultInstaller(t *testing.T, ami *AMIClient) {
go func() {
for {
select {
//handle network errors
case err := <-ami.NetError:
t.Error("Network Error:", err)
case err := <-ami.Error:
t.Error("error:", err)
//wait events and process
case <-ami.Events:
//t.Log("Event:", *ev)
}
}
}()
}
func newAmiServer() *amiServer {
addr := "localhost:0"
listener, err := net.Listen("tcp", addr)
if err != nil {
panic(err)
}
srv := &amiServer{Addr: listener.Addr().String(),
listener: listener,
actionsMocked: make(map[string]amiMockAction)}
go srv.do(listener)
return srv
}
//Mock
func (c *amiServer) Mock(action string, cb amiMockAction) {
c.actionsMocked[action] = cb
}
func (c *amiServer) do(listener net.Listener) {
for {
conn, err := listener.Accept()
if err != nil {
return
}
fmt.Fprintf(conn, "Asterisk Call Manager\r\n")
tconn := textproto.NewConn(conn)
//install event HeartBeat
go func(conn *textproto.Conn) {
for now := range time.Tick(time.Second) {
fmt.Fprintf(conn.W, "Event: HeartBeat\r\nTime: %d\r\n\r\n",
now.Unix())
}
}(tconn)
go func(conn *textproto.Conn) {
defer conn.Close()
for {
header, err := conn.ReadMIMEHeader()
if err != nil {
return
}
var output bytes.Buffer
time.AfterFunc(time.Millisecond*time.Duration(rand.Intn(1000)), func() {
if _, ok := c.actionsMocked[header.Get("Action")]; ok {
rvals := c.actionsMocked[header.Get("Action")](header)
for k, vals := range rvals {
fmt.Fprintf(&output, "%s: %s\r\n", k, vals)
}
output.WriteString("\r\n")
err := conn.PrintfLine(output.String())
if err != nil {
panic(err)
}
} else {
//default response
fmt.Fprintf(&output, "Response: TEST\r\nActionID: %s\r\n\r\n",
header.Get("Actionid"))
err := conn.PrintfLine(output.String())
if err != nil {
panic(err)
}
}
})
}
}(tconn)
}
}
func (c *amiServer) Close() {
c.listener.Close()
}