You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

151 lines
4.3 KiB

25 years ago
  1. # See the file LICENSE for redistribution information.
  2. #
  3. # Copyright (c) 1996, 1997, 1998, 1999, 2000
  4. # Sleepycat Software. All rights reserved.
  5. #
  6. # $Id: lock002.tcl,v 11.10 2000/08/25 14:21:51 sue Exp $
  7. #
  8. # Exercise basic multi-process aspects of lock.
  9. proc lock002 { {maxlocks 1000} {conflicts {0 0 0 0 0 1 0 1 1} } } {
  10. source ./include.tcl
  11. puts "Lock002: Basic multi-process lock tests."
  12. env_cleanup $testdir
  13. set nmodes [isqrt [llength $conflicts]]
  14. # Open the lock
  15. mlock_open $maxlocks $nmodes $conflicts
  16. mlock_wait
  17. }
  18. # Make sure that we can create a region; destroy it, attach to it,
  19. # detach from it, etc.
  20. proc mlock_open { maxl nmodes conflicts } {
  21. source ./include.tcl
  22. puts "Lock002.a multi-process open/close test"
  23. # Open/Create region here. Then close it and try to open from
  24. # other test process.
  25. set env_cmd [concat "berkdb env -create -mode 0644 \
  26. -lock -lock_max $maxl -lock_conflict" \
  27. [list [list $nmodes $conflicts]] "-home $testdir"]
  28. set local_env [eval $env_cmd]
  29. error_check_good env_open [is_valid_env $local_env] TRUE
  30. set ret [$local_env close]
  31. error_check_good env_close $ret 0
  32. # Open from other test process
  33. set env_cmd "berkdb env -mode 0644 -home $testdir"
  34. set f1 [open |$tclsh_path r+]
  35. puts $f1 "source $test_path/test.tcl"
  36. set remote_env [send_cmd $f1 $env_cmd]
  37. error_check_good remote:env_open [is_valid_env $remote_env] TRUE
  38. # Now make sure that we can reopen the region.
  39. set local_env [eval $env_cmd]
  40. error_check_good env_open [is_valid_env $local_env] TRUE
  41. set ret [$local_env close]
  42. error_check_good env_close $ret 0
  43. # Try closing the remote region
  44. set ret [send_cmd $f1 "$remote_env close"]
  45. error_check_good remote:lock_close $ret 0
  46. # Try opening for create. Will succeed because region exists.
  47. set env_cmd [concat "berkdb env -create -mode 0644 \
  48. -lock -lock_max $maxl -lock_conflict" \
  49. [list [list $nmodes $conflicts]] "-home $testdir"]
  50. set local_env [eval $env_cmd]
  51. error_check_good remote:env_open [is_valid_env $local_env] TRUE
  52. # close locally
  53. reset_env $local_env
  54. # Close and exit remote
  55. set ret [send_cmd $f1 "reset_env $remote_env"]
  56. catch { close $f1 } result
  57. }
  58. proc mlock_wait { } {
  59. source ./include.tcl
  60. puts "Lock002.b multi-process get/put wait test"
  61. # Open region locally
  62. set env_cmd "berkdb env -lock -home $testdir"
  63. set local_env [eval $env_cmd]
  64. error_check_good env_open [is_valid_env $local_env] TRUE
  65. # Open region remotely
  66. set f1 [open |$tclsh_path r+]
  67. puts $f1 "source $test_path/test.tcl"
  68. set remote_env [send_cmd $f1 $env_cmd]
  69. error_check_good remote:env_open [is_valid_env $remote_env] TRUE
  70. # Get a write lock locally; try for the read lock
  71. # remotely. We hold the locks for several seconds
  72. # so that we can use timestamps to figure out if the
  73. # other process waited.
  74. set locker 1
  75. set local_lock [$local_env lock_get write $locker object1]
  76. error_check_good lock_get [is_valid_lock $local_lock $local_env] TRUE
  77. # Now request a lock that we expect to hang; generate
  78. # timestamps so we can tell if it actually hangs.
  79. set locker 2
  80. set remote_lock [send_timed_cmd $f1 1 \
  81. "set lock \[$remote_env lock_get write $locker object1\]"]
  82. # Now sleep before releasing lock
  83. tclsleep 5
  84. set result [$local_lock put]
  85. error_check_good lock_put $result 0
  86. # Now get the result from the other script
  87. set result [rcv_result $f1]
  88. error_check_good lock_get:remote_time [expr $result > 4] 1
  89. # Now get the remote lock
  90. set remote_lock [send_cmd $f1 "puts \$lock"]
  91. error_check_good remote:lock_get \
  92. [is_valid_lock $remote_lock $remote_env] TRUE
  93. # Now make the other guy wait 5 second and then release his
  94. # lock while we try to get a write lock on it
  95. set start [timestamp -r]
  96. set ret [send_cmd $f1 "tclsleep 5"]
  97. set ret [send_cmd $f1 "$remote_lock put"]
  98. set locker 1
  99. set local_lock [$local_env lock_get write $locker object1]
  100. error_check_good lock_get:time \
  101. [expr [expr [timestamp -r] - $start] > 2] 1
  102. error_check_good lock_get:local \
  103. [is_valid_lock $local_lock $local_env] TRUE
  104. # Now check remote's result
  105. set result [rcv_result $f1]
  106. error_check_good lock_put:remote $result 0
  107. # Clean up remote
  108. set ret [send_cmd $f1 "reset_env $remote_env"]
  109. close $f1
  110. # Now close up locally
  111. set ret [$local_lock put]
  112. error_check_good lock_put $ret 0
  113. reset_env $local_env
  114. }