1 # This test file covers POSIX file locking
3 # This file contains a collection of tests for one or more of the Tcl built-in
4 # commands. Sourcing this file into Tcl runs the tests and generates output
5 # for errors. No output means no errors were found.
7 # Copyright (c) 2003-2009 Donal K. Fellows
8 # See the file "license.terms" for information on usage and redistribution of
9 # this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 source [file dirname [info script]]/testing.tcl
14 testConstraint aio.lock [expr {"lock" in [stdin -commands]}]
15 needs constraint aio.lock
17 set fh [open locktest.file w]
19 test lock-1.1 {grab lock} {
23 test lock-1.2 {grab lock again} {
27 test lock-1.j {release lock} {
31 test lock-1.4 {release lock again} {
35 test lock-1.5 {grab lock from sub-process} {
36 # Run a child process that grabs the lock for 0.5 seconds
37 set pid [exec [info nameofexecutable] -e {set fh [open locktest.file r+]; $fh lock; sleep 0.5} >/dev/null &]
39 # Try to grab the lock - should fail
45 test lock-1.6 {wait for lock} {
46 # Run a child process that grabs the lock for 0.5 seconds
47 set pid [exec [info nameofexecutable] -e {set fh [open locktest.file r+]; $fh lock; sleep 0.5} >/dev/null &]
48 # And wait to acquire the lock in the parent. Should take ~500ms
49 set start [clock millis]
52 set delta [expr {[clock millis] - $start}]
54 error "Lock acquired after ${delta}ms"
59 file delete locktest.file