File: 11counter.t

package info (click to toggle)
libnet-prometheus-perl 0.14-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 304 kB
  • sloc: perl: 1,847; makefile: 8
file content (70 lines) | stat: -rw-r--r-- 1,535 bytes parent folder | download
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
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Net::Prometheus::Counter;

sub HASHfromSample
{
   my ( $sample ) = @_;
   return { map { $_, $sample->$_ } qw( varname labels value ) };
}

{
   my $counter = Net::Prometheus::Counter->new(
      name => "test_total",
      help => "A testing counter",
   );

   ok( defined $counter, 'defined $counter' );

   my @samples = $counter->samples;
   is( scalar @samples, 1, '$counter->samples yields 1 sample' );

   is( HASHfromSample( $samples[0] ),
      { varname => "test_total", labels => [], value => 0 },
      '$samples[0] initially'
   );

   $counter->inc;

   is( HASHfromSample( ( $counter->samples )[0] ),
      { varname => "test_total", labels => [], value => 1 },
      '$samples[0]'
   );
}

# Remove and clear
{
   my $counter = Net::Prometheus::Counter->new(
      name   => "removal_test",
      help   => "A counter for testing removal",
      labels => [qw( x )],
   );

   $counter->inc( { x => "one" }, 1 );
   $counter->inc( { x => "two" }, 2 );
   $counter->inc( { x => "three" }, 3 );

   is( [ map { $_->labels } $counter->samples ],
      # Grr sorting
      [ [ x => "one" ], [ x => "three" ], [ x => "two" ] ],
      'labels before ->remove' );

   $counter->remove( { x => "one" } );

   is( [ map { $_->labels } $counter->samples ],
      [ [ x => "three" ], [ x => "two" ] ],
      'labels after ->remove' );

   $counter->clear;

   is( [ map { $_->labels } $counter->samples ],
      [],
      'labels after ->clear' );
}

done_testing;