tests: socket.test check for ipv6 support
[jimtcl.git] / tests / lock.test
blobe7b0b2db4cfbcfc873980af1003c22b3812d51df
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
13 needs constraint jim
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} {
20         $fh lock
21 } 1
23 test lock-1.2 {grab lock again} {
24         $fh lock
25 } 1
27 test lock-1.j {release lock} {
28         $fh unlock
29 } 1
31 test lock-1.4 {release lock again} {
32         $fh unlock
33 } 1
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 &]
38         sleep 0.1
39         # Try to grab the lock - should fail
40         set stat [$fh lock]
41         sleep 0.5
42         set stat
43 } 0
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]
50         sleep 0.1
51         $fh lock -wait
52         set delta [expr {[clock millis] - $start}]
53         if {$delta < 100} {
54                 error "Lock acquired after ${delta}ms"
55         }
56 } {}
58 $fh close
59 file delete locktest.file
61 testreport