labels_ut.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #include "labels.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NMonitoring;
  4. Y_UNIT_TEST_SUITE(TLabelsTest) {
  5. TLabel pSolomon("project", "solomon");
  6. TLabel pKikimr("project", "kikimr");
  7. Y_UNIT_TEST(Equals) {
  8. UNIT_ASSERT(pSolomon == TLabel("project", "solomon"));
  9. UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Name(), "project");
  10. UNIT_ASSERT_STRINGS_EQUAL(pSolomon.Value(), "solomon");
  11. UNIT_ASSERT(pSolomon != pKikimr);
  12. }
  13. Y_UNIT_TEST(ToString) {
  14. UNIT_ASSERT_STRINGS_EQUAL(pSolomon.ToString(), "project=solomon");
  15. UNIT_ASSERT_STRINGS_EQUAL(pKikimr.ToString(), "project=kikimr");
  16. }
  17. Y_UNIT_TEST(FromString) {
  18. auto pYql = TLabel::FromString("project=yql");
  19. UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql"));
  20. UNIT_ASSERT_EQUAL(TLabel::FromString("k=v"), TLabel("k", "v"));
  21. UNIT_ASSERT_EQUAL(TLabel::FromString("k=v "), TLabel("k", "v"));
  22. UNIT_ASSERT_EQUAL(TLabel::FromString("k= v"), TLabel("k", "v"));
  23. UNIT_ASSERT_EQUAL(TLabel::FromString("k =v"), TLabel("k", "v"));
  24. UNIT_ASSERT_EQUAL(TLabel::FromString(" k=v"), TLabel("k", "v"));
  25. UNIT_ASSERT_EQUAL(TLabel::FromString(" k = v "), TLabel("k", "v"));
  26. UNIT_ASSERT_EXCEPTION_CONTAINS(
  27. TLabel::FromString(""),
  28. yexception,
  29. "invalid label string format");
  30. UNIT_ASSERT_EXCEPTION_CONTAINS(
  31. TLabel::FromString("k v"),
  32. yexception,
  33. "invalid label string format");
  34. UNIT_ASSERT_EXCEPTION_CONTAINS(
  35. TLabel::FromString(" =v"),
  36. yexception,
  37. "label name cannot be empty");
  38. UNIT_ASSERT_EXCEPTION_CONTAINS(
  39. TLabel::FromString("k= "),
  40. yexception,
  41. "label value cannot be empty");
  42. }
  43. Y_UNIT_TEST(TryFromString) {
  44. TLabel pYql;
  45. UNIT_ASSERT(TLabel::TryFromString("project=yql", pYql));
  46. UNIT_ASSERT_EQUAL(pYql, TLabel("project", "yql"));
  47. {
  48. TLabel label;
  49. UNIT_ASSERT(TLabel::TryFromString("k=v", label));
  50. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  51. }
  52. {
  53. TLabel label;
  54. UNIT_ASSERT(TLabel::TryFromString("k=v ", label));
  55. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  56. }
  57. {
  58. TLabel label;
  59. UNIT_ASSERT(TLabel::TryFromString("k= v", label));
  60. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  61. }
  62. {
  63. TLabel label;
  64. UNIT_ASSERT(TLabel::TryFromString("k =v", label));
  65. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  66. }
  67. {
  68. TLabel label;
  69. UNIT_ASSERT(TLabel::TryFromString(" k=v", label));
  70. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  71. }
  72. {
  73. TLabel label;
  74. UNIT_ASSERT(TLabel::TryFromString(" k = v ", label));
  75. UNIT_ASSERT_EQUAL(label, TLabel("k", "v"));
  76. }
  77. }
  78. Y_UNIT_TEST(Labels) {
  79. TLabels labels;
  80. UNIT_ASSERT(labels.Add(TStringBuf("name1"), TStringBuf("value1")));
  81. UNIT_ASSERT(labels.Size() == 1);
  82. UNIT_ASSERT(labels.Has(TStringBuf("name1")));
  83. {
  84. auto l = labels.Find("name1");
  85. UNIT_ASSERT(l.Defined());
  86. UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name1");
  87. UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value1");
  88. }
  89. {
  90. auto l = labels.Find("name2");
  91. UNIT_ASSERT(!l.Defined());
  92. }
  93. // duplicated name
  94. UNIT_ASSERT(!labels.Add(TStringBuf("name1"), TStringBuf("value2")));
  95. UNIT_ASSERT(labels.Size() == 1);
  96. UNIT_ASSERT(labels.Add(TStringBuf("name2"), TStringBuf("value2")));
  97. UNIT_ASSERT(labels.Size() == 2);
  98. UNIT_ASSERT(labels.Has(TStringBuf("name2")));
  99. {
  100. auto l = labels.Find("name2");
  101. UNIT_ASSERT(l.Defined());
  102. UNIT_ASSERT_STRINGS_EQUAL(l->Name(), "name2");
  103. UNIT_ASSERT_STRINGS_EQUAL(l->Value(), "value2");
  104. }
  105. UNIT_ASSERT_EQUAL(labels[0], TLabel("name1", "value1"));
  106. UNIT_ASSERT_EQUAL(labels[1], TLabel("name2", "value2"));
  107. TVector<TLabel> labelsCopy;
  108. for (auto&& label : labels) {
  109. labelsCopy.emplace_back(label.Name(), label.Value());
  110. }
  111. UNIT_ASSERT_EQUAL(labelsCopy, TVector<TLabel>({
  112. {"name1", "value1"},
  113. {"name2", "value2"},
  114. }));
  115. }
  116. Y_UNIT_TEST(Hash) {
  117. TLabel label("name", "value");
  118. UNIT_ASSERT_EQUAL(ULL(2378153472115172159), label.Hash());
  119. {
  120. TLabels labels = {{"name", "value"}};
  121. UNIT_ASSERT_EQUAL(ULL(5420514431458887014), labels.Hash());
  122. }
  123. {
  124. TLabels labels = {{"name1", "value1"}, {"name2", "value2"}};
  125. UNIT_ASSERT_EQUAL(ULL(2226975250396609813), labels.Hash());
  126. }
  127. }
  128. Y_UNIT_TEST(MakeEmptyLabels) {
  129. {
  130. auto labels = MakeLabels<TString>();
  131. UNIT_ASSERT(labels);
  132. UNIT_ASSERT(labels->Empty());
  133. UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0);
  134. }
  135. {
  136. auto labels = MakeLabels<TStringBuf>();
  137. UNIT_ASSERT(labels);
  138. UNIT_ASSERT(labels->Empty());
  139. UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 0);
  140. }
  141. }
  142. Y_UNIT_TEST(MakeLabelsFromInitializerList) {
  143. auto labels = MakeLabels<TString>({{"my", "label"}});
  144. UNIT_ASSERT(labels);
  145. UNIT_ASSERT(!labels->Empty());
  146. UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1);
  147. UNIT_ASSERT(labels->Has("my"));
  148. auto label = labels->Get("my");
  149. UNIT_ASSERT(label.has_value());
  150. UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my");
  151. UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label");
  152. }
  153. Y_UNIT_TEST(MakeLabelsFromOtherLabel) {
  154. auto labels = MakeLabels({{"my", "label"}});
  155. UNIT_ASSERT(labels);
  156. UNIT_ASSERT(!labels->Empty());
  157. UNIT_ASSERT_VALUES_EQUAL(labels->Size(), 1);
  158. UNIT_ASSERT(labels->Has("my"));
  159. auto label = labels->Get("my");
  160. UNIT_ASSERT(label.has_value());
  161. UNIT_ASSERT_STRINGS_EQUAL((*label)->Name(), "my");
  162. UNIT_ASSERT_STRINGS_EQUAL((*label)->Value(), "label");
  163. }
  164. }