File: timing.t

package info (click to toggle)
libtest-effects-perl 0.002000-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 216 kB
  • sloc: perl: 505; makefile: 2
file content (60 lines) | stat: -rw-r--r-- 1,597 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use Test::Effects;

# How long we're going to test timing for...
my $sleepy_time = 0.1;

# Use a +/- 34% margin of error...
my ($min, $max) = ($sleepy_time * 0.6, $sleepy_time * 1.4);

# Short sleep...
sub nap {
    my $time = shift;
    select undef, undef, undef, $time;
    return $time;
}

# Select a random testing context...
my @contexts = (
    [ void_return   => undef          ],
    [ scalar_return => $sleepy_time   ],
    [ list_return   => [$sleepy_time] ],
);

# The various possible test specifications...
my %timing_spec = (
    'empty hash'   => {},
    'empty array'  => [],
    'number'       => $max,
    'array'        => [$min, $max],
    'hash min'     => { min => $min },
    'hash max'     => { max => $max },
    'hash min/max' => { min => $min, max => $max },
);

# How many tests in total???
plan 
    -e '.developer' ? (tests =>  2 * keys(%timing_spec) * @contexts )
                    : (skip_all => 'Developer test only' );

# Run them all...
for my $context (@contexts) {
    for my $test (keys %timing_spec) {
        # Test quietly...
        effects_ok { nap $sleepy_time }
                {
                        timing => $timing_spec{$test},
                        @$context,
                }
                => "Didn't oversleep: $test under $context->[0] ";

        # Test verbosely...
        effects_ok { nap $sleepy_time }
                VERBOSE {
                        timing => $timing_spec{$test},
                        @$context,
                }
                => "Didn't oversleep: $test under $context->[0]";
    }
}

done_testing();