lock-expiration.t 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 2 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, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. require 't/test.pl';
  19. package OddMuse;
  20. use Test::More tests => 17;
  21. AppendStringToFile($ConfigFile, "\$SurgeProtection = 1;\n");
  22. $localhost = 'confusibombus';
  23. $ENV{'REMOTE_ADDR'} = $localhost;
  24. my $lock = $LockDir . 'visitors';
  25. ok(! -d $lock, 'visitors lock does not exist yet');
  26. ok(! -f $VisitorFile, 'visitors log does not exist yet');
  27. # Don't loop forever trying to remove a lock older than
  28. # $LockExpiration that cannot be removed (eg. if the script user was
  29. # changed, so that the old lockfile cannot be removed by the new
  30. # user). Locks are directories; we simulate a lock that cannot be
  31. # removed by creating a file with the same name instead.
  32. mkdir($TempDir);
  33. ok(open(F, '>', $lock), "create bogus ${LockDir}visitors");
  34. my $ts = time - 120;
  35. utime($ts, $ts, $lock); # change mtime of the lockfile
  36. $ts = time;
  37. get_page('fail-to-get-lock');
  38. my $waiting = time - $ts;
  39. ok($waiting >= 16, "waited $waiting seconds (min. 16)");
  40. unlink($LockDir . 'visitors');
  41. $ts = time;
  42. test_page(get_page('get-lock'), 'get-lock');
  43. my $waiting = time - $ts;
  44. ok($waiting <= 2, "waited $waiting seconds (max. 2)");
  45. # The main lock works as intended.
  46. RequestLockOrError();
  47. update_page('cannot', 'create');
  48. test_page($redirect, 'Status: 503 SERVICE UNAVAILABLE',
  49. 'Could not get main lock');
  50. ReleaseLock();
  51. my $ts = (stat($VisitorFile))[10];
  52. ok($Now - $ts <= 3, 'visitors log recently modified');
  53. # Create a non-essential lock and make sure the lock directory is
  54. # created, and that it remains even if no error occurs.
  55. RequestLockDir('visitors');
  56. ok(-d $LockDir . 'visitors', 'visitors lock created');
  57. update_page('Test', 'page created');
  58. test_page($redirect, 'Status: 503 SERVICE UNAVAILABLE',
  59. "create the directory $TempDir");
  60. ok(-d $LockDir . 'visitors', 'visitors lock remained');
  61. ok($ts == (stat($VisitorFile))[10], 'visitors log was not modified');
  62. AppendStringToFile($ConfigFile, "\$LockExpiration = 3;\n");
  63. test_page(update_page('Test', 'page updated'), 'page updated');
  64. ok(! -d $LockDir . 'visitors', 'visitors lock expired');
  65. ok($ts != (stat($VisitorFile))[10], 'visitors log was modified');