diff options
author | azevaykin <azevaykin@yandex-team.com> | 2023-11-09 14:06:02 +0300 |
---|---|---|
committer | azevaykin <azevaykin@yandex-team.com> | 2023-11-09 14:35:06 +0300 |
commit | 2ca8878b489e638f6cd85bde53d4edd523845098 (patch) | |
tree | 2464c6077459d3227fd4aac554835f720317a708 /library/cpp | |
parent | 0a6f359958788918e202d11bf6378829e85a2d37 (diff) | |
download | ydb-2ca8878b489e638f6cd85bde53d4edd523845098.tar.gz |
An example of using AddObserver in unit tests
Diffstat (limited to 'library/cpp')
-rw-r--r-- | library/cpp/actors/testlib/test_runtime.h | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/library/cpp/actors/testlib/test_runtime.h b/library/cpp/actors/testlib/test_runtime.h index 7e650565cc..f46d3906e4 100644 --- a/library/cpp/actors/testlib/test_runtime.h +++ b/library/cpp/actors/testlib/test_runtime.h @@ -236,7 +236,7 @@ namespace NActors { static bool DefaultFilterFunc(TTestActorRuntimeBase& runtime, TAutoPtr<IEventHandle>& event); static bool NopFilterFunc(TTestActorRuntimeBase& runtime, TAutoPtr<IEventHandle>& event, TDuration delay, TInstant& deadline); static void DefaultRegistrationObserver(TTestActorRuntimeBase& runtime, const TActorId& parentId, const TActorId& actorId); - TEventObserver SetObserverFunc(TEventObserver observerFunc); + TEventObserver SetObserverFunc(TEventObserver observerFunc); // deprecated, use AddObserver TScheduledEventsSelector SetScheduledEventsSelectorFunc(TScheduledEventsSelector scheduledEventsSelectorFunc); TEventFilter SetEventFilter(TEventFilter filterFunc); TScheduledEventFilter SetScheduledEventFilter(TScheduledEventFilter filterFunc); @@ -324,6 +324,22 @@ namespace NActors { TEventObserverCollection::iterator Iter; }; + // An example of using AddObserver in unit tests + /* + auto observerHolder = runtime.AddObserver<TEvDataShard::TEvRead>([&](TEvDataShard::TEvRead::TPtr& event) { + // Do something with the event inside the calback + Cout << "An event is observed " << ev->Get()->Record.ShortDebugString() << Endl; + + // Optionally reset the event, all subsequent handlers of this event will not be called + event.Reset(); + }); + + // Do something inside the main code of the unit test + + // Optionally remove the observer, otherwise it will be destroyed in its destructor + observerHolder.Remove(); + */ + template <typename TEvType> TEventObserverHolder AddObserver(std::function<void(typename TEvType::TPtr&)> observerFunc) { |