-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathgauge_test.cc
More file actions
58 lines (51 loc) · 1.82 KB
/
gauge_test.cc
File metadata and controls
58 lines (51 loc) · 1.82 KB
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
#include "stateless_meters.h"
#include "test_publisher.h"
#include <gtest/gtest.h>
#include <cmath>
#include <limits>
namespace {
using spectator::Gauge;
using spectator::Id;
using spectator::Tags;
using spectator::TestPublisher;
TEST(Gauge, Set) {
TestPublisher publisher;
auto id = std::make_shared<Id>("gauge", Tags{});
auto id2 = std::make_shared<Id>("gauge2", Tags{{"key", "val"}});
Gauge g{id, &publisher};
Gauge g2{id2, &publisher};
g.Set(42);
g2.Set(2);
g.Set(1);
std::vector<std::string> expected = {"g:gauge:42", "g:gauge2,key=val:2",
"g:gauge:1"};
EXPECT_EQ(publisher.SentMessages(), expected);
}
TEST(Gauge, NaN) {
TestPublisher publisher;
auto id = std::make_shared<Id>("gauge", Tags{});
Gauge g{id, &publisher};
g.Set(std::numeric_limits<double>::quiet_NaN());
// Legacy absl::StrFormat("%f") produced "nan"; verify we preserve that.
std::vector<std::string> expected = {"g:gauge:nan"};
EXPECT_EQ(publisher.SentMessages(), expected);
}
TEST(Gauge, Infinity) {
TestPublisher publisher;
auto id = std::make_shared<Id>("gauge", Tags{});
Gauge g{id, &publisher};
g.Set(std::numeric_limits<double>::infinity());
g.Set(-std::numeric_limits<double>::infinity());
// Legacy absl::StrFormat("%f") produced "inf" / "-inf".
std::vector<std::string> expected = {"g:gauge:inf", "g:gauge:-inf"};
EXPECT_EQ(publisher.SentMessages(), expected);
}
TEST(Gauge, InvalidTags) {
TestPublisher publisher;
// test with a single tag, because tags order is not guaranteed in a flat_hash_map
auto id = std::make_shared<Id>("test`!@#$%^&*()-=~_+[]{}\\|;:'\",<.>/?foo",
Tags{{"tag1,:=", "value1,:="}});
Gauge g{id, &publisher};
EXPECT_EQ("g:test______^____-_~______________.___foo,tag1___=value1___:", g.GetPrefix());
}
} // namespace