#
# Test case for continuous operation
#

#
# log message with timestamp
#
log_with_time()
{
	echo "$PROGRAM: [`date +"%y/%m/%d %H:%M"`] $@" 1>&2
	return 0
}

#
# Test parameters
#
# GFARM_HOME:	A directory path of Gfarm which this test will be executed
# WRITE_SIZE:	Size to write to a file at a time
# INTERVAL:	Interval(sec) of operation
#
setup_continuous_operation()
{
	# define mount point
	MOUNT_POINT=mnt_$TEST_EXEC_ID

	# default parameter
	GFARM_HOME=${GFARM_HOME-"/"}
	WRITE_SIZE=${WRITE_SIZE="512K"}
	INTERVAL=${INTERVAL-"10"}

	# prepare: check whether required host is defined
	$GFSERVICE systest::gfarm_v2::check_hostids_defined - "gfmd1 client1"
	[ $? -ne 0 ] && return $RESULT_UNSUPPORTED

	# prepare: mount
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 mkdir $MOUNT_POINT
	$GFSERVICE mount client1 $MOUNT_POINT -o modules=subdir \
		-o subdir=$GFARM_HOME

	return $RESULT_PASS
}

test_continuous_operation()
{
	RESULT=$RESULT_PASS

	# file count
	I=0

	# file name
	FILE_TMPL="$MOUNT_POINT/${TEST_EXEC_ID}_%d.txt"

	while true; do
		FILE_PATH=`printf $FILE_TMPL $I`

		# step: create a file
		$GFSERVICE systest::gfarm2fs::make_file client1 $FILE_PATH $WRITE_SIZE
		if [ $? -eq 0 ]; then
			log_with_time "succeeded to create a file: $FILE_PATH"
		else
			log_with_time "failed to create a file: $FILE_PATH"
			set_result RESULT $RESULT_FAIL
		fi

		# step: append to a file
		$GFSERVICE systest::gfarm2fs::append_file client1 $FILE_PATH $WRITE_SIZE
		if [ $? -eq 0 ]; then
			log_with_time "succeeded to append to a file:" \
				"$FILE_PATH"
		else
			log_with_time "failed to append to a file: $FILE_PATH"
			set_result RESULT $RESULT_FAIL
		fi

		# step: read a file
		$GFSERVICE systest::gfarm2fs::read_file client1 $FILE_PATH
		if [ $? -eq 0 ]; then
			log_with_time "succeeded to read a file: $FILE_PATH"
		else
			log_with_time "failed to read a file: $FILE_PATH"
			set_result RESULT $RESULT_FAIL
		fi

		# step: remove a file
		$GFSERVICE systest::gfarm2fs::exec_ssh client1 rm -f $FILE_PATH
		if [ $? -eq 0 ]; then
			log_with_time "succeeded to remove a file: $FILE_PATH"
		else
			log_with_time "failed to remove a file: $FILE_PATH"
			set_result RESULT $RESULT_FAIL
		fi

		$GFSERVICE systest::gfarm2fs::exec_ssh client1 "test -f $MOUNT_POINT/stop" \
			2> /dev/null
		[ $? -eq 0 ] && break

		sleep $INTERVAL
		I=`expr $I + 1`
	done

	return $RESULT
}

teardown_continuous_operation()
{
	# cleanup: remove file
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rm -f $MOUNT_POINT/stop

	# cleanup: unmount
	$GFSERVICE umount client1 $MOUNT_POINT

	# cleanup: remove mount point
	$GFSERVICE systest::gfarm2fs::exec_ssh client1 rmdir $MOUNT_POINT

	return $RESULT_PASS
}
