Debian Codegraph @

test/distrib/bazel/cpp/greeter_test.sh

raw
1 #!/usr/bin/env bash
2 # Copyright 2022 The 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 set -exo pipefail
17
18 SERVER_PID=""
19 SERVER_TIMEOUT=10
20
21 SERVER_OUTPUT=$(mktemp)
22
23 function cleanup() {
24 if [ -n "$SERVER_PID" ]; then
25 kill "$SERVER_PID"
26 fi
27 }
28
29 function fail() {
30 echo "$1" >/dev/stderr
31 echo "Failed." >/dev/stderr
32 exit 1
33 }
34
35 function await_server() {
36 TIME=0
37 while [ ! -s "$SERVER_OUTPUT" ]; do
38 if [ "$TIME" == "$SERVER_TIMEOUT" ] ; then
39 fail "Server not listening after $SERVER_TIMEOUT seconds."
40 fi
41 sleep 1
42 TIME=$((TIME+1))
43 done
44 cat "$SERVER_OUTPUT"
45 }
46
47 trap cleanup SIGINT SIGTERM EXIT
48
49 ./greeter_server >"$SERVER_OUTPUT" &
50 SERVER_PID=$!
51
52 SERVER_ADDRESS=$(await_server)
53
54 RESPONSE=$(./greeter_client --target="$SERVER_ADDRESS")
55 EXPECTED_RESPONSE="Greeter received: Hello world"
56
57 if [ "$RESPONSE" != "$EXPECTED_RESPONSE" ]; then
58 fail "Received response \"$RESPONSE\" but expected \"$EXPECTED_RESPONSE\""
59 fi
60
61 echo "Success."