-
Notifications
You must be signed in to change notification settings - Fork 0
/
inproc_test.go
76 lines (72 loc) · 1.37 KB
/
inproc_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
// Copyright (c) 2021 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package inproc
import (
"net"
"sync"
"testing"
"time"
)
func TestINPROC(t *testing.T) {
address := ":9999"
if _, err := Dial(address); err == nil {
t.Error(err)
}
l, err := Listen(address)
if err != nil {
t.Error(err)
}
if _, err := Listen(address); err == nil {
t.Error()
}
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
for {
conn, err := l.Accept()
if err != nil {
return
}
wg.Add(1)
go func(conn net.Conn) {
defer wg.Done()
buf := make([]byte, 1024)
for {
n, err := conn.Read(buf)
if err != nil {
break
}
conn.Write(buf[:n])
}
conn.Close()
}(conn)
}
}()
conn, err := Dial(address)
if err != nil {
t.Error(err)
}
conn.SetWriteDeadline(time.Now().Add(time.Second))
conn.SetReadDeadline(time.Now().Add(time.Second))
conn.SetDeadline(time.Now().Add(time.Second))
conn.LocalAddr()
raddr := conn.RemoteAddr()
raddr.Network()
raddr.String()
str := "Hello World"
conn.Write([]byte(str))
buf := make([]byte, 1024)
n, err := conn.Read(buf)
if err != nil {
t.Error(err)
}
if string(buf[:n]) != str {
t.Errorf("error %s != %s", string(buf[:n]), str)
}
time.Sleep(time.Millisecond)
conn.Close()
l.Addr()
l.Close()
wg.Wait()
}