#!/bin/bash
# SPDX-License-Identifier: GPL-3.0+
# Copyright (C) 2024 Martin Wilck, SUSE LLC
#
# Repeatedly rescans nvme controllers while doing IO on an nvme namespace
# connected to these controllers, and make sure that no I/O errors or data
# corruption occurs.

. tests/nvme/rc

DESCRIPTION="test controller rescan under I/O load"
TIMED=1
: "${TIMEOUT:=60}"

get_sleep_time() {
	local duration=$((RANDOM % 50 + 1))

	echo "$((duration / 10)).$((duration % 10))"
}

rescan_controller() {
	local path finish

	path="$1/rescan_controller"

	[[ -f "$path" ]] || {
		echo "cannot rescan $1"
		return 1
	}

	finish=$(($(date +%s) + TIMEOUT))
	while [[ $(date +%s) -le $finish ]]; do
		# sleep interval between 0.1 and 5s
		sleep "$(get_sleep_time)"
		echo 1 >"$path"
	done
}

test_device() {
	local -a ctrls
	local i st line

	echo "Running ${TEST_NAME}"

	while IFS= read -r line; do
		ctrls+=("$line")
	done < <(_nvme_get_ctrl_list)
	_run_fio_verify_io --filename="$TEST_DEV" --time_based &> "$FULL" &

	for i in "${!ctrls[@]}"; do
		rescan_controller "${ctrls[$i]}" "$i"
	done

	while true; do
		wait -n &>/dev/null
		st=$?
		case $st in
			127)
				break
				;;
			0)
				;;
			*)
				echo "child process exited with $st!"
				;;
		esac
	done

	echo "Test complete"
}
