test(e2e): Add return to sender app based test
[ric-plt/lib/rmr.git] / test / app_test / run_rr_test.ksh
1 #!/usr/bin/env ksh
2 # :vi ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019 Nokia
5 #    Copyright (c) 2018-2019 AT&T Intellectual Property.
6 #
7 #   Licensed under the Apache License, Version 2.0 (the "License");
8 #   you may not use this file except in compliance with the License.
9 #   You may obtain a copy of the License at
10 #
11 #       http://www.apache.org/licenses/LICENSE-2.0
12 #
13 #   Unless required by applicable law or agreed to in writing, software
14 #   distributed under the License is distributed on an "AS IS" BASIS,
15 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 #   See the License for the specific language governing permissions and
17 #   limitations under the License.
18 #==================================================================================
19 #
20
21 # ---------------------------------------------------------------------------------
22 #       Mnemonic:       run_multi_test.ksh
23 #       Abstract:       This is a simple script to set up and run the basic send/receive
24 #                               processes for some library validation on top of nano/nng. This
25 #                               particular tests starts several receivers and creates a route table
26 #                               which causes messages to be sent round robin to all of the receivers.
27 #                               The number of messages command line parameter (-n) will be the number
28 #                               of messages that each receiver should expect; the sender will be asked
29 #                               to send r times that many messages so that as they are round robbined
30 #                               each receiver should get the same number of messages.
31 #
32 #                               Example command line:
33 #                                       ksh ./run_rr_test.ksh           # default 10 messages at 1 msg/sec
34 #                                       ksh ./run_rr_test.ksh -N    # default but with nanomsg lib
35 #                                       ksh ./run_rr_test.ksh -d 100 -n 10000 # send 10k messages with 100ms delay between
36 #
37 #       Date:           24 April 2019
38 #       Author:         E. Scott Daniels
39 # ---------------------------------------------------------------------------------
40
41
42 # The sender and receivers are run asynch. Their exit statuses are captured in a
43 # file in order for the 'main' to pick them up easily.
44 #
45 function run_sender {
46         export RMR_RTG_SVC=8990
47         if (( $nano_sender ))
48         then
49                 ./sender_nano $(( nmsg * nrcvrs )) $delay $max_mtype
50         else
51                 ./sender $(( nmsg * nrcvrs ))  $delay $max_mtype
52         fi
53         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
54 }
55
56 # $1 is the instance so we can keep logs separate
57 function run_rcvr {
58         typeset port
59
60         port=$(( 4560 + ${1:-0} ))
61         export RMR_RTG_SVC=$(( 9990 + $1 ))
62         if (( $nano_receiver ))
63         then
64                 ./receiver_nano $nmsg $port
65         else
66                 ./receiver $nmsg $port
67         fi
68         echo $? >/tmp/PID$$.$1.rrc
69 }
70
71 #
72 #       Drop a contrived route table in such that the sender sends each message to n
73 #       receivers.
74 #
75 function set_rt {
76         typeset port=4560
77         typeset endpoints="localhost:4560"
78         for (( i=1; i < ${1:-3}; i++ ))
79         do
80                 endpoints="$endpoints,localhost:$((port+i))"
81         done
82
83         cat <<endKat >rr.rt
84                 newrt |start
85                 rte |0 | $endpoints  |0
86                 rte |1 | $endpoints  |10
87                 mse |2 | 20 | $endpoints                # new style mtype/subid entry
88                 rte |3 | $endpoints  | -1
89                 rte |4 | $endpoints  | -1
90                 rte |5 | $endpoints  | -1
91                 rte |6 | $endpoints  | -1
92                 rte |7 | $endpoints  | -1
93                 rte |8 | $endpoints  | -1
94                 rte |9 | $endpoints  | -1
95                 rte |10 | $endpoints  | -1
96                 rte |11 | $endpoints  | -1
97                 newrt |end
98 endKat
99
100 }
101
102 # ---------------------------------------------------------
103
104 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
105 then
106         hn=$(hostname)
107         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
108 fi
109
110 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
111 delay=1000                                      # microsec sleep between msg 1,000,000 == 1s (shorter than others b/c/ we are sending to multiple)
112 nano_sender=0                           # start nano version if set (-N)
113 nano_receiver=0
114 wait=1
115 rebuild=0
116 nopull=""
117 verbose=0
118 max_mtype=1                                     # causes all msgs to go with type 1; use -M to set up, but likely harder to validate
119 nrcvrs=3                                        # this is sane, but -r allows it to be set up
120
121 while [[ $1 == -* ]]
122 do
123         case $1 in
124                 -B)     rebuild=1;;
125                 -b)     rebuild=1; nopull="nopull";;            # build without pulling
126                 -d)     delay=$2; shift;;
127                 -m) max_mtype=$2; shift;;
128                 -N)     nano_sender=1
129                         nano_receiver=1
130                         ;;
131                 -n)     nmsg=$2; shift;;
132                 -r)     nrcvrs=$2; shift;;
133                 -v)     verbose=1;;
134
135                 *)      echo "unrecognised option: $1"
136                         echo "usage: $0 [-B] [-d micor-sec-delay] [-N] [-n num-msgs]"
137                         echo "  -B forces a rebuild which will use .build"
138                         exit 1
139                         ;;
140         esac
141
142         shift
143 done
144
145 if (( verbose ))
146 then
147         echo "2" >.verbose
148         export RMR_VCTL_FILE=".verbose"
149 fi
150
151 if (( rebuild ))
152 then
153         set -e
154         ksh ./rebuild.ksh $nopull | read build_path
155         set +e
156 else
157         build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
158
159         if [[ ! -d $build_path ]]
160         then
161                 echo "cannot find build in: $build_path"
162                 echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
163                 exit 1
164         fi
165 fi
166
167 if [[ -d $build_path/lib64 ]]
168 then
169         export LD_LIBRARY_PATH=$build_path:$build_path/lib64
170 else
171         export LD_LIBRARY_PATH=$build_path:$build_path/lib
172 fi
173 export LIBRARY_PATH=$LD_LIBRARY_PATH
174 export RMR_SEED_RT=./rr.rt
175
176 set_rt $nrcvrs
177
178 if [[ ! -f ./sender ]]
179 then
180         if ! make >/dev/null 2>&1
181         then
182                 echo "[FAIL] cannot find sender binary, and cannot make it.... humm?"
183                 exit 1
184         fi
185 fi
186
187 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
188 do
189         run_rcvr $i &
190 done
191
192 sleep 2                                 # wait to start sender else we might send before receivers up and drop messages
193 run_sender &
194
195 wait
196
197
198 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
199 do
200         head -1 /tmp/PID$$.$i.rrc | read x
201         (( rrc += x ))
202 done
203
204 head -1 /tmp/PID$$.src | read src
205
206 if (( !! (src + rrc) ))
207 then
208         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
209 else
210         echo "[PASS] sender rc=$src  receiver rc=$rrc"
211 fi
212
213 rm /tmp/PID$$.*
214 rm -f .verbose
215 rm -f rr.rt
216
217 exit $(( !! (src + rrc) ))
218