summaryrefslogtreecommitdiffstats
path: root/metrics/src/tests/metrictest.cpp
blob: f232f9b69aa2f85ef2e7962c1a74c31112911038 (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vdstestlib/cppunit/macros.h>
#include <vespa/metrics/valuemetric.h>
#include <vespa/metrics/countmetric.h>

namespace metrics {

struct MetricTest : public CppUnit::TestFixture
{
    template <typename MetricImpl>
    void testMetricsGetDimensionsAsPartOfMangledNameImpl();
    template <typename MetricImpl>
    void testMangledNameMayContainMultipleDimensionsImpl();

    void valueMetricsGetDimensionsAsPartOfMangledName();
    void countMetricsGetDimensionsAsPartOfMangledName();
    void valueMetricMangledNameMayContainMultipleDimensions();
    void countMetricMangledNameMayContainMultipleDimensions();
    void mangledNameListsDimensionsInLexicographicOrder();
    void manglingDoesNotChangeOriginalMetricName();
    void legacyTagsDoNotCreateMangledName();

    CPPUNIT_TEST_SUITE(MetricTest);
    CPPUNIT_TEST(valueMetricsGetDimensionsAsPartOfMangledName);
    CPPUNIT_TEST(countMetricsGetDimensionsAsPartOfMangledName);
    CPPUNIT_TEST(valueMetricMangledNameMayContainMultipleDimensions);
    CPPUNIT_TEST(countMetricMangledNameMayContainMultipleDimensions);
    CPPUNIT_TEST(mangledNameListsDimensionsInLexicographicOrder);
    CPPUNIT_TEST(manglingDoesNotChangeOriginalMetricName);
    CPPUNIT_TEST(legacyTagsDoNotCreateMangledName);
    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(MetricTest);

// Metric subclasses have the same constructor parameters, so we template
// our way from having to duplicate code. Templated GTest fixtures would be
// a far more elegant solution.
template <typename MetricImpl>
void
MetricTest::testMetricsGetDimensionsAsPartOfMangledNameImpl()
{
    MetricImpl m("test", {{"foo", "bar"}}, "description goes here");
    CPPUNIT_ASSERT_EQUAL(std::string("test{foo:bar}"), m.getMangledName());
}

template <typename MetricImpl>
void
MetricTest::testMangledNameMayContainMultipleDimensionsImpl()
{
    MetricImpl m("test",
                {{"flarn", "yarn"}, {"foo", "bar"}},
                "description goes here");
    CPPUNIT_ASSERT_EQUAL(std::string("test{flarn:yarn,foo:bar}"),
                         m.getMangledName());
}

void
MetricTest::valueMetricsGetDimensionsAsPartOfMangledName()
{
    testMetricsGetDimensionsAsPartOfMangledNameImpl<LongValueMetric>();
}

void
MetricTest::countMetricsGetDimensionsAsPartOfMangledName()
{
    testMetricsGetDimensionsAsPartOfMangledNameImpl<LongCountMetric>();
}

void
MetricTest::valueMetricMangledNameMayContainMultipleDimensions()
{
    testMangledNameMayContainMultipleDimensionsImpl<LongValueMetric>();
}

void
MetricTest::countMetricMangledNameMayContainMultipleDimensions()
{
    testMangledNameMayContainMultipleDimensionsImpl<LongCountMetric>();
}

// Assuming the above tests pass, we simplify by not requiring all subclasses
// to be tested since propagation down to the base class has already been
// verified.
void
MetricTest::mangledNameListsDimensionsInLexicographicOrder()
{
    LongValueMetric m("test",
                      {{"xyz", "bar"}, {"abc", "foo"}, {"def", "baz"}},
                      "");
    CPPUNIT_ASSERT_EQUAL(std::string("test{abc:foo,def:baz,xyz:bar}"),
                         m.getMangledName());
}

void
MetricTest::manglingDoesNotChangeOriginalMetricName()
{
    LongValueMetric m("test", {{"foo", "bar"}}, "");
    CPPUNIT_ASSERT_EQUAL(std::string("test"), m.getName());
}

void
MetricTest::legacyTagsDoNotCreateMangledName()
{
    LongValueMetric m("test", "foo bar", "");
    CPPUNIT_ASSERT_EQUAL(std::string("test"), m.getName());
    CPPUNIT_ASSERT_EQUAL(std::string("test"), m.getMangledName());
}

} // metrics