File: 14adapter-test.t

package info (click to toggle)
libmetrics-any-perl 0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 220 kB
  • sloc: perl: 1,295; makefile: 2
file content (174 lines) | stat: -rw-r--r-- 4,678 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/perl

use v5.14;
use warnings;

use Test2::V0;

use Metrics::Any '$metrics';
use Metrics::Any::Adapter 'Test';

# Force the adapter to exist
$metrics->adapter;

ok( $metrics, '$metrics is still true' );

# unlabelled
{
   Metrics::Any::Adapter::Test->clear;

   $metrics->make_counter( abc => name => "the_ABC_counter" );
   $metrics->make_counter( def => name => [qw( the DEF counter )] );

   $metrics->inc_counter( 'abc' );

   $metrics->inc_counter( 'def' );
   $metrics->inc_counter( 'def' );
   $metrics->inc_counter( 'def' );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_counter = 1\n" .
      "the_DEF_counter = 3\n",
      'Metrics::Any::Adapter::Test->metrics for basic metrics'
   );
}

# with labels
{
   Metrics::Any::Adapter::Test->clear;

   $metrics->make_counter( ghi =>
      name   => "the_GHI_counter",
      labels => [qw( label )],
   );
   $metrics->make_counter( jkl =>
      name   => "the_JKL_counter",
      labels => [qw( x y )],
   );

   # inc by ARRAYref
   $metrics->inc_counter( ghi => [ label => "value" ] );
   $metrics->inc_counter( jkl => [ x => 10, y => 20 ] );
   is( Metrics::Any::Adapter::Test->metrics,
      "the_GHI_counter label:value = 1\n" .
      "the_JKL_counter x:10 y:20 = 1\n",
      'Metrics::Any::Adapter::Test->metrics after increment with label by ARRAYref'
   );

   # inc by HASHref
   $metrics->inc_counter( ghi => { label => "value" } );
   $metrics->inc_counter( jkl => { x => 10, y => 20 } );
   is( Metrics::Any::Adapter::Test->metrics,
      "the_GHI_counter label:value = 2\n" .
      "the_JKL_counter x:10 y:20 = 2\n",
      'Metrics::Any::Adapter::Test->metrics after increment with label by HASHref'
   );

   # legacy inc by list of values
   $metrics->inc_counter( ghi => "value" );
   $metrics->inc_counter( jkl => 10, 20 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_GHI_counter label:value = 3\n" .
      "the_JKL_counter x:10 y:20 = 3\n",
      'Metrics::Any::Adapter::Test->metrics after increment with label by legacy value list'
   );
}

# distributions
{
   Metrics::Any::Adapter::Test->clear;

   $metrics->make_distribution( distribution => name => "the_ABC_distribution" );

   $metrics->report_distribution( distribution => 150 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_distribution_count = 1\n" .
      "the_ABC_distribution_total = 150\n",
      'Metrics::Any::Adapter::Test->metrics after ->report_distribution'
   );
}

# gauges
{
   Metrics::Any::Adapter::Test->clear;

   $metrics->make_gauge( gauge => name => "the_ABC_gauge" );

   $metrics->set_gauge_to( gauge => 30 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_gauge = 30\n",
      'Metrics::Any::Adapter::Test->metrics after ->set_gauge_to'
   );

   $metrics->inc_gauge_by( gauge => 10 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_gauge = 40\n",
      'Metrics::Any::Adapter::Test->metrics after ->inc_gauge_by'
   );
}

# timers
{
   Metrics::Any::Adapter::Test->clear;

   $metrics->make_timer( timer => name => "the_ABC_timer" );

   $metrics->report_timer( timer => 0.02 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_timer_count = 1\n" .
      "the_ABC_timer_total = 0.02\n",
      'Metrics::Any::Adapter::Test->metrics after ->report_timer'
   );
}

# full distributions
{
   Metrics::Any::Adapter::Test->clear;

   Metrics::Any::Adapter::Test->use_full_distributions;

   $metrics->make_distribution( fulldist => name => "the_full_distribution" );

   $metrics->report_distribution( fulldist => 1 );
   $metrics->report_distribution( fulldist => 2 );
   $metrics->report_distribution( fulldist => 3 );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_full_distribution[1] = 1\n" .
      "the_full_distribution[2] = 1\n" .
      "the_full_distribution[3] = 1\n" .
      "the_full_distribution_count = 3\n",
      'Metrics::Any::Adapter::Test->metrics after ->report_distribution on full distribution'
   );
}

# batch mode is supported
{
   Metrics::Any::Adapter::Test->clear;

   my $called;

   my $batch_ok = $metrics->add_batch_mode_callback( sub {
      $called++;
      $metrics->inc_counter_by( abc => 100 );
      $metrics->set_gauge_to( gauge => 25 );
   } );
   ok( $batch_ok, 'Test adapter supports batch mode' );

   ok( !$called, 'Batch mode callback not yet invoked' );

   is( Metrics::Any::Adapter::Test->metrics,
      "the_ABC_counter = 100\n" .
      "the_ABC_gauge = 25\n",
      'Metrics::Any::Adapter::Test->metrics contains batch-reported values' );
   ok( $called, 'Batch mode callback invoked by ->metrics' );
}

ok( $metrics, '$metrics is still true at EOF' );

done_testing;