aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/testlib
diff options
context:
space:
mode:
authorxenoxeno <xeno@ydb.tech>2023-03-09 12:10:01 +0300
committerxenoxeno <xeno@ydb.tech>2023-03-09 12:10:01 +0300
commitad607bb887619f321dec03b02df8220e01b7f5aa (patch)
tree7d5c87352cbe835b56bb2bdac93b37cbdf8ead21 /library/cpp/actors/testlib
parent6324d075a5e80b6943b5de6b465b775050fe83df (diff)
downloadydb-ad607bb887619f321dec03b02df8220e01b7f5aa.tar.gz
light events for actor system
Diffstat (limited to 'library/cpp/actors/testlib')
-rw-r--r--library/cpp/actors/testlib/decorator_ut.cpp26
-rw-r--r--library/cpp/actors/testlib/test_runtime.cpp59
-rw-r--r--library/cpp/actors/testlib/test_runtime.h30
3 files changed, 65 insertions, 50 deletions
diff --git a/library/cpp/actors/testlib/decorator_ut.cpp b/library/cpp/actors/testlib/decorator_ut.cpp
index e9a2fa35604..3c70d25ec00 100644
--- a/library/cpp/actors/testlib/decorator_ut.cpp
+++ b/library/cpp/actors/testlib/decorator_ut.cpp
@@ -28,14 +28,14 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
virtual ~TDyingChecker() {
Write("TDyingChecker::~TDyingChecker");
- TActivationContext::Send(new IEventHandle(MasterId, SelfId(), new TEvents::TEvPing()));
+ TActivationContext::Send(new IEventHandleFat(MasterId, SelfId(), new TEvents::TEvPing()));
}
bool DoBeforeReceiving(TAutoPtr<IEventHandle> &/*ev*/, const TActorContext &/*ctx*/) override {
Write("TDyingChecker::DoBeforeReceiving");
return true;
}
-
+
void DoAfterReceiving(const TActorContext &/*ctx*/) override {
Write("TDyingChecker::DoAfterReceiving");
}
@@ -47,7 +47,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
TSet<TActorId> ActorIds;
TVector<THolder<IActor>> Actors;
TActorId EdgeActor;
-
+
TTestMasterActor(TVector<THolder<IActor>> &&actors, TActorId edgeActor)
: TActorBootstrapped()
, Actors(std::move(actors))
@@ -104,7 +104,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
return true;
}
Write("TFizzBuzzToFooBar::DoBeforeSending");
- TEventHandle<TEvWords> *handle = reinterpret_cast<TEventHandle<TEvWords>*>(ev.Get());
+ TEventHandleFat<TEvWords> *handle = reinterpret_cast<TEventHandleFat<TEvWords>*>(ev.Get());
UNIT_ASSERT(handle);
TEvWords *event = handle->Get();
TVector<TString> &words = event->Words;
@@ -124,12 +124,12 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
Write("TFizzBuzzToFooBar::DoBeforeReceiving");
return true;
}
-
+
void DoAfterReceiving(const TActorContext &/*ctx*/) override {
Write("TFizzBuzzToFooBar::DoAfterReceiving");
}
- };
-
+ };
+
struct TWordEraser : TTestDecorator {
TString ErasingWord;
@@ -144,7 +144,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
return true;
}
Write("TWordEraser::DoBeforeSending");
- TEventHandle<TEvWords> *handle = reinterpret_cast<TEventHandle<TEvWords>*>(ev.Get());
+ TEventHandleFat<TEvWords> *handle = reinterpret_cast<TEventHandleFat<TEvWords>*>(ev.Get());
UNIT_ASSERT(handle);
TEvWords *event = handle->Get();
TVector<TString> &words = event->Words;
@@ -159,7 +159,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
Write("TWordEraser::DoBeforeReceiving");
return true;
}
-
+
void DoAfterReceiving(const TActorContext &/*ctx*/) override {
Write("TWordEraser::DoAfterReceiving");
}
@@ -176,7 +176,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
return true;
}
Write("TWithoutWordsDroper::DoBeforeSending");
- TEventHandle<TEvWords> *handle = reinterpret_cast<TEventHandle<TEvWords>*>(ev.Get());
+ TEventHandleFat<TEvWords> *handle = reinterpret_cast<TEventHandleFat<TEvWords>*>(ev.Get());
UNIT_ASSERT(handle);
TEvWords *event = handle->Get();
return bool(event->Words);
@@ -186,7 +186,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
Write("TWithoutWordsDroper::DoBeforeReceiving");
return true;
}
-
+
void DoAfterReceiving(const TActorContext &/*ctx*/) override {
Write("TWithoutWordsDroper::DoAfterReceiving");
}
@@ -208,7 +208,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
}
STATEFN(State) {
- TEventHandle<TEvWords> *handle = reinterpret_cast<TEventHandle<TEvWords>*>(ev.Get());
+ TEventHandleFat<TEvWords> *handle = reinterpret_cast<TEventHandleFat<TEvWords>*>(ev.Get());
UNIT_ASSERT(handle);
UNIT_ASSERT(handle->Sender == MasterId);
TEvWords *event = handle->Get();
@@ -284,7 +284,7 @@ Y_UNIT_TEST_SUITE(TesTTestDecorator) {
return true;
}
};
-
+
bool ScheduledFilterFunc(NActors::TTestActorRuntimeBase& runtime, TAutoPtr<NActors::IEventHandle>& event,
TDuration delay, TInstant& deadline) {
if (runtime.IsScheduleForActorEnabled(event->GetRecipientRewrite())) {
diff --git a/library/cpp/actors/testlib/test_runtime.cpp b/library/cpp/actors/testlib/test_runtime.cpp
index ad124bf258b..889edc4969b 100644
--- a/library/cpp/actors/testlib/test_runtime.cpp
+++ b/library/cpp/actors/testlib/test_runtime.cpp
@@ -47,11 +47,11 @@ namespace NActors {
Cerr << " \"" << name << "\"";
Cerr << ", ";
if (ev->HasEvent())
- Cerr << " : " << (PRINT_EVENT_BODY ? ev->GetBase()->ToString() : ev->GetBase()->ToStringHeader());
- else if (ev->HasBuffer())
- Cerr << " : BUFFER";
- else
- Cerr << " : EMPTY";
+ Cerr << " : " << (PRINT_EVENT_BODY ? ev->ToString() : ev->GetTypeName());
+ // else if (ev->HasBuffer())
+ // Cerr << " : BUFFER";
+ // else
+ // Cerr << " : EMPTY";
Cerr << "\n";
}
@@ -397,7 +397,7 @@ namespace NActors {
TActorContext ctx(*mailbox, *node->ExecutorThread, GetCycleCountFast(), ev->GetRecipientRewrite());
TActivationContext *prevTlsActivationContext = TlsActivationContext;
TlsActivationContext = &ctx;
- recipientActor->Receive(ev, ctx);
+ recipientActor->Receive(ev);
TlsActivationContext = prevTlsActivationContext;
// we expect the logger to never die in tests
}
@@ -669,7 +669,7 @@ namespace NActors {
while (!scheduledEvents.empty() && scheduledEvents.begin()->Deadline == time) {
// static THashMap<std::pair<TActorId, TString>, ui64> eventTypes;
auto& item = *scheduledEvents.begin();
- TString name = item.Event->GetBase() ? TypeName(*item.Event->GetBase()) : Sprintf("%08" PRIx32, item.Event->Type);
+ TString name = item.Event->GetTypeName();
// eventTypes[std::make_pair(item.Event->Recipient, name)]++;
runtime.ScheduledCount++;
if (runtime.ScheduledCount > runtime.ScheduledLimit) {
@@ -1420,18 +1420,23 @@ namespace NActors {
}
}
- void TTestActorRuntimeBase::Send(IEventHandle* ev, ui32 senderNodeIndex, bool viaActorSystem) {
+ void TTestActorRuntimeBase::Send(const TActorId& recipient, const TActorId& sender, TAutoPtr<IEventHandleLight> ev, ui32 senderNodeIndex, bool viaActorSystem) {
+ ev->PrepareSend(recipient, sender);
+ Send(ev.Release(), senderNodeIndex, viaActorSystem);
+ }
+
+ void TTestActorRuntimeBase::Send(TAutoPtr<IEventHandle> ev, ui32 senderNodeIndex, bool viaActorSystem) {
TGuard<TMutex> guard(Mutex);
Y_VERIFY(senderNodeIndex < NodeCount, "senderNodeIndex# %" PRIu32 " < NodeCount# %" PRIu32,
senderNodeIndex, NodeCount);
SendInternal(ev, senderNodeIndex, viaActorSystem);
}
- void TTestActorRuntimeBase::SendAsync(IEventHandle* ev, ui32 senderNodeIndex) {
+ void TTestActorRuntimeBase::SendAsync(TAutoPtr<IEventHandle> ev, ui32 senderNodeIndex) {
Send(ev, senderNodeIndex, true);
}
- void TTestActorRuntimeBase::Schedule(IEventHandle* ev, const TDuration& duration, ui32 nodeIndex) {
+ void TTestActorRuntimeBase::Schedule(TAutoPtr<IEventHandle> ev, const TDuration& duration, ui32 nodeIndex) {
TGuard<TMutex> guard(Mutex);
Y_VERIFY(nodeIndex < NodeCount);
ui32 nodeId = FirstNodeId + nodeIndex;
@@ -1568,7 +1573,7 @@ namespace NActors {
NeedMonitoring = true;
}
- void TTestActorRuntimeBase::SendInternal(IEventHandle* ev, ui32 nodeIndex, bool viaActorSystem) {
+ void TTestActorRuntimeBase::SendInternal(TAutoPtr<IEventHandle> ev, ui32 nodeIndex, bool viaActorSystem) {
Y_VERIFY(nodeIndex < NodeCount);
ui32 nodeId = FirstNodeId + nodeIndex;
TNodeDataBase* node = Nodes[nodeId].Get();
@@ -1587,23 +1592,22 @@ namespace NActors {
}
Y_VERIFY(!ev->GetRecipientRewrite().IsService() && (targetNodeIndex == nodeIndex));
- TAutoPtr<IEventHandle> evHolder(ev);
- if (!AllowSendFrom(node, evHolder)) {
+ if (!AllowSendFrom(node, ev)) {
return;
}
ui32 mailboxHint = ev->GetRecipientRewrite().Hint();
TEventMailBox& mbox = GetMailbox(nodeId, mailboxHint);
if (!mbox.IsActive(TInstant::MicroSeconds(CurrentTimestamp))) {
- mbox.PushFront(evHolder);
+ mbox.PushFront(ev);
return;
}
ui64 recipientLocalId = ev->GetRecipientRewrite().LocalId();
if ((BlockedOutput.find(ev->Sender) == BlockedOutput.end()) && VERBOSE) {
Cerr << "Send event, ";
- PrintEvent(evHolder, this);
+ PrintEvent(ev, this);
}
EvCounters[ev->GetTypeRewrite()]++;
@@ -1624,7 +1628,7 @@ namespace NActors {
TCallstack::GetTlsCallstack() = ev->Callstack;
TCallstack::GetTlsCallstack().SetLinesToSkip();
#endif
- recipientActor->Receive(evHolder, ctx);
+ recipientActor->Receive(ev);
node->ExecutorThread->DropUnregistered();
}
CurrentRecipient = TActorId();
@@ -1634,10 +1638,8 @@ namespace NActors {
Cerr << "Failed to find actor with local id: " << recipientLocalId << "\n";
}
- auto forwardedEv = ev->ForwardOnNondelivery(TEvents::TEvUndelivered::ReasonActorUnknown);
- if (!!forwardedEv) {
- node->ActorSystem->Send(forwardedEv);
- }
+ auto fw = IEventHandle::ForwardOnNondelivery(ev, TEvents::TEvUndelivered::ReasonActorUnknown);
+ node->ActorSystem->Send(fw);
}
}
@@ -1864,7 +1866,8 @@ namespace NActors {
if (HasReply) {
delete Context->Queue->Pop();
}
- ctx.ExecutorThread.Send(ev->Forward(originalSender));
+ IEventHandle::Forward(ev, originalSender);
+ ctx.ExecutorThread.Send(ev);
if (!IsSync && Context->Queue->Head()) {
SendHead(ctx);
}
@@ -1896,9 +1899,17 @@ namespace NActors {
TAutoPtr<IEventHandle> GetForwardedEvent() {
IEventHandle* ev = Context->Queue->Head();
ReplyChecker->OnRequest(ev);
- TAutoPtr<IEventHandle> forwardedEv = ev->HasEvent()
- ? new IEventHandle(Delegatee, ReplyId, ev->ReleaseBase().Release(), ev->Flags, ev->Cookie)
- : new IEventHandle(ev->GetTypeRewrite(), ev->Flags, Delegatee, ReplyId, ev->ReleaseChainBuffer(), ev->Cookie);
+ TAutoPtr<IEventHandle> forwardedEv;
+ if (ev->IsEventLight()) {
+ IEventHandleLight* evl = IEventHandleLight::GetLight(ev);
+ evl->PrepareSend(Delegatee, ReplyId);
+ forwardedEv = ev;
+ } else {
+ IEventHandleFat* evf = IEventHandleFat::GetFat(ev);
+ forwardedEv = ev->HasEvent()
+ ? new IEventHandleFat(Delegatee, ReplyId, evf->ReleaseBase().Release(), evf->Flags, evf->Cookie)
+ : new IEventHandleFat(evf->GetTypeRewrite(), evf->Flags, Delegatee, ReplyId, evf->ReleaseChainBuffer(), evf->Cookie);
+ }
return forwardedEv;
}
diff --git a/library/cpp/actors/testlib/test_runtime.h b/library/cpp/actors/testlib/test_runtime.h
index 343b417b35e..c48708b975b 100644
--- a/library/cpp/actors/testlib/test_runtime.h
+++ b/library/cpp/actors/testlib/test_runtime.h
@@ -260,9 +260,10 @@ namespace NActors {
bool DispatchEvents(const TDispatchOptions& options = TDispatchOptions());
bool DispatchEvents(const TDispatchOptions& options, TDuration simTimeout);
bool DispatchEvents(const TDispatchOptions& options, TInstant simDeadline);
- void Send(IEventHandle* ev, ui32 senderNodeIndex = 0, bool viaActorSystem = false);
- void SendAsync(IEventHandle* ev, ui32 senderNodeIndex = 0);
- void Schedule(IEventHandle* ev, const TDuration& duration, ui32 nodeIndex = 0);
+ void Send(const TActorId& recipient, const TActorId& sender, TAutoPtr<IEventHandleLight> ev, ui32 senderNodeIndex = 0, bool viaActorSystem = false);
+ void Send(TAutoPtr<IEventHandle> ev, ui32 senderNodeIndex = 0, bool viaActorSystem = false);
+ void SendAsync(TAutoPtr<IEventHandle> ev, ui32 senderNodeIndex = 0);
+ void Schedule(TAutoPtr<IEventHandle> ev, const TDuration& duration, ui32 nodeIndex = 0);
void ClearCounters();
ui64 GetCounter(ui32 evType) const;
TActorId GetLocalServiceId(const TActorId& serviceId, ui32 nodeIndex = 0);
@@ -301,7 +302,7 @@ namespace NActors {
if (event->GetTypeRewrite() != eventType)
return false;
- TEvent* typedEvent = reinterpret_cast<TAutoPtr<TEventHandle<TEvent>>&>(event)->Get();
+ TEvent* typedEvent = event->Get<TEvent>();
if (predicate(*typedEvent)) {
handle = event;
return true;
@@ -314,7 +315,7 @@ namespace NActors {
Y_VERIFY(handle);
if (handle) {
- return reinterpret_cast<TAutoPtr<TEventHandle<TEvent>>&>(handle)->Get();
+ return handle->Get<TEvent>();
} else {
return nullptr;
}
@@ -369,7 +370,10 @@ namespace NActors {
TAutoPtr<IEventHandle> handle;
std::function<bool(const TEvent&)> truth = [](const TEvent&) { return true; };
GrabEdgeEventIf(handle, truth, simTimeout);
- return THolder(handle ? handle->Release<TEvent>().Release() : nullptr);
+ if (handle) {
+ return THolder<TEvent>(IEventHandle::Release<TEvent>(handle));
+ }
+ return {};
}
template<class TEvent>
@@ -398,7 +402,7 @@ namespace NActors {
Y_VERIFY(handle);
if (handle) {
return std::make_tuple(handle->Type == TEvents::EventType
- ? reinterpret_cast<TAutoPtr<TEventHandle<TEvents>>&>(handle)->Get()
+ ? handle->Get<TEvents>()
: static_cast<TEvents*>(nullptr)...);
}
return {};
@@ -503,7 +507,7 @@ namespace NActors {
private:
IActor* FindActor(const TActorId& actorId, TNodeDataBase* node) const;
- void SendInternal(IEventHandle* ev, ui32 nodeIndex, bool viaActorSystem);
+ void SendInternal(TAutoPtr<IEventHandle> ev, ui32 nodeIndex, bool viaActorSystem);
TEventMailBox& GetMailbox(ui32 nodeId, ui32 hint);
void ClearMailbox(ui32 nodeId, ui32 hint);
void HandleNonEmptyMailboxesForEachContext(TEventMailboxId mboxId);
@@ -647,7 +651,7 @@ namespace NActors {
TEvent* FindEvent(TEventsList& events) {
for (auto& event : events) {
if (event && event->GetTypeRewrite() == TEvent::EventType) {
- return static_cast<TEvent*>(event->GetBase());
+ return event->CastAsLocal<TEvent>();
}
}
@@ -657,8 +661,8 @@ namespace NActors {
template <typename TEvent>
TEvent* FindEvent(TEventsList& events, const std::function<bool(const TEvent&)>& predicate) {
for (auto& event : events) {
- if (event && event->GetTypeRewrite() == TEvent::EventType && predicate(*static_cast<TEvent*>(event->GetBase()))) {
- return static_cast<TEvent*>(event->GetBase());
+ if (event && event->GetTypeRewrite() == TEvent::EventType && predicate(*event->CastAsLocal<TEvent>())) {
+ return event->CastAsLocal<TEvent>();
}
}
@@ -671,7 +675,7 @@ namespace NActors {
for (auto& event : events) {
if (event && event->GetTypeRewrite() == TEvent::EventType) {
ev = event;
- return static_cast<TEvent*>(ev->GetBase());
+ return ev->CastAsLocal<TEvent>();
}
}
@@ -686,7 +690,7 @@ namespace NActors {
if (event && event->GetTypeRewrite() == TEvent::EventType) {
if (predicate(reinterpret_cast<const typename TEvent::TPtr&>(event))) {
ev = event;
- return static_cast<TEvent*>(ev->GetBase());
+ return ev->CastAsLocal<TEvent>();
}
}
}