File Coverage

blib/lib/Test/Stream/Plugin/SRand.pm
Criterion Covered Total %
statement 41 41 100.0
branch 13 14 92.8
condition 3 9 33.3
subroutine 11 11 100.0
pod 0 3 0.0
total 68 78 87.1


line stmt bran cond sub pod time code
1             package Test::Stream::Plugin::SRand;
2 1     1   365 use strict;
  1         2  
  1         22  
3 1     1   3 use warnings;
  1         2  
  1         82  
4              
5 1     1   4 use Test::Stream::Plugin qw/import/;
  1         1  
  1         5  
6 1     1   4 use Test::Stream::Sync();
  1         2  
  1         17  
7              
8 1     1   3 use Carp qw/carp/;
  1         1  
  1         42  
9              
10 1     1   4 use Test::Stream::Context qw/context/;
  1         1  
  1         4  
11              
12             my $ADDED_HOOK = 0;
13             my $SEED;
14             my $FROM;
15              
16 5     5 0 19 sub seed { $SEED }
17 4     4 0 12 sub from { $FROM }
18              
19             sub load_ts_plugin {
20 6     6 0 31 my $class = shift;
21 6         6 my $caller = shift;
22              
23 6 100       492 carp "SRand loaded multiple times, re-seeding rand"
24             if defined $SEED;
25              
26 6 100       20 if (@_) {
    100          
27 2         2 ($SEED) = @_;
28 2         3 $FROM = 'import arg'
29             }
30             elsif(exists $ENV{TS_RAND_SEED}) {
31 1         2 $SEED = $ENV{TS_RAND_SEED};
32 1         1 $FROM = 'environment variable'
33             }
34             else {
35 3         139 my @ltime = localtime;
36 3         17 $SEED = sprintf('%04d%02d%02d', 1900 + $ltime[5], 1 + $ltime[4], $ltime[3]);
37 3         5 $FROM = 'local date';
38             }
39              
40 6 100       10 $SEED = 0 unless $SEED;
41 6         11 srand($SEED);
42              
43 6 100       70 if ($ENV{HARNESS_IS_VERBOSE}) {
    100          
44             # If the harness is verbose then just display the message for all to
45             # see. It is nice info and they already asked for noisy output.
46             Test::Stream::Sync->post_load(sub {
47 3     3   6 my $ctx = context();
48 3         12 $ctx->note("Seeded srand with seed '$SEED' from $FROM.");
49 3         7 $ctx->release;
50 3         24 });
51             }
52             elsif (!$ADDED_HOOK++) {
53             # The seed can be important for debugging, so if anything is wrong we
54             # should output the seed message as a diagnostics message. This must be
55             # done at the very end, even later than a hub hook.
56             Test::Stream::Sync->add_hook(
57             sub {
58 1     1   2 my ($ctx, $real, $new) = @_;
59              
60 1 50 33     10 $ctx->diag("Seeded srand with seed '$SEED' from $FROM.")
      33        
      33        
61             if $real
62             || ($new && $$new)
63             || !$ctx->hub->state->is_passing;
64             }
65 1         9 );
66             }
67             }
68              
69             1;
70              
71             =pod
72              
73             =encoding UTF-8
74              
75             =head1 NAME
76              
77             Test::Stream::Plugin::SRand - Control the random seed for more controlled test
78             environments.
79              
80             =head1 DEPRECATED
81              
82             B in favor of L, L, and
83             L.
84              
85             See L for a conversion guide.
86              
87             =head1 DESCRIPTION
88              
89             This module gives you control over the random seed used for your unit tests. In
90             some testing environments the random seed can play a major role in results.
91              
92             The default configuration for this module will seed srand with the local date.
93             Using the date as the seed means that on any given day the random seed will
94             always be the same, this means behavior will not change from run to run on a
95             given day. However the seed is different on different days allowing you to be
96             sure the code still works with actual randomness.
97              
98             The seed is printed for you on failure, or when the harness is verbose. You can
99             use the C environment variable to specify the seed. You can also
100             provide a specific seed as a load-time argument to the plugin.
101              
102             =head1 SYNOPSIS
103              
104             Loading the plugin is easy, and the defaults are sane:
105              
106             use Test::Stream 'SRand';
107              
108             Custom seed:
109              
110             use Test::Stream SRand => ['42'];
111              
112             =head1 NOTE ON LOAD ORDER
113              
114             If you use this plugin you probably want to use it as the first, or near-first
115             plugin. C is not called until the plugin is loaded, so other plugins
116             loaded first may already be making use of random numbers before your seed
117             takes effect.
118              
119             =head1 SOURCE
120              
121             The source code repository for Test::Stream can be found at
122             F.
123              
124             =head1 MAINTAINERS
125              
126             =over 4
127              
128             =item Chad Granum Eexodist@cpan.orgE
129              
130             =back
131              
132             =head1 AUTHORS
133              
134             =over 4
135              
136             =item Chad Granum Eexodist@cpan.orgE
137              
138             =back
139              
140             =head1 COPYRIGHT
141              
142             Copyright 2015 Chad Granum Eexodist7@gmail.comE.
143              
144             This program is free software; you can redistribute it and/or
145             modify it under the same terms as Perl itself.
146              
147             See F
148              
149             =cut