big-brother.t 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # Copyright (C) 2009–2015 Alex Schroeder <alex@gnu.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 => 28;
  18. add_module('big-brother.pl');
  19. my $ts = time;
  20. $VisitorTime = 10;
  21. AppendStringToFile($ConfigFile,
  22. "\$SurgeProtection = 1;\n"
  23. . "\$VisitorTime = $VisitorTime;\n");
  24. get_page('action=browse id=SomePage username=Alex');
  25. get_page('username=Berta pwd=foo');
  26. my $visitors = get_page('action=visitors');
  27. my $item = xpath_test($visitors,
  28. '//li[contains(., "Alex")]');
  29. like($item, qr/Alex was here (just now|\d seconds? ago) and read SomePage/,
  30. 'Alex was here and read SomePage');
  31. my $item = xpath_test($visitors,
  32. '//li[contains(., "Berta")]');
  33. like($item, qr/Berta was here (just now|\d seconds? ago) and read some action/,
  34. 'Berta was here and read some action');
  35. unlike($item, qr/pwd/, 'Link does not contain password');
  36. # check surge protection still works (taking into account the previous
  37. # get_page call for username=Alex
  38. for (2 .. $SurgeProtectionViews - 1) {
  39. test_page(get_page('action=browse id=HomePage username=Alex'),
  40. 'Status: 404 NOT FOUND');
  41. }
  42. my $load = 0;
  43. test_page(get_page('action=browse id=OneExtraPage username=Alex'),
  44. 'Status: 503 SERVICE UNAVAILABLE');
  45. my ($status, $data) = ReadFile($VisitorFile);
  46. ok($status, "Read $VisitorFile");
  47. %BigBrotherData = ();
  48. foreach (split(/\n/,$data)) {
  49. my ($name, %entries) = split /$FS/;
  50. $BigBrotherData{$name} = \%entries if $name and %entries;
  51. }
  52. my %entries = %{$BigBrotherData{Alex}};
  53. my @times = sort keys %entries;
  54. # Under heavy load, we might have less...
  55. ok(@times <= $SurgeProtectionViews, "$SurgeProtectionViews entries in the log file, or less");
  56. # Since the latest entry into the log gets a +1 added whenever there is
  57. # a duplicate entry, we might have entries in the list that are a few
  58. # seconds into the future. Take this into account as we are waiting for
  59. # $VisitorTime to expire.
  60. my $seconds = $VisitorTime + $times[-1] - time() + 1;
  61. diag("Waiting for ${seconds}s");
  62. sleep $seconds;
  63. test_page(get_page('action=browse id=AfterWaiting username=Alex'),
  64. 'Status: 404 NOT FOUND');
  65. # now the previous 10 entries should have expired out of the log file
  66. ($status, $data) = ReadFile($VisitorFile);
  67. %BigBrotherData = ();
  68. foreach (split(/\n/,$data)) {
  69. my ($name, %entries) = split /$FS/;
  70. $BigBrotherData{$name} = \%entries if $name and %entries;
  71. }
  72. %entries = %{$BigBrotherData{Alex}};
  73. @times = sort keys %entries;
  74. ok(@times == 1, "just one entry remains in the log file after this wait ("
  75. . @times . ")");