lock-expiration.t 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. # Copyright (C) 2007 Alex Schroeder <alex@emacswiki.org>
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 3 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. require './t/test.pl';
  16. package OddMuse;
  17. use Test::More tests => 17;
  18. AppendStringToFile($ConfigFile, "\$SurgeProtection = 1;\n");
  19. $localhost = 'confusibombus';
  20. $ENV{'REMOTE_ADDR'} = $localhost;
  21. my $lock = $LockDir . 'visitors';
  22. ok(! -d $lock, 'visitors lock does not exist yet');
  23. ok(! -f $VisitorFile, 'visitors log does not exist yet');
  24. # Don't loop forever trying to remove a lock older than
  25. # $LockExpiration that cannot be removed (eg. if the script user was
  26. # changed, so that the old lockfile cannot be removed by the new
  27. # user). Locks are directories; we simulate a lock that cannot be
  28. # removed by creating a file with the same name instead.
  29. mkdir($TempDir);
  30. ok(open(F, '>', $lock), "create bogus ${LockDir}visitors");
  31. my $ts = time - 120;
  32. utime($ts, $ts, $lock); # change mtime of the lockfile
  33. $ts = time;
  34. get_page('fail-to-get-lock');
  35. my $waiting = time - $ts;
  36. ok($waiting >= 16, "waited $waiting seconds (min. 16)");
  37. unlink($LockDir . 'visitors');
  38. $ts = time;
  39. test_page(get_page('get-lock'), 'get-lock');
  40. my $waiting = time - $ts;
  41. ok($waiting <= 2, "waited $waiting seconds (max. 2)");
  42. # The main lock works as intended.
  43. RequestLockOrError();
  44. update_page('cannot', 'create');
  45. test_page($redirect, 'Status: 503 SERVICE UNAVAILABLE',
  46. 'Could not get main lock');
  47. ReleaseLock();
  48. my $ts = (stat($VisitorFile))[10];
  49. ok($Now - $ts <= 3, 'visitors log recently modified');
  50. # Create a non-essential lock and make sure the lock directory is
  51. # created, and that it remains even if no error occurs.
  52. RequestLockDir('visitors');
  53. ok(-d $LockDir . 'visitors', 'visitors lock created');
  54. update_page('Test', 'page created');
  55. test_page($redirect, 'Status: 503 SERVICE UNAVAILABLE',
  56. "create the directory $TempDir");
  57. ok(-d $LockDir . 'visitors', 'visitors lock remained');
  58. ok($ts == (stat($VisitorFile))[10], 'visitors log was not modified');
  59. AppendStringToFile($ConfigFile, "\$LockExpiration = 3;\n");
  60. test_page(update_page('Test', 'page updated'), 'page updated');
  61. ok(! -d $LockDir . 'visitors', 'visitors lock expired');
  62. ok($ts != (stat($VisitorFile))[10], 'visitors log was modified');