xref: /aosp_15_r20/external/grpc-grpc/test/distrib/bazel/cpp/greeter_test.sh (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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
16set -exo pipefail
17
18SERVER_PID=""
19SERVER_TIMEOUT=10
20
21SERVER_OUTPUT=$(mktemp)
22
23function cleanup() {
24  if [ -n "$SERVER_PID" ]; then
25    kill "$SERVER_PID"
26  fi
27}
28
29function fail() {
30  echo "$1" >/dev/stderr
31  echo "Failed." >/dev/stderr
32  exit 1
33}
34
35function 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
47trap cleanup SIGINT SIGTERM EXIT
48
49./greeter_server >"$SERVER_OUTPUT" &
50SERVER_PID=$!
51
52SERVER_ADDRESS=$(await_server)
53
54RESPONSE=$(./greeter_client --target="$SERVER_ADDRESS")
55EXPECTED_RESPONSE="Greeter received: Hello world"
56
57if [ "$RESPONSE" != "$EXPECTED_RESPONSE" ]; then
58  fail "Received response \"$RESPONSE\" but expected \"$EXPECTED_RESPONSE\""
59fi
60
61echo "Success."
62