test_events_plugin.sh: Assert for expected events count

Rather than asserting for expected events to be in a specific order,
assert for the event count of each desired type. Also, fix an issue
where the commands to capture events were killed improperly when the
desired number of events are captured.

Change-Id: I1ed2e8fd6af469c7c2cbbb73170c8025918ae694
diff --git a/test/test_events_plugin.sh b/test/test_events_plugin.sh
index c68e39b..ee02ded 100755
--- a/test/test_events_plugin.sh
+++ b/test/test_events_plugin.sh
@@ -94,32 +94,36 @@
 capture_events() { # count
     local count=$1
     [ -n "$count" ] || count=1
-    ssh -p 29418 -x "$SERVER" "${PLUGIN_CMD[@]}" > "$EVENT_FIFO" &
-    CAPTURE_PID_SSH=$!
+
+    # Re-create the fifo to ensure that is is empty
+    rm -f "$EVENT_FIFO"
+    mkfifo "$EVENT_FIFO"
+
     head -n $count < "$EVENT_FIFO" > "$EVENTS" &
     CAPTURE_PID_HEAD=$!
     sleep 1
+    ssh -p 29418 -x "$SERVER" "${PLUGIN_CMD[@]}" > "$EVENT_FIFO" &
+    CAPTURE_PID_SSH=$!
+    sleep 1
 }
 
 wait_event() {
-   (sleep 1 ; q kill -9 $CAPTURE_PID_SSH ; q kill -9 $CAPTURE_PID_HEAD ) &
-    q wait $CAPTURE_PID_SSH $CAPTURE_PID_HEAD
+   # Below kill of CAPTURE_PID_HEAD is a safety net and ideally we wouldn't
+   # want this kill to stop CAPTURE_PID_HEAD, rather we want it die on its
+   # own when the 'head' in capture_events() captures the desired events. The
+   # delay here must ideally be greater than the run time of the entire suite.
+   (sleep 120 ; q kill -9 $CAPTURE_PID_HEAD ; ) &
+   q wait $CAPTURE_PID_HEAD
+   q kill -9 $CAPTURE_PID_SSH
+   q wait $CAPTURE_PID_SSH
 }
 
-get_event() { # number
-    local number=$1
-    [ -n "$number" ] || number=1
-
-    awk "NR==$number" "$EVENTS"
-}
-
-result_type() { # test type [n]
-    local test=$1 type=$2 number=$3
-    [ -n "$number" ] || number=1
+result_type() { # test type [expected_count]
+    local test=$1 type=$2 expected_count=$3
+    [ -n "$expected_count" ] || expected_count=1
     wait_event
-    local event=$(get_event "$number")
-    echo "$event" | grep -q "\"type\":\"$type\""
-    result "$test $type" "$event"
+    local actual_count=$(grep -c "\"type\":\"$type\"" "$EVENTS")
+    result_out "$test $type" "$expected_count $type event(s)" "$actual_count $type event(s)"
 }
 
 # ------------------------- Usage ---------------------------
@@ -195,7 +199,6 @@
 EVENTS_PLUGIN=$TEST_DIR/events-plugin
 EVENT_FIFO=$TEST_DIR/event-fifo
 EVENTS=$TEST_DIR/events
-mkfifo "$EVENT_FIFO"
 
 trap cleanup EXIT
 
@@ -209,7 +212,7 @@
 capture_events 2
 ch1=$(create_change --draft "$REF_BRANCH" "$FILE_A") || exit
 result_type "$GROUP" "$type"
-result_type "$GROUP $type" "ref-updated" 2
+result_type "$GROUP $type" "ref-updated"
 
 type=draft-published
 capture_events
@@ -238,8 +241,8 @@
 is_plugin_installed reviewnotes && events_count=3
 capture_events "$events_count"
 submit "$ch1,1"
-result_type "$GROUP $type" "ref-updated"
-result_type "$GROUP" "$type" "$events_count"
+result_type "$GROUP $type" "ref-updated" "$((events_count-1))"
+result_type "$GROUP" "$type"
 
 # reviewer-added needs to be tested via Rest-API