File: mock_metermap.cpp

package info (click to toggle)
vzlogger 0.8.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,140 kB
  • sloc: cpp: 12,020; sh: 331; ansic: 157; makefile: 25
file content (179 lines) | stat: -rw-r--r-- 5,338 bytes parent folder | download | duplicates (2)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <gmock/gmock.h>
using ::testing::_;
using ::testing::AtLeast;
using ::testing::Eq;
using ::testing::Ge;
using ::testing::InSequence;
using ::testing::Invoke;
using ::testing::Return;
using ::testing::SetArrayArgument;

#include <gtest/gtest.h>
using ::testing::Test;

#include "Channel.hpp"
#include "Config_Options.hpp"
#include "Meter.hpp"
#include "MeterMap.hpp"

namespace mock_metermap {

class mock_meter : public Meter {
  public:
	mock_meter(const std::list<Option> &o) : Meter(o){};
	virtual ~mock_meter(){};
	//    mock_meter() : Meter(std::list<Option>()) {};
	MOCK_METHOD0(open, void());
	MOCK_METHOD0(close, int());
	MOCK_CONST_METHOD0(isEnabled, bool());
	MOCK_METHOD2(read, size_t(std::vector<Reading> &rds, size_t n));
};

// here we test class MeterMap and threads.cpp:
// we mock the other classes used

TEST(mock_metermap, basic_open_close_enabled) {
	std::list<Option> o;
	o.push_back(Option("protocol", "random"));
	mock_meter *mtr = new mock_meter(o);
	mtr->interval(1);
	testing::Mock::AllowLeak(mtr);
	EXPECT_CALL(*mtr, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(true));
	{
		InSequence d;
		EXPECT_CALL(*mtr, open()).Times(1);
		EXPECT_CALL(*mtr, close()); // Times(1) assumed
	}

	MeterMap m(mtr);
	m.start();
	m.cancel();
	EXPECT_FALSE(m.running());
}

// test that disabled meters don't get open/closed

TEST(mock_metermap, basic_open_close_not_enabled) {
	std::list<Option> o;
	o.push_back(Option("protocol", "random"));
	mock_meter *mtr = new mock_meter(o);
	mtr->interval(1);
	EXPECT_CALL(*mtr, isEnabled()).Times(AtLeast(1));
	EXPECT_CALL(*mtr, open()).Times(0);
	EXPECT_CALL(*mtr, close()).Times(0);

	MeterMap m(mtr);
	m.start();
	m.cancel();
	EXPECT_FALSE(m.running());
}

TEST(mock_metermap, DISABLED_one_channel) { // todo issue #400
	std::list<Option> o;
	o.push_back(Option("protocol", "random"));
	mock_meter *mtr = new mock_meter(o);
	mtr->interval(1);
	EXPECT_CALL(*mtr, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(true));
	{
		InSequence d;
		EXPECT_CALL(*mtr, open()).Times(1);
		EXPECT_CALL(*mtr, close()); // Times(1) assumed
	}

	MeterMap m(mtr);
	Channel *ch = new Channel();
	EXPECT_CALL(*ch, name()).Times(AtLeast(1));
	EXPECT_CALL(*ch, identifier())
		.Times(AtLeast(1))
		.WillRepeatedly(Return(ReadingIdentifier::Ptr()));
	EXPECT_CALL(*ch, buffer()).Times(AtLeast(1)).WillRepeatedly(Invoke(ch, &Channel::real_buf));
	EXPECT_CALL(*ch, notify()).Times(AtLeast(0)); // can be called 0 or sometimes
	{
		InSequence s;
		EXPECT_CALL(*ch, start(_)).Times(1);
		EXPECT_CALL(*ch, cancel()).Times(1);
		EXPECT_CALL(*ch, join()).Times(1);
	}
	// TODO Bug? Channel::Notify get's called even without a single reading gathered!
	m.push_back(Channel::Ptr(ch));
	EXPECT_CALL(*mtr, read(_, Ge(1u))).Times(AtLeast(1)).WillRepeatedly(Return(1));

	m.start();
	usleep(100000); // give reading thread a chance. todo better cancel that synchronizes with
					// readingthread
	m.cancel();
	EXPECT_FALSE(m.running());
}

// test whether the read data get's only into the proper channels: (two channel can have same id)
size_t return_read(std::vector<Reading> &rds, size_t n) {
	if (n > 0)
		rds[0] = Reading(ReadingIdentifier::Ptr(new ChannelIdentifier(1)));
	return 1;
}

TEST(mock_metermap, DISABLED_reading_in_proper_channel) { // todo issue #400
	std::list<Option> o;
	o.push_back((Option("protocol", "random")));
	mock_meter *mtr = new mock_meter(o);
	EXPECT_CALL(*mtr, isEnabled()).Times(AtLeast(1)).WillRepeatedly(Return(true));

	MeterMap m(mtr);
	mtr->interval(1);
	Channel *ch1 = new Channel();
	Channel *ch2 = new Channel();
	Channel *ch3 = new Channel();

	// assign ids: 1 2 1 (so ch1.id == ch3.id)
	EXPECT_CALL(*ch1, identifier())
		.Times(AtLeast(1))
		.WillRepeatedly(Return(ReadingIdentifier::Ptr(new ChannelIdentifier(1))));
	EXPECT_CALL(*ch2, identifier())
		.Times(AtLeast(1))
		.WillRepeatedly(Return(ReadingIdentifier::Ptr(new ChannelIdentifier(2))));
	EXPECT_CALL(*ch3, identifier())
		.Times(AtLeast(1))
		.WillRepeatedly(Return(ReadingIdentifier::Ptr(new ChannelIdentifier(1))));

	// make sure the channels have a real buffer:
	EXPECT_CALL(*ch1, buffer()).Times(AtLeast(1)).WillRepeatedly(Invoke(ch1, &Channel::real_buf));
	EXPECT_CALL(*ch2, buffer()).Times(AtLeast(1)).WillRepeatedly(Invoke(ch2, &Channel::real_buf));
	EXPECT_CALL(*ch3, buffer()).Times(AtLeast(1)).WillRepeatedly(Invoke(ch3, &Channel::real_buf));

	// our test. Make sure that ch1 and ch3 get one reading and ch2 gets none
	EXPECT_CALL(*ch1, push(_)).Times(1);
	EXPECT_CALL(*ch2, push(_)).Times(0);
	EXPECT_CALL(*ch3, push(_)).Times(1);

	EXPECT_CALL(*mtr, read(_, Ge(1u))).Times(AtLeast(1)).WillRepeatedly(Invoke(return_read));

	m.push_back(Channel::Ptr(ch1));
	m.push_back(Channel::Ptr(ch2));
	m.push_back(Channel::Ptr(ch3));
	m.start();
	usleep(100000);
	m.cancel();
}

} // namespace mock_metermap

Config_Options options;

void print(log_level_t l, char const *s1, char const *s2, ...) {
	//	if (l!= log_debug)
	{
		fprintf(stdout, "\n  %s:", s2);
		va_list argp;
		va_start(argp, s2);
		vfprintf(stdout, s1, argp);
		va_end(argp);
		fprintf(stdout, "\n");
	}
}

int main(int argc, char **argv) {
	// The following line must be executed to initialize Google Mock
	// (and Google Test) before running the tests.
	::testing::InitGoogleMock(&argc, argv);
	return RUN_ALL_TESTS();
}