test(e2e): Add return to sender app based test
[ric-plt/lib/rmr.git] / test / app_test / run_multi_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 to all receivers in parallel
27 #                               (forcing message cloning internally in RMr).
28 #                               It should be possible to clone the repo, switch to this directory
29 #                               and execute  'ksh run -B'  which will build RMr, make the sender and
30 #                               recevier then  run the basic test.
31 #
32 #                               Example command line:
33 #                                       ksh ./run_multi_test.ksh                # default 10 messages at 1 msg/sec
34 #                                       ksh ./run_multi_test.ksh -N    # default but with nanomsg lib
35 #                                       ksh ./run_multi_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         if (( $nano_sender ))
47         then
48                 ./sender_nano $nmsg $delay
49         else
50                 ./sender $nmsg $delay
51         fi
52         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
53 }
54
55 # $1 is the instance so we can keep logs separate
56 function run_rcvr {
57         typeset port
58
59         port=$(( 4460 + ${1:-0} ))
60         export RMR_RTG_SVC=$(( 9990 + $1 ))
61         if (( $nano_receiver ))
62         then
63                 ./receiver_nano $nmsg $port
64         else
65                 ./receiver $nmsg $port
66         fi
67         echo $? >/tmp/PID$$.$1.rrc
68 }
69
70 #       Drop a contrived route table in such that the sender sends each message to n
71 #       receivers.
72 #
73 function set_rt {
74         typeset port=4460
75         typeset groups="localhost:4460"
76         for (( i=1; i < ${1:-3}; i++ ))
77         do
78                 groups="$groups;localhost:$((port+i))"
79         done
80
81         cat <<endKat >multi.rt
82                 newrt | start
83                 mse |0 | 0 | $groups
84                 mse |1 | 10 | $groups
85                 mse |2 | 20 | $groups
86                 rte |3 | $groups
87                 rte |4 | $groups
88                 rte |5 | $groups
89                 rte |6 | $groups
90                 rte |7 | $groups
91                 rte |8 | $groups
92                 rte |9 | $groups
93                 rte |10 | $groups
94                 rte |11 | $groups
95                 newrt | end
96 endKat
97
98 }
99
100 # ---------------------------------------------------------
101
102 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
103 then
104         hn=$(hostname)
105         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
106 fi
107
108 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
109 delay=1000000                           # microsec sleep between msg 1,000,000 == 1s
110 nano_sender=0                           # start nano version if set (-N)
111 nano_receiver=0
112 wait=1
113 rebuild=0
114 nopull=""
115 verbose=0
116 nrcvrs=3                                        # this is sane, but -r allows it to be set up
117
118 while [[ $1 == -* ]]
119 do
120         case $1 in
121                 -B)     rebuild=1;;
122                 -b)     rebuild=1; nopull="nopull";;            # enable build but without pull
123                 -d)     delay=$2; shift;;
124                 -N)     nano_sender=1
125                         nano_receiver=1
126                         ;;
127                 -n)     nmsg=$2; shift;;
128                 -r)     nrcvrs=$2; shift;;
129                 -v)     verbose=1;;
130
131                 *)      echo "unrecognised option: $1"
132                         echo "usage: $0 [-B] [-d micor-sec-delay] [-N] [-n num-msgs]"
133                         echo "  -B forces a rebuild which will use .build"
134                         exit 1
135                         ;;
136         esac
137
138         shift
139 done
140
141 if (( verbose ))
142 then
143         echo "2" >.verbose
144         export RMR_VCTL_FILE=".verbose"
145 fi
146
147 if (( rebuild ))
148 then
149         set -e
150         ksh ./rebuild.ksh $nopull | read build_path
151         set +e
152 else
153         build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
154
155         if [[ ! -d $build_path ]]
156         then
157                 echo "cannot find build in: $build_path"
158                 echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
159                 exit 1
160         fi
161 fi
162
163 if [[ -d $build_path/lib64 ]]
164 then
165         export LD_LIBRARY_PATH=$build_path:$build_path/lib64
166 else
167         export LD_LIBRARY_PATH=$build_path:$build_path/lib
168 fi
169 export LIBRARY_PATH=$LD_LIBRARY_PATH
170 export RMR_SEED_RT=./multi.rt
171
172 set_rt $nrcvrs                                          # set up the rt for n receivers
173
174 if [[ ! -f ./sender ]]
175 then
176         if ! make >/dev/null 2>&1
177         then
178                 echo "[FAIL] cannot find sender binary, and cannot make it.... humm?"
179                 exit 1
180         fi
181 fi
182
183 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
184 do
185         run_rcvr $i &
186 done
187
188 sleep 2                         # let receivers init so we don't shoot at an empty target
189 run_sender &
190
191 wait
192
193
194 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
195 do
196         head -1 /tmp/PID$$.$i.rrc | read x
197         (( rrc += x ))
198 done
199
200 head -1 /tmp/PID$$.src | read src
201
202 if (( !! (src + rrc) ))
203 then
204         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
205 else
206         echo "[PASS] sender rc=$src  receiver rc=$rrc"
207         rm -f multi.rt
208 fi
209
210 rm /tmp/PID$$.*
211 rm -f .verbose
212
213 exit $(( !! (src + rrc) ))
214