#!/usr/share/ucs-test/runner bash
# shellcheck shell=bash
## desc: Concurrent setting and getting of UCR variables
## bugs: [31725]
## tags: [basic]
## exposure: careful
## versions:
##  3.1-0: skip
##  3.1-1: fixed

# shellcheck source=../../lib/random.sh
. "$TESTLIBPATH/random.sh" || exit 137
# shellcheck source=../../lib/base.sh
. "$TESTLIBPATH/base.sh" || exit 137

RETVAL=0

VAR_COUNT=500
PREFIX="$(random_chars 128)"

cleanup () {
	[ -e "$resultFile" ] && rm -f "$resultFile"
	seq -f "${PREFIX}_%.0f" 0 "$VAR_COUNT" | xargs ucr unset
	[ -n "$pid" ] && ps "$pid" >/dev/null 2>&1 && kill "$pid"
	wait
}
trap cleanup EXIT

echo "Starting at $(date)"
ucr set "${PREFIX}_400=value400"

resultFile=$(mktemp)
while [ -w "$resultFile" ]
do
	if [ "$(ucr get "${PREFIX}_400")" != "value400" ]
	then
		echo "Error at $(date)" | tee -a "$resultFile"
	fi
done &
pid=$!

echo "Setting UCR variables"
for ((i=0;i<=VAR_COUNT;i++)); do ucr set "${PREFIX}_${i}=value${i}"; done

echo "Stopping at $(date)"

if [ -s "$resultFile" ]; then
	echo "UCR get failed at:"
	cat "$resultFile"
	fail_test 110
fi

echo "Starting cleanup"

exit $RETVAL
