# !/bin/bash
# parameter 1: port
# parameter 2: metric name
# parameter 3: retry count, if check failed we will wait 1s before next retry, until retry time exceeds retry count
# parameter 4...: valid value list

set -eu

port=$1
metric1_name=$2
metric2_name=$3
retry_count=$4

counter=0
while [ $counter -lt $retry_count ]; do
	metric1=$(curl -s http://127.0.0.1:$port/metrics | grep $metric1_name | grep -v "#" | awk '{print $2}')
	metric2=$(curl -s http://127.0.0.1:$port/metrics | grep $metric2_name | grep -v "#" | awk '{print $2}')
	if [ $metric1 -eq $metric2 ]; then
		echo "matched"
		exit 0
	fi
	((counter += 1))
	echo "wait for valid metric for $counter-th time"
	sleep 1
done

echo "metric1 and metric2 not equal"
exit 1
