mosn

Форк
0
/
multiple_protocol_test.go 
297 строк · 8.0 Кб
1
//go:build MOSNTest
2
// +build MOSNTest
3

4
package simple
5

6
import (
7
	"fmt"
8
	"testing"
9
	"time"
10

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"
16
)
17

18
const MockBoltV2ServerConfig = `{
19
	"protocol":"boltv2",
20
	"config": {
21
		"address": "127.0.0.1:8080"
22
	}
23
}`
24

25
const MockHttp2ServerConfig = `{
26
	"protocol":"Http2",
27
	"config": {
28
		"address": "127.0.0.1:8080"
29
	}
30
}`
31

32
func TestOneProtocolConfig(t *testing.T) {
33
	Scenario(t, "downstream protocol config to bolt", func() {
34
		lib.InitMosn(CreateConfigByProtocol("bolt"), lib.CreateConfig(MockBoltServerConfig))
35
		Case("call", func() {
36
			client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
37
				TargetAddr: "127.0.0.1:2045",
38
				Verify: &boltv1.VerifyConfig{
39
					ExpectedStatusCode: 0,
40
				},
41
			})
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)
47
		})
48
	})
49
	Scenario(t, "downstream protocol config to boltv2", func() {
50
		lib.InitMosn(CreateConfigByProtocol("boltv2"), lib.CreateConfig(MockBoltV2ServerConfig))
51
		Case("call", func() {
52
			client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
53
				TargetAddr: "127.0.0.1:2045",
54
				Verify: &boltv2.VerifyConfig{
55
					ExpectedStatusCode: 0,
56
				},
57
			})
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)
63
		})
64
	})
65
	Scenario(t, "downstream protocol config to http1", func() {
66
		lib.InitMosn(CreateConfigByProtocol("Http1"), lib.CreateConfig(MockHttpServerConfig))
67
		Case("call", func() {
68
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
69
				TargetAddr: "127.0.0.1:2045",
70
				Verify: &http.VerifyConfig{
71
					ExpectedStatusCode: 200,
72
				},
73
			})
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)
79
		})
80
	})
81
	Scenario(t, "downstream protocol config to http2", func() {
82
		lib.InitMosn(CreateConfigByProtocol("Http2"), lib.CreateConfig(MockHttp2ServerConfig))
83
		Case("call", func() {
84
			client := lib.CreateClient("Http2", &http.HttpClientConfig{
85
				TargetAddr: "127.0.0.1:2045",
86
				Verify: &http.VerifyConfig{
87
					ExpectedStatusCode: 200,
88
				},
89
			})
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)
95
		})
96
	})
97
}
98

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)
105
			defer srv.Stop()
106
			go srv.Start()
107
			time.Sleep(time.Second) // wait server start
108
			client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
109
				TargetAddr: "127.0.0.1:2045",
110
				Verify: &boltv1.VerifyConfig{
111
					ExpectedStatusCode: 0,
112
				},
113
			})
114
			Verify(client.SyncCall(), Equal, true)
115
			stats := client.Stats()
116
			Verify(stats.ConnectionClosed(), Equal, 0)
117
		})
118
		Case("call boltv2", func() {
119
			sc := lib.CreateConfig(MockBoltV2ServerConfig)
120
			srv := lib.CreateServer(sc.Protocol, sc.Config)
121
			defer srv.Stop()
122
			go srv.Start()
123
			time.Sleep(time.Second) // wait server start
124
			client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
125
				TargetAddr: "127.0.0.1:2045",
126
				Verify: &boltv2.VerifyConfig{
127
					ExpectedStatusCode: 0,
128
				},
129
			})
130
			Verify(client.SyncCall(), Equal, true)
131
			stats := client.Stats()
132
			Verify(stats.ConnectionClosed(), Equal, 0)
133

134
		})
135
		Case("call http", func() {
136
			sc := lib.CreateConfig(MockHttpServerConfig)
137
			srv := lib.CreateServer(sc.Protocol, sc.Config)
138
			defer srv.Stop()
139
			go srv.Start()
140
			time.Sleep(time.Second) // wait server start
141
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
142
				TargetAddr: "127.0.0.1:2045",
143
				Verify: &http.VerifyConfig{
144
					ExpectedStatusCode: 200,
145
				},
146
			})
147
			Verify(client.SyncCall(), Equal, true)
148
			stats := client.Stats()
149
			Verify(stats.ConnectionClosed(), Equal, 0)
150

151
		})
