Skip to content

Commit 9337685

Browse files
committed
MINIFICPP-1448 - Check if event is available
1 parent 497c216 commit 9337685

File tree

1 file changed

+53
-7
lines changed

1 file changed

+53
-7
lines changed

extensions/windows-event-log/tests/CWELCustomProviderTests.cpp

Lines changed: 53 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
* limitations under the License.
1616
*/
1717

18+
#undef NDEBUG
19+
1820
#include "ConsumeWindowsEventLog.h"
1921

2022
#include "core/ConfigurableComponent.h"
@@ -24,6 +26,8 @@
2426
#include "utils/TestUtils.h"
2527
#include "utils/file/FileUtils.h"
2628
#include "rapidjson/document.h"
29+
#include "wel/UniqueEvtHandle.h"
30+
#include "IntegrationTestUtils.h"
2731

2832
#include "CWELTestUtils.h"
2933

@@ -58,25 +62,67 @@ bool dispatchCustomEvent(const CustomEventData& event) {
5862
return result == ERROR_SUCCESS;
5963
}
6064

65+
using org::apache::nifi::minifi::wel::unique_evt_handle;
66+
67+
bool advanceBookmark(const unique_evt_handle& hBookmark, const std::string& channel, const std::string& query, bool advance_to_last = false) {
68+
const auto hEventResults = unique_evt_handle{ EvtQuery(0, std::wstring{channel.begin(), channel.end()}.c_str(), std::wstring{query.begin(), query.end()}.c_str(), EvtQueryChannelPath) };
69+
if (!hEventResults) {
70+
return false;
71+
}
72+
73+
if (advance_to_last) {
74+
if (!EvtSeek(hEventResults.get(), 0, 0, 0, EvtSeekRelativeToLast)) {
75+
return false;
76+
}
77+
} else {
78+
if (!EvtSeek(hEventResults.get(), 1, hBookmark.get(), 0, EvtSeekRelativeToBookmark)) {
79+
return false;
80+
}
81+
}
82+
83+
const unique_evt_handle hEvent = [&hEventResults] {
84+
DWORD dwReturned{};
85+
EVT_HANDLE hEvent{ nullptr };
86+
EvtNext(hEventResults.get(), 1, &hEvent, INFINITE, 0, &dwReturned);
87+
return unique_evt_handle{ hEvent };
88+
}();
89+
90+
if (!hEvent) {
91+
return false;
92+
}
93+
94+
REQUIRE(EvtUpdateBookmark(hBookmark.get(), hEvent.get()));
95+
96+
return true;
97+
}
98+
6199
class CustomProviderController : public OutputFormatTestController {
62100
public:
63-
CustomProviderController(std::string format, std::string json_format) : OutputFormatTestController(CUSTOM_CHANNEL, "*", std::move(format), std::move(json_format)) {}
101+
CustomProviderController(std::string format, std::string json_format) : OutputFormatTestController(CUSTOM_CHANNEL, "*", std::move(format), std::move(json_format)) {
102+
bookmark_.reset(EvtCreateBookmark(0));
103+
advanceBookmark(bookmark_, channel_, query_, true);
104+
REQUIRE(bookmark_);
105+
}
64106

65107
protected:
66108
void dispatchBookmarkEvent() override {
67109
auto binary = reinterpret_cast<const unsigned char*>("\x0c\x10");
68110
REQUIRE(dispatchCustomEvent({L"Bookmark", L"Second", L"Third", 2, binary}));
69-
// even though we are using the API, we still have to wait for the event to appear
70-
// for CWEL processor
71-
std::this_thread::sleep_for(std::chrono::seconds{2});
111+
REQUIRE(checkNewEventAvailable());
72112
}
73113
void dispatchCollectedEvent() override {
74114
auto binary = reinterpret_cast<const unsigned char*>("\x09\x01");
75115
REQUIRE(dispatchCustomEvent({L"Actual event", L"Second", L"Third", 2, binary}));
76-
// even though we are using the API, we still have to wait for the event to appear
77-
// for CWEL processor
78-
std::this_thread::sleep_for(std::chrono::seconds{2});
116+
REQUIRE(checkNewEventAvailable());
117+
}
118+
119+
private:
120+
bool checkNewEventAvailable() {
121+
return org::apache::nifi::minifi::utils::verifyEventHappenedInPollTime(std::chrono::seconds{5}, [&] {
122+
return advanceBookmark(bookmark_, channel_, query_);
123+
});
79124
}
125+
unique_evt_handle bookmark_;
80126
};
81127

82128
const std::string EVENT_DATA_JSON = R"(

0 commit comments

Comments
 (0)