Debian Codegraph @

examples/cpp/features/run_tests.sh

raw
1 #!/usr/bin/env bash
2 # Copyright 2021 gRPC authors.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15
16 # Test structure borrowed with gratitude from
17 # https://github.com/grpc/grpc-go/tree/master/examples/features
18
19 set -eux
20
21 # execute in root dir
22 SCRIPTPATH="$(
23 cd -- "$(dirname "$0")" >/dev/null 2>&1
24 pwd -P
25 )"
26 cd ${SCRIPTPATH}/../../..
27
28 SERVER_PORT=50051
29
30 export TMPDIR=$(mktemp -d)
31 trap "rm -rf ${TMPDIR}" EXIT
32
33 clean() {
34 # loop a handful of times in case job shutdown is not immediate
35 for i in {1..5}; do
36 # kill all jobs
37 jobs -p | xargs kill &>/dev/null || true
38 # wait for all jobs to exit
39 sleep 0.3
40 if ! jobs | read; then
41 return
42 fi
43 done
44 echo "ERROR: clean failed to kill tests"
45 jobs
46 exit 1
47 }
48
49 fail() {
50 echo "$@" >&2
51 clean
52 exit 1
53 }
54
55 pass() {
56 echo "SUCCESS: $1"
57 }
58
59 wait_for_server() {
60 feature=$1
61 wait_command=${SERVER_WAIT_COMMAND[$feature]:-${SERVER_WAIT_COMMAND["default"]}}
62 echo "INFO: waiting for server to start"
63 declare -i i=0
64 while ! eval "$wait_command"; do
65 ((++i < 10)) || fail "cannot determine if server started"
66 lsof -U
67 sleep 1
68 done
69 pass "server started"
70 }
71
72 FEATURES=(
73 "unix_abstract"
74 )
75
76 declare -A SERVER_WAIT_COMMAND=(
77 ["unix_abstract"]="lsof -U | grep '@grpc@abstract'"
78 ["default"]="lsof -i :$SERVER_PORT | grep $SERVER_PORT"
79 )
80
81 declare -A EXPECTED_SERVER_OUTPUT=(
82 ["unix_abstract"]="Server listening on unix-abstract:grpc%00abstract ... Echoing: arst"
83 )
84
85 declare -A EXPECTED_CLIENT_OUTPUT=(
86 ["unix_abstract"]="Sending 'arst' to unix-abstract:grpc%00abstract ... Received: arst"
87 )
88
89 for feature in ${FEATURES[@]}; do
90 echo "TESTING: $feature"
91 bazel build --define=use_strict_warning=true //examples/cpp/features/${feature}:all || fail "failed to build $feature"
92
93 SERVER_LOG="$(mktemp)"
94 ./bazel-bin/examples/cpp/features/$feature/server &>$SERVER_LOG &
95
96 wait_for_server $feature
97
98 # TODO(hork): add a timeout to abort client?
99 CLIENT_LOG="$(mktemp)"
100 ./bazel-bin/examples/cpp/features/$feature/client &>$CLIENT_LOG
101
102 if [ -n "${EXPECTED_SERVER_OUTPUT[$feature]}" ]; then
103 if ! grep -q "${EXPECTED_SERVER_OUTPUT[$feature]}" $SERVER_LOG; then
104 fail "server log missing output: ${EXPECTED_SERVER_OUTPUT[$feature]}
105 got server log:
106 $(cat $SERVER_LOG)
107 got client log:
108 $(cat $CLIENT_LOG)
109 "
110 else
111 pass "server log contains expected output: ${EXPECTED_SERVER_OUTPUT[$feature]}"
112 fi
113 fi
114
115 if [ -n "${EXPECTED_CLIENT_OUTPUT[$feature]}" ]; then
116 if ! grep -q "${EXPECTED_CLIENT_OUTPUT[$feature]}" $CLIENT_LOG; then
117 fail "client log missing output: ${EXPECTED_CLIENT_OUTPUT[$feature]}
118 got server log:
119 $(cat $SERVER_LOG)
120 got client log:
121 $(cat $CLIENT_LOG)
122 "
123 else
124 pass "client log contains expected output: ${EXPECTED_CLIENT_OUTPUT[$feature]}"
125 fi
126 fi
127
128 clean
129 done