152
		Case("call http2", func() {
153
			sc := lib.CreateConfig(MockHttp2ServerConfig)
154
			srv := lib.CreateServer(sc.Protocol, sc.Config)
155
			defer srv.Stop()
156
			go srv.Start()
157
			time.Sleep(time.Second) // wait server start
158
			client := lib.CreateClient("Http2", &http.HttpClientConfig{
159
				TargetAddr: "127.0.0.1:2045",
160
				Verify: &http.VerifyConfig{
161
					ExpectedStatusCode: 200,
162
				},
163
			})
164
			Verify(client.SyncCall(), Equal, true)
165
			stats := client.Stats()
166
			Verify(stats.ConnectionClosed(), Equal, 0)
167
		})
168

169
	})
170
}
171

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)
178
			defer srv.Stop()
179
			go srv.Start()
180
			time.Sleep(time.Second) // wait server start
181
			client := lib.CreateClient("bolt", &boltv1.BoltClientConfig{
182
				TargetAddr: "127.0.0.1:2045",
183
				Verify: &boltv1.VerifyConfig{
184
					ExpectedStatusCode: 0,
185
				},
186
			})
187
			Verify(client.SyncCall(), Equal, true)
188
			stats := client.Stats()
189
			Verify(stats.ConnectionClosed(), Equal, 0)
190
		})
191
		Case("call boltv2", func() {
192
			sc := lib.CreateConfig(MockBoltV2ServerConfig)
193
			srv := lib.CreateServer(sc.Protocol, sc.Config)
194
			defer srv.Stop()
195
			go srv.Start()
196
			time.Sleep(time.Second) // wait server start
197
			client := lib.CreateClient("boltv2", &boltv2.BoltClientConfig{
198
				TargetAddr: "127.0.0.1:2045",
199
				Verify: &boltv2.VerifyConfig{
200
					ExpectedStatusCode: 0,
201
				},
202
			})
203
			Verify(client.SyncCall(), Equal, true)
204
			stats := client.Stats()
205
			Verify(stats.ConnectionClosed(), Equal, 0)
206

207
		})
208
		Case("call http", func() {
209
			sc := lib.CreateConfig(MockHttpServerConfig)
210
			srv := lib.CreateServer(sc.Protocol, sc.Config)
211
			defer srv.Stop()
212
			go srv.Start()
213
			time.Sleep(time.Second) // wait server start
214
			client := lib.CreateClient("Http1", &http.HttpClientConfig{
215
				TargetAddr: "127.0.0.1:2045",
216
			})
217
			// cannot request for http, got a connection closed error
218
			Verify(client.SyncCall(), Equal, false)
219
			stats := client.Stats()
220
			Verify(stats.ConnectionClosed(), Equal, 1)
221

222
		})
223

224
		Case("call http2", func() {
225
			sc := lib.CreateConfig(MockHttp2ServerConfig)
226
			srv := lib.CreateServer(sc.Protocol, sc.Config)
227
			defer srv.Stop()
228
			go srv.Start()
229
			time.Sleep(time.Second) // wait server start
230
			client := lib.CreateClient("Http2", &http.HttpClientConfig{
231
				TargetAddr: "127.0.0.1:2045",
232
			})
233
			// cannot request for http2, got a connection closed error
234
			Verify(client.SyncCall(), Equal, false)
235
			stats := client.Stats()
236
			Verify(stats.ConnectionClosed(), Equal, 1)
237
		})
238

239
	})
240
}
241

242
func CreateConfigByProtocol(protos string) string {
243
	return fmt.Sprintf(ConfigSimpleMultipleTmpl, protos)
244
}
245

246
const ConfigSimpleMultipleTmpl = `{
247
	 "servers":[
248
	 	{
249
			"default_log_path":"stdout",
250
			"default_log_level": "ERROR",
251
			"routers":[
252
				{
253
					"router_config_name":"router_to_server",
254
					"virtual_hosts":[{
255
						"name":"server_hosts",
256
						"domains": ["*"],
257
						"routers": [
258
							{
259
								"match":{},
260
								"route":{"cluster_name":"server_cluster"}
261
							}
262
						]
263
					}]
264
				}
265
			],
266
			"listeners":[
267
				{
268
					"address":"127.0.0.1:2045",
269
					"bind_port": true,
270
					"filter_chains": [{
271
						"filters": [
272
							{
273
								"type": "proxy",
274
								"config": {
275
									"downstream_protocol":"%s",
276
									"router_config_name":"router_to_server"
277
								}
278
							}
279
						]
280
					}]
281
				}
282
			]
283
		}
284
	 ],
285
	 "cluster_manager":{
286
		 "clusters":[
287
		 	{
288
				"name": "server_cluster",
289
				"type": "SIMPLE",
290
				"lb_type": "LB_RANDOM",
291
				"hosts":[
292
					{"address":"127.0.0.1:8080"}
293
				]
294
			}
295
		 ]
296
	 }
297
 }`
298

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.