-
Notifications
You must be signed in to change notification settings - Fork 236
Expand file tree
/
Copy pathintegrate_test.sh
More file actions
executable file
·476 lines (394 loc) · 11.8 KB
/
integrate_test.sh
File metadata and controls
executable file
·476 lines (394 loc) · 11.8 KB
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
#!/bin/bash
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -euo pipefail
if [ -z "${1:-}" ]; then
echo "Provide sample directory, like: ./integrate_test.sh direct"
exit 1
fi
SAMPLE="$1"
P_DIR="$(pwd)/$SAMPLE"
PROJECT_NAME="$(basename "$P_DIR")"
GO_SERVER_LOG="/tmp/.${PROJECT_NAME}.go-server.log"
JAVA_SERVER_LOG="/tmp/.${PROJECT_NAME}.java-server.log"
PID_FILE="/tmp/.${PROJECT_NAME}.pid"
GO_CLIENT_TIMEOUT_SECONDS="${GO_CLIENT_TIMEOUT_SECONDS:-90}"
JAVA_SERVER_READY_TIMEOUT_SECONDS="${JAVA_SERVER_READY_TIMEOUT_SECONDS:-60}"
JAVA_SERVER_HOST="${JAVA_SERVER_HOST:-127.0.0.1}"
JAVA_SERVER_PORT="${JAVA_SERVER_PORT:-20000}"
if [ ! -d "$P_DIR" ]; then
echo "Sample directory not found: $P_DIR"
exit 1
fi
MAKE_CMD=(make PROJECT_DIR="$P_DIR" PROJECT_NAME="$PROJECT_NAME" -f Makefile)
JAVA_SERVER_RUN_SH="$(find "$P_DIR" -type f -path '*/java-server*/run.sh' -print -quit || true)"
JAVA_CLIENT_RUN_SH="$(find "$P_DIR" -type f -path '*/java-client*/run.sh' -print -quit || true)"
JAVA_SERVER_PID=""
GO_AUX_PIDS=()
JAVA_ENABLED=true
if { [ -n "$JAVA_SERVER_RUN_SH" ] || [ -n "$JAVA_CLIENT_RUN_SH" ]; } && ! command -v mvn >/dev/null 2>&1; then
JAVA_ENABLED=false
echo "Maven (mvn) is not available, all Java phases will be skipped for sample: $SAMPLE"
fi
run_make_target() {
"${MAKE_CMD[@]}" "$1"
}
kill_if_running() {
local pid="$1"
if [ -n "$pid" ] && kill -0 "$pid" 2>/dev/null; then
kill "$pid" 2>/dev/null || true
sleep 1
kill -9 "$pid" 2>/dev/null || true
fi
}
cleanup() {
local server_pid=""
local aux_pid
for aux_pid in "${GO_AUX_PIDS[@]:-}"; do
kill_if_running "$aux_pid"
done
kill_if_running "$JAVA_SERVER_PID"
if [ -f "$PID_FILE" ]; then
server_pid="$(cat "$PID_FILE" 2>/dev/null || true)"
kill_if_running "$server_pid"
rm -f "$PID_FILE"
fi
run_make_target stop >/dev/null 2>&1 || true
}
trap cleanup EXIT
resolve_config_path() {
local role="$1"
local conf_dir="$P_DIR/$role/conf"
if [ -f "$conf_dir/dubbogo.yml" ]; then
echo "$conf_dir/dubbogo.yml"
return 0
fi
if [ -f "$conf_dir/dubbogo.yaml" ]; then
echo "$conf_dir/dubbogo.yaml"
return 0
fi
return 1
}
wait_for_tcp_port() {
local host="$1"
local port="$2"
local timeout_seconds="$3"
local elapsed=0
while [ "$elapsed" -lt "$timeout_seconds" ]; do
if python3 - "$host" "$port" <<'PY' >/dev/null 2>&1
import socket
import sys
host = sys.argv[1]
port = int(sys.argv[2])
family = socket.AF_UNSPEC
type_ = socket.SOCK_STREAM
for af, socktype, proto, _, sockaddr in socket.getaddrinfo(host, port, family, type_):
sock = None
try:
sock = socket.socket(af, socktype, proto)
sock.settimeout(1.0)
sock.connect(sockaddr)
sys.exit(0)
except OSError:
continue
finally:
if sock is not None:
sock.close()
sys.exit(1)
PY
then
return 0
fi
sleep 1
elapsed=$((elapsed + 1))
done
return 1
}
wait_for_process_exit() {
local pid="$1"
local timeout_seconds="$2"
local elapsed=0
while kill -0 "$pid" 2>/dev/null; do
if [ "$elapsed" -ge "$timeout_seconds" ]; then
return 1
fi
sleep 1
elapsed=$((elapsed + 1))
done
return 0
}
wait_for_log_pattern() {
local log_file="$1"
local pattern="$2"
local timeout_seconds="$3"
local elapsed=0
while [ "$elapsed" -lt "$timeout_seconds" ]; do
if [ -f "$log_file" ] && grep -q "$pattern" "$log_file"; then
return 0
fi
sleep 1
elapsed=$((elapsed + 1))
done
return 1
}
run_go_client() {
if ! compgen -G "$P_DIR/go-client/cmd/*.go" >/dev/null; then
echo "go-client/cmd/*.go not found in $P_DIR"
return 1
fi
local client_conf
client_conf="$(resolve_config_path "go-client" || true)"
echo "Running Go client..."
(
cd "$P_DIR"
if [ -n "$client_conf" ]; then
export DUBBO_GO_CONFIG_PATH="$client_conf"
fi
go run ./go-client/cmd/*.go
) &
local go_client_pid=$!
local elapsed=0
while kill -0 "$go_client_pid" 2>/dev/null; do
if [ "$elapsed" -ge "$GO_CLIENT_TIMEOUT_SECONDS" ]; then
echo "Go client timed out after ${GO_CLIENT_TIMEOUT_SECONDS}s: $SAMPLE"
kill_if_running "$go_client_pid"
wait "$go_client_pid" 2>/dev/null || true
return 124
fi
sleep 1
elapsed=$((elapsed + 1))
done
wait "$go_client_pid"
}
start_aux_go_servers() {
local aux_server_dir
local aux_name
local aux_log
local aux_pid
local elapsed
while IFS= read -r aux_server_dir; do
[ -z "$aux_server_dir" ] && continue
aux_name="$(basename "$(dirname "$aux_server_dir")")"
aux_log="/tmp/.${PROJECT_NAME}.${aux_name}.log"
echo "Starting auxiliary Go server: $aux_name"
(
cd "$P_DIR"
go run "./${aux_server_dir#"$P_DIR"/}"/*.go
) >"$aux_log" 2>&1 &
aux_pid="$!"
GO_AUX_PIDS+=("$aux_pid")
elapsed=0
while kill -0 "$aux_pid" 2>/dev/null; do
if [ "$elapsed" -ge 10 ]; then
break
fi
sleep 1
elapsed=$((elapsed + 1))
done
if ! kill -0 "$aux_pid" 2>/dev/null; then
echo "Auxiliary Go server exited unexpectedly: $aux_name"
cat "$aux_log" || true
return 1
fi
done < <(find "$P_DIR" -mindepth 1 -maxdepth 1 -type d -name '*-server' ! -name 'go-server' ! -name 'java-server' -exec sh -c 'test -d "$1/cmd" && ls "$1"/cmd/*.go >/dev/null 2>&1 && echo "$1/cmd"' _ {} \;)
}
start_go_server() {
echo "Starting Go server..."
local server_conf
server_conf="$(resolve_config_path "go-server" || true)"
if [ -n "$server_conf" ]; then
DUBBO_GO_CONFIG_PATH="$server_conf" run_make_target start >"$GO_SERVER_LOG" 2>&1
else
run_make_target start >"$GO_SERVER_LOG" 2>&1
fi
sleep 5
if [ ! -f "$PID_FILE" ]; then
echo "Go server pid file not found: $PID_FILE"
cat "$GO_SERVER_LOG" || true
return 1
fi
local server_pid
server_pid="$(cat "$PID_FILE" 2>/dev/null || true)"
if [ -z "$server_pid" ] || ! kill -0 "$server_pid" 2>/dev/null; then
echo "Go server is not running after startup: $SAMPLE"
cat "$GO_SERVER_LOG" || true
local app_log="$P_DIR/go-server/dist/linux_amd64/release/${PROJECT_NAME}.log"
[ -f "$app_log" ] && cat "$app_log" || true
return 1
fi
}
stop_go_server() {
echo "Stopping Go server..."
run_make_target stop >/dev/null 2>&1 || true
}
run_java_client_if_present() {
if [ -z "$JAVA_CLIENT_RUN_SH" ]; then
echo "No Java client found, skipping Java client run"
return 0
fi
if [ "$JAVA_ENABLED" != "true" ]; then
echo "Java phase disabled, skipping Java client run"
return 0
fi
local java_client_dir
java_client_dir="$(dirname "$JAVA_CLIENT_RUN_SH")"
echo "Running Java client: $JAVA_CLIENT_RUN_SH"
(
cd "$java_client_dir"
bash ./run.sh
)
}
start_java_server_if_present() {
if [ -z "$JAVA_SERVER_RUN_SH" ]; then
echo "No Java server found, skipping Java server phase"
return 1
fi
if [ "$JAVA_ENABLED" != "true" ]; then
echo "Java phase disabled, skipping Java server phase"
return 1
fi
local java_server_dir
java_server_dir="$(dirname "$JAVA_SERVER_RUN_SH")"
echo "Starting Java server: $JAVA_SERVER_RUN_SH"
(
cd "$java_server_dir"
bash ./run.sh
) >"$JAVA_SERVER_LOG" 2>&1 &
JAVA_SERVER_PID="$!"
sleep 3
if ! kill -0 "$JAVA_SERVER_PID" 2>/dev/null; then
echo "Java server exited unexpectedly. Log:"
cat "$JAVA_SERVER_LOG" || true
return 1
fi
if ! wait_for_tcp_port "$JAVA_SERVER_HOST" "$JAVA_SERVER_PORT" "$JAVA_SERVER_READY_TIMEOUT_SECONDS"; then
echo "Java server is running but not ready on ${JAVA_SERVER_HOST}:${JAVA_SERVER_PORT} after ${JAVA_SERVER_READY_TIMEOUT_SECONDS}s"
cat "$JAVA_SERVER_LOG" || true
return 1
fi
return 0
}
run_graceful_shutdown_sample() {
local client_log="/tmp/.${PROJECT_NAME}.go-client.log"
local server_pid=""
local client_pid=""
local server_bin="/tmp/.${PROJECT_NAME}.go-server.bin"
local client_bin="/tmp/.${PROJECT_NAME}.go-client.bin"
if command -v lsof >/dev/null 2>&1; then
lsof -tiTCP:20000 -sTCP:LISTEN | xargs -r kill -9 || true
fi
echo "Building graceful_shutdown Go server..."
(
cd "$P_DIR"
go build -o "$server_bin" ./go-server/cmd
)
echo "Building graceful_shutdown Go client..."
(
cd "$P_DIR"
go build -o "$client_bin" ./go-client/cmd
)
echo "Starting graceful_shutdown Go server..."
(
cd "$P_DIR"
exec "$server_bin" -timeout=15s -step-timeout=2s -consumer-update-wait=0s -delay=2s
) >"$GO_SERVER_LOG" 2>&1 &
server_pid="$!"
echo "$server_pid" >"$PID_FILE"
if ! wait_for_tcp_port "127.0.0.1" "20000" 30; then
echo "graceful_shutdown server did not become ready on 127.0.0.1:20000"
cat "$GO_SERVER_LOG" || true
return 1
fi
if ! kill -0 "$server_pid" 2>/dev/null; then
echo "graceful_shutdown server exited unexpectedly before client start"
cat "$GO_SERVER_LOG" || true
return 1
fi
echo "Running graceful_shutdown Go client..."
(
cd "$P_DIR"
exec "$client_bin" \
-addr=tri://127.0.0.1:20000 \
-concurrency=2 \
-interval=200ms \
-short \
-request-timeout=6s \
-max-requests=12 \
-min-successes=1 \
-min-failures=1 \
-name-prefix=integration
) >"$client_log" 2>&1 &
client_pid="$!"
if ! wait_for_log_pattern "$client_log" "succeeded" 30; then
echo "graceful_shutdown client did not observe a successful request before shutdown"
cat "$client_log" || true
cat "$GO_SERVER_LOG" || true
return 1
fi
echo "Triggering graceful shutdown..."
kill -INT "$server_pid" 2>/dev/null || true
if ! wait "$client_pid"; then
echo "graceful_shutdown client exited with failure"
cat "$client_log" || true
cat "$GO_SERVER_LOG" || true
return 1
fi
if ! wait_for_process_exit "$server_pid" 30; then
echo "graceful_shutdown server did not exit within 30s after SIGINT"
cat "$GO_SERVER_LOG" || true
return 1
fi
wait "$server_pid" 2>/dev/null || true
if ! grep -q "failed" "$client_log"; then
echo "graceful_shutdown client did not observe request failures during shutdown"
cat "$client_log" || true
return 1
fi
echo "graceful_shutdown integration completed"
}
main() {
echo "=========================================="
echo "Starting sample flow for: $SAMPLE"
echo "Sample directory: $P_DIR"
echo "=========================================="
if [ "$SAMPLE" = "graceful_shutdown" ]; then
run_graceful_shutdown_sample
echo "=========================================="
echo "Sample flow completed for: $SAMPLE"
echo "=========================================="
return 0
fi
start_go_server
start_aux_go_servers
run_go_client
run_java_client_if_present
stop_go_server
if start_java_server_if_present; then
run_java_client_if_present
if ! kill -0 "$JAVA_SERVER_PID" 2>/dev/null; then
echo "Java server exited before final Go client phase. Log:"
cat "$JAVA_SERVER_LOG" || true
exit 1
fi
run_go_client
else
echo "Java server phase skipped"
fi
echo "=========================================="
echo "Sample flow completed for: $SAMPLE"
echo "=========================================="
}
main