1
0

Waiter.t 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #! /usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Test::More;
  5. use Test::Warn;
  6. use Test::Fatal;
  7. use Time::Mock throttle => 100;
  8. use Selenium::Waiter;
  9. SIMPLE_WAIT: {
  10. my $ret;
  11. waits_ok( sub { $ret = wait_until { 1 } }, '<', 5, 'immediately true returns quickly' );
  12. ok($ret == 1, 'return value for a true wait_until is passed up');
  13. waits_ok( sub { $ret = wait_until { 0 } }, '>', 25, 'never true expires the timeout' );
  14. ok($ret eq '', 'return value for a false wait is an empty string');
  15. }
  16. EVENTUALLY: {
  17. my $ret = 0;
  18. waits_ok( sub { wait_until { $ret++ > 2 } }, '>', 2, 'eventually true takes time');
  19. $ret = 0;
  20. my %opts = ( interval => 2, timeout => 5 );
  21. waits_ok(
  22. sub { wait_until { $ret++; 0 } %opts }, '>', 4,
  23. 'timeout is respected'
  24. );
  25. ok(1 <= $ret && $ret <= 3, 'interval option changes iteration speed');
  26. }
  27. EXCEPTIONS: {
  28. my %opts = ( timeout => 2 );
  29. warning_is { wait_until { die 'caught!' } %opts } 'caught!',
  30. 'exceptions usually only warn once';
  31. # This test is flaky when accelerated, so let's slow it down.
  32. Time::Mock->throttle(1);
  33. my %debug = ( debug => 1, %opts );
  34. warnings_are { wait_until { die 'caught!' } %debug } ['caught!', 'caught!'],
  35. 'exceptions warn repreatedly when in debug mode';
  36. }
  37. sub waits_ok {
  38. my ($sub, $cmp, $expected_duration, $test_desc) = @_;
  39. my $start = time;
  40. $sub->();
  41. my $elapsed = time - $start;
  42. cmp_ok($elapsed, $cmp, $expected_duration, $test_desc);
  43. }
  44. done_testing;