# This test file covers POSIX file locking
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands. Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# Copyright (c) 2003-2009 Donal K. Fellows
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

source [file dirname [info script]]/testing.tcl

needs constraint jim
testConstraint aio.lock [expr {"lock" in [stdin -commands]}]
needs constraint aio.lock

set fh [open locktest.file w]

test lock-1.1 {grab lock} {
	$fh lock
} 1

test lock-1.2 {grab lock again} {
	$fh lock
} 1

test lock-1.j {release lock} {
	$fh unlock
} 1

test lock-1.4 {release lock again} {
	$fh unlock
} 1

test lock-1.5 {grab lock from sub-process} {
	# Run a child process that grabs the lock for 0.5 seconds
	set pid [exec [info nameofexecutable] -e {set fh [open locktest.file r+]; $fh lock; sleep 0.5} >/dev/null &]
	sleep 0.1
	# Try to grab the lock - should fail
	set stat [$fh lock]
	sleep 0.5
	set stat
} 0

$fh close
file delete locktest.file

testreport
