-
Notifications
You must be signed in to change notification settings - Fork 3
/
dsn_test.go
174 lines (147 loc) · 4.63 KB
/
dsn_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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
//
// 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 dsn_test
import (
"fmt"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"trpc.group/trpc-go/trpc-go/naming/registry"
"trpc.group/trpc-go/trpc-go/naming/selector"
dsn "trpc.group/trpc-go/trpc-selector-dsn"
)
func TestDsn(t *testing.T) {
s := selector.Get("dsn")
assert.NotNil(t, s)
node, err := s.Select("user:passwd@tcp(ip:port)/db")
assert.Nil(t, err)
assert.Equal(t, "user:passwd@tcp(ip:port)/db", node.Address)
_, err = s.Select("")
assert.EqualErrorf(t, err, "dsn address can not be empty", "empty src test")
err = s.Report(nil, time.Second, nil)
assert.Nil(t, err)
}
type FakeSelector struct {
Addrs map[string]string
}
func (s *FakeSelector) Select(serviceName string, opt ...selector.Option) (*registry.Node, error) {
addr, ok := s.Addrs[serviceName]
if !ok {
return nil, fmt.Errorf("unknown service name %s", serviceName)
}
return ®istry.Node{
Address: addr,
ServiceName: serviceName,
}, nil
}
func (s *FakeSelector) Report(node *registry.Node, cost time.Duration, success error) error {
node.Metadata = map[string]interface{}{
"reported": node,
}
return nil
}
type serviceNameExtractor struct {
}
func (e *serviceNameExtractor) Extract(dsn string) (int, int, error) {
idx := strings.Index(dsn, "@")
if idx+1 >= len(dsn) {
return 0, 0, fmt.Errorf("extract service name failed, src is %s", dsn)
}
pos := idx + 1
length := len(dsn) - pos
return pos, length, nil
}
func TestResolvableSelector(t *testing.T) {
assert := assert.New(t)
selector.Register("empty", dsn.NewResolvableSelector("", &serviceNameExtractor{}))
selector.Register("noextractor", dsn.NewResolvableSelector("test", nil))
selector.Register("fake", &FakeSelector{Addrs: map[string]string{"abc": "127.0.0.1:8080"}})
selector.Register("dsn+fake", dsn.NewResolvableSelector("fake", &serviceNameExtractor{}))
s := selector.Get("dsn+fake")
assert.NotNil(t, s)
cases := []struct {
src string
dest string
err string
}{
{
src: "@abc",
dest: "@127.0.0.1:8080",
},
{
src: "abc@",
dest: "",
err: "extract service name failed, src is abc@",
},
{
src: "@abc/",
dest: "@",
err: "unknown service name abc/",
},
}
for _, c := range cases {
node, err := s.Select(c.src)
if len(c.err) == 0 {
assert.Nilf(err, "case: %+v", c)
assert.Equalf(c.dest, node.Address, "case: %+v", c)
assert.Nilf(s.Report(node, node.CostTime, nil), "case: %+v", c)
resolved, _ := node.Metadata["resolved"].(*registry.Node)
assert.Equalf(resolved.Metadata["reported"], resolved, "case: %+v", c)
} else {
assert.EqualErrorf(err, c.err, "case: %+v", c)
}
}
// compare with no cache and cache, hit cache, same node
src := "user@abc"
dest := "[email protected]:8080"
nodeWithNoCache, err := s.Select(src)
assert.Nil(err)
assert.Equal(dest, nodeWithNoCache.Address)
nodeWithCache, err := s.Select(src)
assert.Nil(err)
assert.True(nodeWithCache == nodeWithNoCache)
emptyS := selector.Get("empty")
_, err = emptyS.Select("123")
assert.EqualError(err, "resolver selector name can not be empty", "case: empty")
noExtractorS := selector.Get("noextractor")
_, err = noExtractorS.Select("123")
assert.EqualError(err, "service name extractor can not be nil", "case: empty")
}
func TestUnresolvedAddress(t *testing.T) {
n, err := dsn.NewDsnSelector(true).Select(t.Name())
require.Nil(t, err)
require.NotNil(t, n.ParseAddr)
addr := n.ParseAddr(n.Network, n.Address)
require.Equal(t, t.Name(), addr.String())
require.Equal(t, "", addr.Network())
}
func TestResolvableSelectorParseAddr(t *testing.T) {
target := "root:root1234@tcp(127.0.0.1:3306)/redis_db?timeout=1s"
// old selector
n, err := dsn.NewResolvableSelector("dsn", &dsn.URIHostExtractor{}).Select(target)
require.Nil(t, err)
require.Nil(t, n.ParseAddr)
// new selector with parseaddr
n, err = dsn.NewResolvableSelectorWithOpts("dsn").Select(target)
require.Nil(t, err)
require.NotNil(t, n.ParseAddr)
addr := n.ParseAddr(n.Network, n.Address)
require.Equal(t, "127.0.0.1:3306", addr.String())
require.Equal(t, "", addr.Network())
// new selector disable parseaddr
n, err = dsn.NewResolvableSelectorWithOpts("dsn", dsn.WithEnableParseAddr(false)).Select(target)
require.Nil(t, err)
require.Nil(t, n.ParseAddr)
}