aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/messagebus/test/helper/object_count_check.h
blob: 1c4756e58c12480888f9eeca37d7ba7b33e6add7 (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
#pragma once

#include <library/cpp/testing/unittest/registar.h>

#include <library/cpp/messagebus/remote_client_connection.h>
#include <library/cpp/messagebus/remote_client_session.h>
#include <library/cpp/messagebus/remote_server_connection.h>
#include <library/cpp/messagebus/remote_server_session.h>
#include <library/cpp/messagebus/ybus.h>
#include <library/cpp/messagebus/oldmodule/module.h>
#include <library/cpp/messagebus/scheduler/scheduler.h>

#include <util/generic/object_counter.h>
#include <util/system/type_name.h>
#include <util/stream/output.h>

#include <typeinfo>

struct TObjectCountCheck {
    bool Enabled;

    template <typename T>
    struct TReset {
        TObjectCountCheck* const Thiz;

        TReset(TObjectCountCheck* thiz)
            : Thiz(thiz)
        {
        }

        void operator()() {
            long oldValue = TObjectCounter<T>::ResetObjectCount();
            if (oldValue != 0) {
                Cerr << "warning: previous counter: " << oldValue << " for " << TypeName<T>() << Endl;
                Cerr << "won't check in this test" << Endl;
                Thiz->Enabled = false;
            }
        }
    };

    TObjectCountCheck() {
        Enabled = true;
        DoForAllCounters<TReset>();
    }

    template <typename T>
    struct TCheckZero {
        TCheckZero(TObjectCountCheck*) {
        }

        void operator()() {
            UNIT_ASSERT_VALUES_EQUAL_C(0L, TObjectCounter<T>::ObjectCount(), TypeName<T>());
        }
    };

    ~TObjectCountCheck() {
        if (Enabled) {
            DoForAllCounters<TCheckZero>();
        }
    }

    template <template <typename> class TOp>
    void DoForAllCounters() {
        TOp< ::NBus::NPrivate::TRemoteClientConnection>(this)();
        TOp< ::NBus::NPrivate::TRemoteServerConnection>(this)();
        TOp< ::NBus::NPrivate::TRemoteClientSession>(this)();
        TOp< ::NBus::NPrivate::TRemoteServerSession>(this)();
        TOp< ::NBus::NPrivate::TScheduler>(this)();
        TOp< ::NEventLoop::TEventLoop>(this)();
        TOp< ::NEventLoop::TChannel>(this)();
        TOp< ::NBus::TBusModule>(this)();
        TOp< ::NBus::TBusJob>(this)();
    }
};