-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn_test.go
65 lines (55 loc) · 1.5 KB
/
conn_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
// SPDX-FileCopyrightText: 2020-2024 caixw
//
// SPDX-License-Identifier: MIT
package jsonrpc
import (
"context"
"io/ioutil"
"log"
"net"
"testing"
"time"
"github.com/issue9/assert/v4"
)
func TestConn_Serve(t *testing.T) {
a := assert.New(t, false)
srv := initServer(a)
srvConn, clientConn := net.Pipe()
srvExit := make(chan struct{}, 1)
srvCtx, srvCancel := context.WithCancel(context.Background())
go func() {
conn := srv.NewConn(NewSocketTransport(false, srvConn, 0), log.New(ioutil.Discard, "", 0))
err := conn.Serve(srvCtx)
a.Equal(err, context.Canceled)
srvExit <- struct{}{}
}()
time.Sleep(500 * time.Millisecond) // 等待服务启动完成
clientExit := make(chan struct{}, 1)
clientCtx, clientCancel := context.WithCancel(context.Background())
client := srv.NewConn(NewSocketTransport(false, clientConn, 0), nil)
go func() {
err := client.Serve(clientCtx)
a.Equal(err, context.Canceled)
clientExit <- struct{}{}
}()
time.Sleep(500 * time.Millisecond) // 等待服务启动完成
err := client.Notify("f1", &inType{Age: 18})
a.NotError(err)
err = client.Send("f1", &inType{Age: 18}, func(out *outType) error {
a.Equal(out.Age, 18)
return nil
})
a.NotError(err)
err = client.Send("f1", &inType{Age: 19, Last: "l"}, func(out *outType) error {
a.Equal(out.Age, 19).Equal(out.Name, "l")
return nil
})
a.NotError(err)
srvCancel()
clientCancel()
// 触发 srvCtx 的退出事件
//err = client.Notify("f1", &inType{})
//a.NotError(err)
<-srvExit
<-clientExit
}