-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtcpservice_test.go
79 lines (69 loc) · 1.73 KB
/
tcpservice_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
//
//
// Tencent is pleased to support the open source community by making tRPC available.
//
// Copyright (C) 2023 THL A29 Limited, a Tencent company.
// All rights reserved.
//
// If you have downloaded a copy of the tRPC source code from Tencent,
// please note that tRPC source code is licensed under the Apache 2.0 License,
// A copy of the Apache 2.0 License is included in this file.
//
//
package tnet_test
import (
"context"
"net"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"trpc.group/trpc-go/tnet"
)
func TestTCPServiceNoListener(t *testing.T) {
_, err := tnet.NewTCPService(nil, nil)
assert.NotNil(t, err)
}
func TestTCPServiceAcceptAndCancel(t *testing.T) {
ln, err := tnet.Listen("tcp", "127.0.0.1:9999")
assert.Nil(t, err)
s, err := tnet.NewTCPService(ln, nil)
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
s.Serve(ctx)
wg.Done()
}()
time.Sleep(time.Millisecond * 5)
assert.NotNil(t, s.Serve(ctx))
client, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
assert.Nil(t, err)
defer client.Close()
time.Sleep(time.Millisecond * 5)
cancel()
wg.Wait()
}
func TestTCPServiceExternalListener(t *testing.T) {
ln, err := net.Listen("tcp", "127.0.0.1:9999")
assert.Nil(t, err)
defer ln.Close()
s, err := tnet.NewTCPService(ln, nil)
assert.Nil(t, err)
ctx, cancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
s.Serve(ctx)
wg.Done()
}()
time.Sleep(time.Millisecond * 5)
assert.NotNil(t, s.Serve(ctx))
client, err := net.Dial(ln.Addr().Network(), ln.Addr().String())
assert.Nil(t, err)
defer client.Close()
time.Sleep(time.Millisecond * 5)
cancel()
wg.Wait()
}