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
|
/**
* Copyright © 2019 IBM Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "extensions/openpower-pels/event_logger.hpp"
#include "log_manager.hpp"
#include <gtest/gtest.h>
using namespace openpower::pels;
using namespace phosphor::logging;
class CreateHelper
{
public:
void create(const std::string& name, Entry::Level level,
const EventLogger::ADMap& ad)
{
_createCount++;
_prevName = name;
_prevLevel = level;
_prevAD = ad;
// Try to create another event from within the creation
// function. Should never work or else we could get stuck
// infinitely creating events.
if (_eventLogger)
{
AdditionalData d;
_eventLogger->log(name, level, d);
}
}
size_t _createCount = 0;
std::string _prevName;
Entry::Level _prevLevel;
EventLogger::ADMap _prevAD;
EventLogger* _eventLogger = nullptr;
};
void runEvents(sd_event* event, size_t numEvents)
{
sdeventplus::Event e{event};
for (size_t i = 0; i < numEvents; i++)
{
e.run(std::chrono::milliseconds(1));
}
}
TEST(EventLoggerTest, TestCreateEvents)
{
sd_event* sdEvent = nullptr;
auto r = sd_event_default(&sdEvent);
ASSERT_TRUE(r >= 0);
CreateHelper ch;
EventLogger eventLogger(
sdEvent, std::bind(std::mem_fn(&CreateHelper::create), &ch,
std::placeholders::_1, std::placeholders::_2,
std::placeholders::_3));
ch._eventLogger = &eventLogger;
AdditionalData ad;
ad.add("key1", "value1");
eventLogger.log("one", Entry::Level::Error, ad);
EXPECT_EQ(eventLogger.queueSize(), 1);
runEvents(sdEvent, 1);
// Verify 1 event was created
EXPECT_EQ(eventLogger.queueSize(), 0);
EXPECT_EQ(ch._prevName, "one");
EXPECT_EQ(ch._prevLevel, Entry::Level::Error);
EXPECT_EQ(ch._prevAD, ad.getData());
EXPECT_EQ(ch._createCount, 1);
// Create 2 more, and run 1 event loop at a time and check the results
eventLogger.log("two", Entry::Level::Error, ad);
eventLogger.log("three", Entry::Level::Error, ad);
EXPECT_EQ(eventLogger.queueSize(), 2);
runEvents(sdEvent, 1);
EXPECT_EQ(ch._createCount, 2);
EXPECT_EQ(ch._prevName, "two");
EXPECT_EQ(eventLogger.queueSize(), 1);
runEvents(sdEvent, 1);
EXPECT_EQ(ch._createCount, 3);
EXPECT_EQ(ch._prevName, "three");
EXPECT_EQ(eventLogger.queueSize(), 0);
// Add them all again and run them all at once
eventLogger.log("three", Entry::Level::Error, ad);
eventLogger.log("two", Entry::Level::Error, ad);
eventLogger.log("one", Entry::Level::Error, ad);
runEvents(sdEvent, 3);
EXPECT_EQ(ch._createCount, 6);
EXPECT_EQ(ch._prevName, "one");
EXPECT_EQ(eventLogger.queueSize(), 0);
// Run extra events - doesn't do anything
runEvents(sdEvent, 1);
EXPECT_EQ(ch._createCount, 6);
EXPECT_EQ(ch._prevName, "one");
EXPECT_EQ(eventLogger.queueSize(), 0);
sd_event_unref(sdEvent);
}
|