object_count_check.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <library/cpp/testing/unittest/registar.h>
  3. #include <library/cpp/messagebus/remote_client_connection.h>
  4. #include <library/cpp/messagebus/remote_client_session.h>
  5. #include <library/cpp/messagebus/remote_server_connection.h>
  6. #include <library/cpp/messagebus/remote_server_session.h>
  7. #include <library/cpp/messagebus/ybus.h>
  8. #include <library/cpp/messagebus/oldmodule/module.h>
  9. #include <library/cpp/messagebus/scheduler/scheduler.h>
  10. #include <util/generic/object_counter.h>
  11. #include <util/system/type_name.h>
  12. #include <util/stream/output.h>
  13. #include <typeinfo>
  14. struct TObjectCountCheck {
  15. bool Enabled;
  16. template <typename T>
  17. struct TReset {
  18. TObjectCountCheck* const Thiz;
  19. TReset(TObjectCountCheck* thiz)
  20. : Thiz(thiz)
  21. {
  22. }
  23. void operator()() {
  24. long oldValue = TObjectCounter<T>::ResetObjectCount();
  25. if (oldValue != 0) {
  26. Cerr << "warning: previous counter: " << oldValue << " for " << TypeName<T>() << Endl;
  27. Cerr << "won't check in this test" << Endl;
  28. Thiz->Enabled = false;
  29. }
  30. }
  31. };
  32. TObjectCountCheck() {
  33. Enabled = true;
  34. DoForAllCounters<TReset>();
  35. }
  36. template <typename T>
  37. struct TCheckZero {
  38. TCheckZero(TObjectCountCheck*) {
  39. }
  40. void operator()() {
  41. UNIT_ASSERT_VALUES_EQUAL_C(0L, TObjectCounter<T>::ObjectCount(), TypeName<T>());
  42. }
  43. };
  44. ~TObjectCountCheck() {
  45. if (Enabled) {
  46. DoForAllCounters<TCheckZero>();
  47. }
  48. }
  49. template <template <typename> class TOp>
  50. void DoForAllCounters() {
  51. TOp< ::NBus::NPrivate::TRemoteClientConnection>(this)();
  52. TOp< ::NBus::NPrivate::TRemoteServerConnection>(this)();
  53. TOp< ::NBus::NPrivate::TRemoteClientSession>(this)();
  54. TOp< ::NBus::NPrivate::TRemoteServerSession>(this)();
  55. TOp< ::NBus::NPrivate::TScheduler>(this)();
  56. TOp< ::NEventLoop::TEventLoop>(this)();
  57. TOp< ::NEventLoop::TChannel>(this)();
  58. TOp< ::NBus::TBusModule>(this)();
  59. TOp< ::NBus::TBusJob>(this)();
  60. }
  61. };