-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdialer_test.go
More file actions
46 lines (44 loc) · 977 Bytes
/
dialer_test.go
File metadata and controls
46 lines (44 loc) · 977 Bytes
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
package socket
import (
"fmt"
"net"
"testing"
"time"
)
func TestConnection(t *testing.T) {
dialer := NewSocketDialer(SocketConfig{
Network: "tcp",
RemoteAddress: "127.0.0.1:10700",
RetryMax: 10,
RetryDelay: time.Second,
}, WithOpenFunc(func(conn net.Conn, config SocketConfig) error {
fmt.Println("==> On Opened")
return nil
}), WithErrorFunc(func(conn *SocketDialer, err error) (continued bool) {
fmt.Println("==> On Error: " + err.Error())
return true
}), WithReadFunc(func(conn net.Conn) error {
meta := make([]byte, 1024)
n, err := conn.Read(meta)
if err != nil {
return err
}
fmt.Println("==> On Read: " + string(meta[:n]))
return nil
}))
dialer.SetCloseFunc(func(conn net.Conn) {
dialer.ResetState()
})
ticker := time.NewTicker(time.Second * 5)
defer ticker.Stop()
go func() {
for {
<-ticker.C
dialer.Close()
}
}()
time.AfterFunc(time.Minute*5, func() {
dialer.Shutdown()
})
dialer.Serve()
}