acc065d35e654d0139e896ede1c9f7c23a14989a
[ric-plt/lib/rmr.git] / test / app_test / run_multi_test.ksh
1 #!/usr/bin/env ksh
2 # vim: ts=4 sw=4 noet :
3 #==================================================================================
4 #    Copyright (c) 2019-2020 Nokia
5 #    Copyright (c) 2018-2020 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 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 -d 100 -n 10000 # send 10k messages with 100ms delay between
35 #
36 #       Date:           24 April 2019
37 #       Author:         E. Scott Daniels
38 # ---------------------------------------------------------------------------------
39
40
41 # The sender and receivers are run asynch. Their exit statuses are captured in a
42 # file in order for the 'main' to pick them up easily. For the multi test the 
43 # async connect must be DISABLED because in some environments (um, jenkins) the
44 # session connect time lags enough that the first message can be dropped silently.
45 # It doesn't happen all of the time, but frequently enough to be annoying. 
46 #
47 function run_sender {
48         RMR_ASYNC_CONN=0 ./sender${si} $nmsg $delay
49         echo $? >/tmp/PID$$.src         # must communicate state back via file b/c asynch
50 }
51
52 # $1 is the instance so we can keep logs separate
53 function run_rcvr {
54         typeset port
55
56         port=$(( 4460 + ${1:-0} ))
57         export RMR_RTG_SVC=$(( 9990 + $1 ))
58         ./receiver${si} $nmsg $port
59         echo $? >/tmp/PID$$.$1.rrc
60 }
61
62 #       Drop a contrived route table in such that the sender sends each message to n
63 #       receivers.
64 #
65 function set_rt {
66         typeset port=4460
67         typeset groups="localhost:4460"
68         for (( i=1; i < ${1:-3}; i++ ))
69         do
70                 groups="$groups;localhost:$((port+i))"
71         done
72
73         cat <<endKat >multi.rt
74                 newrt | start
75                 mse |0 | 0 | $groups
76                 mse |1 | 10 | $groups
77                 mse |2 | 20 | $groups
78                 rte |3 | $groups
79                 rte |4 | $groups
80                 rte |5 | $groups
81                 rte |6 | $groups
82                 rte |7 | $groups
83                 rte |8 | $groups
84                 rte |9 | $groups
85                 rte |10 | $groups
86                 rte |11 | $groups
87                 newrt | end
88 endKat
89
90 }
91
92 # ---------------------------------------------------------
93
94 if [[ ! -f local.rt ]]          # we need the real host name in the local.rt; build one from mask if not there
95 then
96         hn=$(hostname)
97         sed "s!%%hostname%%!$hn!" rt.mask >local.rt
98 fi
99
100 nmsg=10                                         # total number of messages to be exchanged (-n value changes)
101 delay=1000000                           # microsec sleep between msg 1,000,000 == 1s
102 wait=1
103 rebuild=0
104 nopull=""
105 verbose=0
106 nrcvrs=3                                        # this is sane, but -r allows it to be set up
107 force_make=0
108 si=""
109
110 while [[ $1 == -* ]]
111 do
112         case $1 in
113                 -B)     rebuild=1;;
114                 -b)     rebuild=1; nopull="nopull";;            # enable build but without pull
115                 -d)     delay=$2; shift;;
116                 -n)     nmsg=$2; shift;;
117                 -N)     si="";;                                                         # buld/run NNG binaries (turn off si)
118                 -M)     force_make=1;;
119                 -r)     nrcvrs=$2; shift;;
120                 -S)     si="_si";;                                                      # buld/run SI95 binaries
121                 -v)     verbose=1;;
122
123                 *)      echo "unrecognised option: $1"
124                         echo "usage: $0 [-B] [-d micor-sec-delay] [-M] [-n num-msgs] [-S]"
125                         echo "  -B forces an RMR rebuild which will use .build"
126                         echo "  -m force test applications to be remade"
127                         echo "  -S build/test SI95 based binaries"
128                         exit 1
129                         ;;
130         esac
131
132         shift
133 done
134
135 if (( verbose ))
136 then
137         echo "2" >.verbose
138         export RMR_VCTL_FILE=".verbose"
139 fi
140
141 if (( rebuild ))
142 then
143         set -e
144         $SHELL ./rebuild.ksh $nopull | read build_path
145         set +e
146 else
147         build_path=${BUILD_PATH:-"../../.build"}        # we prefer .build at the root level, but allow user option
148
149         if [[ ! -d $build_path ]]
150         then
151                 echo "cannot find build in: $build_path"
152                 echo "either create, and then build RMr, or set BUILD_PATH as an evironment var before running this"
153                 exit 1
154         fi
155 fi
156
157 if [[ -d $build_path/lib64 ]]
158 then
159         export LD_LIBRARY_PATH=$build_path:$build_path/lib64:$LD_LIBRARY_PATH
160 else
161         export LD_LIBRARY_PATH=$build_path:$build_path/lib:$LD_LIBRARY_PATH
162 fi
163 export LIBRARY_PATH=$LD_LIBRARY_PATH
164 export RMR_SEED_RT=./multi.rt
165
166 set_rt $nrcvrs                                          # set up the rt for n receivers
167
168 if (( rebuild || force_make )) || [[ ! -f ./sender${si} || ! -f ./receiver${si} ]]
169 then
170         if ! make >/dev/null 2>&1
171         then
172                 echo "[FAIL] cannot find sender${si} and/or receiver${si} binary, and cannot make them.... humm?"
173                 exit 1
174         fi
175 fi
176
177 for (( i=0; i < nrcvrs; i++ ))          # start the receivers with an instance number
178 do
179         run_rcvr $i &
180 done
181
182 sleep 2                         # let receivers init so we don't shoot at an empty target
183 run_sender &
184
185 wait
186
187
188 for (( i=0; i < nrcvrs; i++ ))          # collect return codes
189 do
190         head -1 /tmp/PID$$.$i.rrc | read x
191         (( rrc += x ))
192 done
193
194 head -1 /tmp/PID$$.src | read src
195
196 if (( !! (src + rrc) ))
197 then
198         echo "[FAIL] sender rc=$src  receiver rc=$rrc"
199 else
200         echo "[PASS] sender rc=$src  receiver rc=$rrc"
201         rm -f multi.rt
202 fi
203
204 rm /tmp/PID$$.*
205 rm -f .verbose
206
207 exit $(( !! (src + rrc) ))
208