mkql_stats_registry_ut.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #include "mkql_stats_registry.h"
  2. #include <library/cpp/testing/unittest/registar.h>
  3. using namespace NKikimr;
  4. using namespace NMiniKQL;
  5. Y_UNIT_TEST_SUITE(TStatsRegistryTest) {
  6. Y_UNIT_TEST(Stats) {
  7. static TStatKey key("key", false);
  8. auto stats = CreateDefaultStatsRegistry();
  9. stats->SetStat(key, 37);
  10. UNIT_ASSERT_EQUAL(stats->GetStat(key), 37);
  11. stats->AddStat(key, 3);
  12. UNIT_ASSERT_EQUAL(stats->GetStat(key), 40);
  13. stats->IncStat(key);
  14. UNIT_ASSERT_EQUAL(stats->GetStat(key), 41);
  15. stats->DecStat(key);
  16. UNIT_ASSERT_EQUAL(stats->GetStat(key), 40);
  17. stats->SetMaxStat(key, 3);
  18. UNIT_ASSERT_EQUAL(stats->GetStat(key), 40);
  19. stats->SetMaxStat(key, 43);
  20. UNIT_ASSERT_EQUAL(stats->GetStat(key), 43);
  21. stats->SetMinStat(key, 57);
  22. UNIT_ASSERT_EQUAL(stats->GetStat(key), 43);
  23. stats->SetMinStat(key, 34);
  24. UNIT_ASSERT_EQUAL(stats->GetStat(key), 34);
  25. }
  26. Y_UNIT_TEST(ForEach) {
  27. static TStatKey key1("key1", false), key2("key2", true);
  28. UNIT_ASSERT(!key1.IsDeriv());
  29. UNIT_ASSERT(key2.IsDeriv());
  30. auto stats = CreateDefaultStatsRegistry();
  31. stats->SetStat(key1, 42);
  32. stats->SetStat(key2, 37);
  33. stats->ForEachStat([](const TStatKey& key, i64 value) {
  34. if (key.GetName() == "key1") {
  35. UNIT_ASSERT_EQUAL(value, 42);
  36. } else if (key.GetName() == "key2") {
  37. UNIT_ASSERT_EQUAL(value, 37);
  38. } else {
  39. // depending on how to run tests other keys can be linked
  40. // together and appears here
  41. }
  42. });
  43. }
  44. Y_UNIT_TEST(DuplicateKeys) {
  45. TString error;
  46. try {
  47. static TStatKey key("my_key", false), sameKey("my_key", false);
  48. // avoid variables elimitation
  49. Cerr << key.GetId() << ": " << key.GetName() << '\n'
  50. << sameKey.GetId() << ": " << sameKey.GetName() << Endl;
  51. } catch (const yexception& e) {
  52. error = e.AsStrBuf();
  53. }
  54. UNIT_ASSERT(error.Contains("duplicated stat key: my_key"));
  55. }
  56. }