Skip to content

Commit 046e3f9

Browse files
committed
[UR][CTS] Update urEventSetCallback test to not assume sync. events
Before this patch, urEventSetCallbackTest assumed that the callback would be executed during and blocking urEventWait or urEventRelease. This change removes that assumptions by having the main thread wait until the callback is complete.
1 parent 9f516d6 commit 046e3f9

File tree

1 file changed

+35
-14
lines changed

1 file changed

+35
-14
lines changed

unified-runtime/test/conformance/event/urEventSetCallback.cpp

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,24 @@
66

77
#include "fixtures.h"
88
#include "uur/known_failure.h"
9+
using namespace std::chrono_literals;
910

1011
/* Using urEventReferenceTest to be able to release the event during the test */
11-
using urEventSetCallbackTest = uur::event::urEventReferenceTest;
12+
struct urEventSetCallbackTest : uur::event::urEventReferenceTest {
13+
std::mutex m;
14+
std::condition_variable cv;
15+
int flag = 0;
16+
17+
void WaitForFlag(int Target = 1) {
18+
std::unique_lock lk(m);
19+
cv.wait_for(lk, 1000ms, [&] { return flag == Target; });
20+
}
21+
22+
void SetFlag() {
23+
flag++;
24+
cv.notify_one();
25+
}
26+
};
1227

1328
/**
1429
* Checks that the callback function is called.
@@ -22,19 +37,19 @@ TEST_P(urEventSetCallbackTest, Success) {
2237
[[maybe_unused]] ur_execution_info_t execStatus,
2338
void *pUserData) {
2439

25-
auto status = reinterpret_cast<bool *>(pUserData);
26-
*status = true;
40+
auto that = reinterpret_cast<urEventSetCallbackTest *>(pUserData);
41+
that->SetFlag();
2742
}
2843
};
2944

30-
bool didRun = false;
3145
ASSERT_SUCCESS(
3246
urEventSetCallback(event, ur_execution_info_t::UR_EXECUTION_INFO_COMPLETE,
33-
Callback::callback, &didRun));
47+
Callback::callback, this));
3448

3549
ASSERT_SUCCESS(urEventWait(1, &event));
3650
ASSERT_SUCCESS(urEventRelease(event));
37-
ASSERT_TRUE(didRun);
51+
WaitForFlag();
52+
ASSERT_EQ(flag, 1);
3853
}
3954

4055
/**
@@ -45,6 +60,7 @@ TEST_P(urEventSetCallbackTest, ValidateParameters) {
4560
uur::LevelZeroV2{}, uur::NativeCPU{});
4661

4762
struct CallbackParameters {
63+
urEventSetCallbackTest *test;
4864
ur_event_handle_t event;
4965
ur_execution_info_t execStatus;
5066
};
@@ -56,17 +72,19 @@ TEST_P(urEventSetCallbackTest, ValidateParameters) {
5672
auto parameters = reinterpret_cast<CallbackParameters *>(pUserData);
5773
parameters->event = hEvent;
5874
parameters->execStatus = execStatus;
75+
parameters->test->SetFlag();
5976
}
6077
};
6178

62-
CallbackParameters parameters{};
79+
CallbackParameters parameters{this, nullptr, UR_EXECUTION_INFO_QUEUED};
6380

6481
ASSERT_SUCCESS(
6582
urEventSetCallback(event, ur_execution_info_t::UR_EXECUTION_INFO_COMPLETE,
6683
Callback::callback, &parameters));
6784

6885
ASSERT_SUCCESS(urEventWait(1, &event));
6986
ASSERT_SUCCESS(urEventRelease(event));
87+
WaitForFlag();
7088
ASSERT_EQ(event, parameters.event);
7189
ASSERT_EQ(ur_execution_info_t::UR_EXECUTION_INFO_COMPLETE,
7290
parameters.execStatus);
@@ -80,6 +98,7 @@ TEST_P(urEventSetCallbackTest, AllStates) {
8098
uur::LevelZeroV2{}, uur::NativeCPU{});
8199

82100
struct CallbackStatus {
101+
urEventSetCallbackTest *test = nullptr;
83102
bool submitted = false;
84103
bool running = false;
85104
bool complete = false;
@@ -107,10 +126,12 @@ TEST_P(urEventSetCallbackTest, AllStates) {
107126
FAIL() << "Invalid execution info enumeration";
108127
}
109128
}
129+
130+
status->test->SetFlag();
110131
}
111132
};
112133

113-
CallbackStatus status{};
134+
CallbackStatus status{this};
114135

115136
ASSERT_SUCCESS(urEventSetCallback(
116137
event, ur_execution_info_t::UR_EXECUTION_INFO_SUBMITTED,
@@ -124,6 +145,7 @@ TEST_P(urEventSetCallbackTest, AllStates) {
124145

125146
ASSERT_SUCCESS(urEventWait(1, &event));
126147
ASSERT_SUCCESS(urEventRelease(event));
148+
WaitForFlag(3);
127149

128150
ASSERT_TRUE(status.submitted);
129151
ASSERT_TRUE(status.running);
@@ -145,19 +167,18 @@ TEST_P(urEventSetCallbackTest, EventAlreadyCompleted) {
145167
[[maybe_unused]] ur_execution_info_t execStatus,
146168
void *pUserData) {
147169

148-
auto status = reinterpret_cast<bool *>(pUserData);
149-
*status = true;
170+
auto that = reinterpret_cast<urEventSetCallbackTest *>(pUserData);
171+
that->SetFlag();
150172
}
151173
};
152174

153-
bool didRun = false;
154-
155175
ASSERT_SUCCESS(
156176
urEventSetCallback(event, ur_execution_info_t::UR_EXECUTION_INFO_COMPLETE,
157-
Callback::callback, &didRun));
177+
Callback::callback, this));
158178

159179
ASSERT_SUCCESS(urEventRelease(event));
160-
ASSERT_TRUE(didRun);
180+
WaitForFlag();
181+
ASSERT_EQ(flag, 1);
161182
}
162183

163184
UUR_INSTANTIATE_DEVICE_TEST_SUITE(urEventSetCallbackTest);

0 commit comments

Comments
 (0)