forked from emiago/diago
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiago_test_utils.go
56 lines (45 loc) · 1.08 KB
/
diago_test_utils.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
package diago
import (
"context"
"net"
"sync/atomic"
"github.com/emiago/sipgo/sip"
"github.com/rs/zerolog/log"
)
type connRecorder struct {
msgs []sip.Message
ref atomic.Int32
}
func NewConnRecorder() *connRecorder {
return &connRecorder{}
}
func (c *connRecorder) LocalAddr() net.Addr {
return nil
}
func (c *connRecorder) WriteMsg(msg sip.Message) error {
c.msgs = append(c.msgs, msg)
return nil
}
func (c *connRecorder) Ref(i int) int {
return int(c.ref.Add(int32(i)))
}
func (c *connRecorder) TryClose() (int, error) {
new := c.ref.Add(int32(-1))
return int(new), nil
}
func (c *connRecorder) Close() error { return nil }
type clientTxRequester struct {
// rec *siptest.ClientTxRecorder
onRequest func(req *sip.Request) *sip.Response
}
func (r *clientTxRequester) Request(ctx context.Context, req *sip.Request) (sip.ClientTransaction, error) {
key, _ := sip.MakeClientTxKey(req)
rec := NewConnRecorder()
tx := sip.NewClientTx(key, req, rec, log.Logger)
if err := tx.Init(); err != nil {
return nil, err
}
resp := r.onRequest(req)
go tx.Receive(resp)
return tx, nil
}