PageRenderTime 36ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/test/common/cache/monitorcache.cpp

https://gitlab.com/admin-github-cloud/cynara
C++ | 95 lines | 55 code | 19 blank | 21 comment | 1 complexity | d95e76d58fadcca1768f82fcde4450fc MD5 | raw file
  1. /*
  2. * Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * @file test/common/cache/monitorcache.cpp
  18. * @author Aleksander Zdyb <a.zdyb@samsung.com>
  19. * @version 1.0
  20. * @brief Tests of MonitorCache
  21. */
  22. #include <gtest/gtest.h>
  23. #include <gmock/gmock.h>
  24. #include <ctime>
  25. #include "../../helpers.h"
  26. #include <common/types/MonitorEntry.h>
  27. #include <client-common/cache/MonitorCache.h>
  28. using namespace Cynara;
  29. namespace {
  30. int constClock(int, struct timespec *ts) {
  31. ts->tv_sec = 42;
  32. ts->tv_nsec = 666;
  33. return 0;
  34. }
  35. int halfMaxAgeIncrementClock(int, struct timespec *ts) {
  36. static unsigned callCount = 1;
  37. ts->tv_sec = callCount * (MonitorCache::MAX_LOG_AGE_SEC / 2);
  38. ts->tv_nsec = 0;
  39. ++callCount;
  40. return 0;
  41. }
  42. }
  43. TEST(MonitorCache, addOne) {
  44. using ::testing::ElementsAre;
  45. MonitorCache monitorCache(constClock);
  46. MonitorEntry testEntry{{"c", "u", "p"}, 1, {42, 666}};
  47. monitorCache.update(testEntry.key(), testEntry.result());
  48. ASSERT_THAT(monitorCache.entries(), ElementsAre(testEntry));
  49. }
  50. TEST(MonitorCache, clear) {
  51. using ::testing::ElementsAre;
  52. using ::testing::IsEmpty;
  53. MonitorCache monitorCache(constClock);
  54. MonitorEntry testEntry{{"c", "u", "p"}, 1, {42, 666}};
  55. monitorCache.update(testEntry.key(), testEntry.result());
  56. ASSERT_THAT(monitorCache.entries(), ElementsAre(testEntry));
  57. monitorCache.clear();
  58. ASSERT_THAT(monitorCache.entries(), IsEmpty());
  59. }
  60. TEST(MonitorCache, shouldFlushAge) {
  61. MonitorCache monitorCache(halfMaxAgeIncrementClock);
  62. monitorCache.update({"c", "u", "p"}, 1);
  63. ASSERT_FALSE(monitorCache.shouldFlush());
  64. monitorCache.update({"c", "u", "p"}, 1);
  65. ASSERT_FALSE(monitorCache.shouldFlush());
  66. monitorCache.update({"c", "u", "p"}, 1);
  67. ASSERT_TRUE(monitorCache.shouldFlush());
  68. }
  69. TEST(MonitorCache, shouldFlushCapacity) {
  70. MonitorCache monitorCache(constClock);
  71. for (std::size_t i = 0; i < MonitorCache::CACHE_CAPACITY - 1; ++i)
  72. monitorCache.update({"c", "u", "p"}, 1);
  73. ASSERT_FALSE(monitorCache.shouldFlush());
  74. monitorCache.update({"c", "u", "p"}, 1);
  75. ASSERT_TRUE(monitorCache.shouldFlush());
  76. }