11
. "mosn.io/mosn/test/framework"
12
"mosn.io/mosn/test/lib"
13
"mosn.io/mosn/test/lib/http"
14
"mosn.io/mosn/test/lib/xprotocol/boltv1"
15
"mosn.io/mosn/test/lib/xprotocol/boltv2"
18
const MockBoltV2ServerConfig = `{
21
"address": "127.0.0.1:8080"
25
const MockHttp2ServerConfig = `{
28
"address": "127.0.0.1:8080"
32
func TestOneProtocolConfig(t *testing.T) {
33
Scenario(t, "downstream protocol config to bolt", func() {
34
lib.InitMosn(CreateConfigByProtocol("bolt"), lib.CreateConfig(MockBoltServerConfig))
36
client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
37
TargetAddr: "127.0.0.1:2045",
38
Verify: &boltv1.VerifyConfig{
39
ExpectedStatusCode: 0,
42
Verify(client.SyncCall(), Equal, true)
43
stats := client.Stats()
44
Verify(stats.Requests(), Equal, 1)
45
Verify(stats.ExpectedResponseCount(), Equal, 1)
46
Verify(stats.ConnectionClosed(), Equal, 0)
49
Scenario(t, "downstream protocol config to boltv2", func() {
50
lib.InitMosn(CreateConfigByProtocol("boltv2"), lib.CreateConfig(MockBoltV2ServerConfig))
52
client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
53
TargetAddr: "127.0.0.1:2045",
54
Verify: &boltv2.VerifyConfig{
55
ExpectedStatusCode: 0,
58
Verify(client.SyncCall(), Equal, true)
59
stats := client.Stats()
60
Verify(stats.Requests(), Equal, 1)
61
Verify(stats.ExpectedResponseCount(), Equal, 1)
62
Verify(stats.ConnectionClosed(), Equal, 0)
65
Scenario(t, "downstream protocol config to http1", func() {
66
lib.InitMosn(CreateConfigByProtocol("Http1"), lib.CreateConfig(MockHttpServerConfig))
68
client := lib.CreateClient("Http1", &http.HttpClientConfig{
69
TargetAddr: "127.0.0.1:2045",
70
Verify: &http.VerifyConfig{
71
ExpectedStatusCode: 200,
74
Verify(client.SyncCall(), Equal, true)
75
stats := client.Stats()
76
Verify(stats.Requests(), Equal, 1)
77
Verify(stats.ExpectedResponseCount(), Equal, 1)
78
Verify(stats.ConnectionClosed(), Equal, 0)
81
Scenario(t, "downstream protocol config to http2", func() {
82
lib.InitMosn(CreateConfigByProtocol("Http2"), lib.CreateConfig(MockHttp2ServerConfig))
84
client := lib.CreateClient("Http2", &http.HttpClientConfig{
85
TargetAddr: "127.0.0.1:2045",
86
Verify: &http.VerifyConfig{
87
ExpectedStatusCode: 200,
90
Verify(client.SyncCall(), Equal, true)
91
stats := client.Stats()
92
Verify(stats.Requests(), Equal, 1)
93
Verify(stats.ExpectedResponseCount(), Equal, 1)
94
Verify(stats.ConnectionClosed(), Equal, 0)
99
func TestAutoProtocol(t *testing.T) {
100
Scenario(t, "downstream protocol config as auto", func() {
101
lib.InitMosn(CreateConfigByProtocol("Auto"))
102
Case("call bolt", func() {
103
sc := lib.CreateConfig(MockBoltServerConfig)
104
srv := lib.CreateServer(sc.Protocol, sc.Config)
107
time.Sleep(time.Second)
108
client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
109
TargetAddr: "127.0.0.1:2045",
110
Verify: &boltv1.VerifyConfig{
111
ExpectedStatusCode: 0,
114
Verify(client.SyncCall(), Equal, true)
115
stats := client.Stats()
116
Verify(stats.ConnectionClosed(), Equal, 0)
118
Case("call boltv2", func() {
119
sc := lib.CreateConfig(MockBoltV2ServerConfig)
120
srv := lib.CreateServer(sc.Protocol, sc.Config)
123
time.Sleep(time.Second)
124
client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
125
TargetAddr: "127.0.0.1:2045",
126
Verify: &boltv2.VerifyConfig{
127
ExpectedStatusCode: 0,
130
Verify(client.SyncCall(), Equal, true)
131
stats := client.Stats()
132
Verify(stats.ConnectionClosed(), Equal, 0)
135
Case("call http", func() {
136
sc := lib.CreateConfig(MockHttpServerConfig)
137
srv := lib.CreateServer(sc.Protocol, sc.Config)
140
time.Sleep(time.Second)
141
client := lib.CreateClient("Http1", &http.HttpClientConfig{
142
TargetAddr: "127.0.0.1:2045",
143
Verify: &http.VerifyConfig{
144
ExpectedStatusCode: 200,
147
Verify(client.SyncCall(), Equal, true)
148
stats := client.Stats()
149
Verify(stats.ConnectionClosed(), Equal, 0)
152
Case("call http2", func() {
153
sc := lib.CreateConfig(MockHttp2ServerConfig)
154
srv := lib.CreateServer(sc.Protocol, sc.Config)
157
time.Sleep(time.Second)
158
client := lib.CreateClient("Http2", &http.HttpClientConfig{
159
TargetAddr: "127.0.0.1:2045",
160
Verify: &http.VerifyConfig{
161
ExpectedStatusCode: 200,
164
Verify(client.SyncCall(), Equal, true)
165
stats := client.Stats()
166
Verify(stats.ConnectionClosed(), Equal, 0)
172
func TestSofaRPCAutoProtocol(t *testing.T) {
173
Scenario(t, "downstream protocol config as sofarpc", func() {
174
lib.InitMosn(CreateConfigByProtocol("bolt,boltv2"))
175
Case("call bolt", func() {
176
sc := lib.CreateConfig(MockBoltServerConfig)
177
srv := lib.CreateServer(sc.Protocol, sc.Config)
180
time.Sleep(time.Second)
181
client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
182
TargetAddr: "127.0.0.1:2045",
183
Verify: &boltv1.VerifyConfig{
184
ExpectedStatusCode: 0,
187
Verify(client.SyncCall(), Equal, true)
188
stats := client.Stats()
189
Verify(stats.ConnectionClosed(), Equal, 0)
191
Case("call boltv2", func() {
192
sc := lib.CreateConfig(MockBoltV2ServerConfig)
193
srv := lib.CreateServer(sc.Protocol, sc.Config)
196
time.Sleep(time.Second)
197
client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
198
TargetAddr: "127.0.0.1:2045",
199
Verify: &boltv2.VerifyConfig{
200
ExpectedStatusCode: 0,
203
Verify(client.SyncCall(), Equal, true)
204
stats := client.Stats()
205
Verify(stats.ConnectionClosed(), Equal, 0)
208
Case("call http", func() {
209
sc := lib.CreateConfig(MockHttpServerConfig)
210
srv := lib.CreateServer(sc.Protocol, sc.Config)
213
time.Sleep(time.Second)
214
client := lib.CreateClient("Http1", &http.HttpClientConfig{
215
TargetAddr: "127.0.0.1:2045",
218
Verify(client.SyncCall(), Equal, false)
219
stats := client.Stats()
220
Verify(stats.ConnectionClosed(), Equal, 1)
224
Case("call http2", func() {
225
sc := lib.CreateConfig(MockHttp2ServerConfig)
226
srv := lib.CreateServer(sc.Protocol, sc.Config)
229
time.Sleep(time.Second)
230
client := lib.CreateClient("Http2", &http.HttpClientConfig{
231
TargetAddr: "127.0.0.1:2045",
234
Verify(client.SyncCall(), Equal, false)
235
stats := client.Stats()
236
Verify(stats.ConnectionClosed(), Equal, 1)
242
func CreateConfigByProtocol(protos string) string {
243
return fmt.Sprintf(ConfigSimpleMultipleTmpl, protos)
246
const ConfigSimpleMultipleTmpl = `{
249
"default_log_path":"stdout",
250
"default_log_level": "ERROR",
253
"router_config_name":"router_to_server",
255
"name":"server_hosts",
260
"route":{"cluster_name":"server_cluster"}
268
"address":"127.0.0.1:2045",
275
"downstream_protocol":"%s",
276
"router_config_name":"router_to_server"
288
"name": "server_cluster",
290
"lb_type": "LB_RANDOM",
292
{"address":"127.0.0.1:8080"}