Skip to content

Commit 23dcf10

Browse files
authored
Test runtime: Easy wait for a single event (#8608)
1 parent 538b589 commit 23dcf10

File tree

5 files changed

+111
-1
lines changed

5 files changed

+111
-1
lines changed

ydb/core/testlib/actors/block_events.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,5 +86,4 @@ namespace NActors {
8686
bool Stopped = false;
8787
};
8888

89-
9089
} // namespace NActors

ydb/core/testlib/actors/test_runtime_ut.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <ydb/core/testlib/actors/test_runtime.h>
22
#include <ydb/core/testlib/actors/block_events.h>
3+
#include <ydb/core/testlib/actors/wait_events.h>
34
#include <ydb/core/base/appdata.h>
45
#include <ydb/library/actors/core/event_local.h>
56
#include <ydb/library/actors/core/events.h>
@@ -826,6 +827,65 @@ Y_UNIT_TEST_SUITE(TActorTest) {
826827
UNIT_ASSERT_VALUES_EQUAL(values.at(1), 4);
827828
UNIT_ASSERT_VALUES_EQUAL(values.at(2), 7);
828829
}
830+
831+
Y_UNIT_TEST(TestWaitForFirstEvent) {
832+
enum EEv {
833+
EvTrigger = EventSpaceBegin(TEvents::ES_PRIVATE)
834+
};
835+
836+
struct TEvTrigger : public TEventLocal<TEvTrigger, EvTrigger> {
837+
int Value;
838+
839+
TEvTrigger(int value)
840+
: Value(value)
841+
{}
842+
};
843+
844+
class TSourceActor : public TActorBootstrapped<TSourceActor> {
845+
public:
846+
TSourceActor(const TActorId& target)
847+
: Target(target)
848+
{}
849+
850+
void Bootstrap() {
851+
Become(&TThis::StateWork);
852+
Schedule(TDuration::Seconds(1), new TEvents::TEvWakeup);
853+
}
854+
855+
private:
856+
STFUNC(StateWork) {
857+
switch (ev->GetTypeRewrite()) {
858+
hFunc(TEvents::TEvWakeup, Handle);
859+
}
860+
}
861+
862+
void Handle(TEvents::TEvWakeup::TPtr&) {
863+
Send(Target, new TEvTrigger(++Counter));
864+
Schedule(TDuration::Seconds(1), new TEvents::TEvWakeup);
865+
}
866+
867+
private:
868+
TActorId Target;
869+
int Counter = 0;
870+
};
871+
872+
TTestActorRuntime runtime;
873+
runtime.Initialize(MakeEgg());
874+
875+
TActorId target = runtime.AllocateEdgeActor();
876+
877+
TActorId actorId = runtime.Register(new TSourceActor(target));
878+
runtime.EnableScheduleForActor(actorId);
879+
880+
{
881+
TWaitForFirstEvent<TEvTrigger> waiter(runtime);
882+
waiter.Wait();
883+
}
884+
{
885+
TWaitForFirstEvent<TEvTrigger> waiter(runtime, [](const TEvTrigger::TPtr& ev){ return ev->Get()->Value == 10; });
886+
waiter.Wait();
887+
}
888+
}
829889
}
830890

831891
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#include "wait_events.h"

ydb/core/testlib/actors/wait_events.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include "test_runtime.h"
2+
3+
#include <functional>
4+
5+
namespace NActors {
6+
7+
/**
8+
* Easy wait for a first event under the test actor runtime
9+
*
10+
*/
11+
template<class TEvType>
12+
class TWaitForFirstEvent {
13+
public:
14+
TWaitForFirstEvent(TTestActorRuntime& runtime, std::function<bool(const typename TEvType::TPtr&)> condition = {})
15+
: Runtime(runtime)
16+
, Condition(std::move(condition))
17+
, Holder(Runtime.AddObserver<TEvType>(
18+
[this](typename TEvType::TPtr& ev) {
19+
if (EventSeen)
20+
return;
21+
if (Condition && !Condition(ev))
22+
return;
23+
EventSeen = true;
24+
}))
25+
{}
26+
27+
/**
28+
* Wait for a first event
29+
*/
30+
void Wait() {
31+
Runtime.WaitFor(TypeName<TEvType>(), [&]{ return EventSeen; });
32+
}
33+
34+
/**
35+
* Stops waiting and remove event observer
36+
*/
37+
void Stop() {
38+
Holder.Remove();
39+
}
40+
41+
private:
42+
TTestActorRuntime& Runtime;
43+
std::function<bool(const typename TEvType::TPtr&)> Condition;
44+
TTestActorRuntime::TEventObserverHolder Holder;
45+
bool EventSeen = false;
46+
};
47+
48+
} // namespace NActors

ydb/core/testlib/actors/ya.make

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ SRCS(
55
block_events.h
66
test_runtime.cpp
77
test_runtime.h
8+
wait_events.cpp
9+
wait_events.h
810
)
911

1012
PEERDIR(

0 commit comments

Comments
 (0)