diff options
author | vladimir <vladimir@yandex-team.ru> | 2022-02-10 16:50:28 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:50:28 +0300 |
commit | 3e7ff6e4ee637c04455854159e84850e613ebc16 (patch) | |
tree | 1ea1786a47f104a0657e0f935ce63dcaeec3fd26 /library/cpp/messagebus/test/ut | |
parent | dad82c0e0157ebad6bfd7cf0e5fb3c15c42922b3 (diff) | |
download | ydb-3e7ff6e4ee637c04455854159e84850e613ebc16.tar.gz |
Restoring authorship annotation for <vladimir@yandex-team.ru>. Commit 1 of 2.
Diffstat (limited to 'library/cpp/messagebus/test/ut')
-rw-r--r-- | library/cpp/messagebus/test/ut/moduletest.h | 78 | ||||
-rw-r--r-- | library/cpp/messagebus/test/ut/one_way_ut.cpp | 126 |
2 files changed, 102 insertions, 102 deletions
diff --git a/library/cpp/messagebus/test/ut/moduletest.h b/library/cpp/messagebus/test/ut/moduletest.h index d5da72c0cbf..a128d3ab216 100644 --- a/library/cpp/messagebus/test/ut/moduletest.h +++ b/library/cpp/messagebus/test/ut/moduletest.h @@ -1,9 +1,9 @@ #pragma once - -/////////////////////////////////////////////////////////////////// -/// \file -/// \brief Example of using local session for communication. - + +/////////////////////////////////////////////////////////////////// +/// \file +/// \brief Example of using local session for communication. + #include <library/cpp/messagebus/test/helper/alloc_counter.h> #include <library/cpp/messagebus/test/helper/example.h> #include <library/cpp/messagebus/test/helper/message_handler_error.h> @@ -14,10 +14,10 @@ namespace NBus { namespace NTest { using namespace std; - + #define TYPE_HOSTINFOREQUEST 100 -#define TYPE_HOSTINFORESPONSE 101 - +#define TYPE_HOSTINFORESPONSE 101 + //////////////////////////////////////////////////////////////////// /// \brief DupDetect protocol that common between client and server //////////////////////////////////////////////////////////////////// @@ -32,11 +32,11 @@ namespace NBus { : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) { } - + ~THostInfoMessage() override { } }; - + //////////////////////////////////////////////////////////////////// /// \brief HostInfo reply class class THostInfoReply: public TBusMessage { @@ -49,11 +49,11 @@ namespace NBus { : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) { } - + ~THostInfoReply() override { } }; - + //////////////////////////////////////////////////////////////////// /// \brief HostInfo protocol that common between client and server class THostInfoProtocol: public TBusProtocol { @@ -67,7 +67,7 @@ namespace NBus { Y_UNUSED(data); Y_UNUSED(mess); } - + /// deserialized TBusData into new instance of the message TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override { Y_UNUSED(payload); @@ -81,23 +81,23 @@ namespace NBus { } } }; - + ////////////////////////////////////////////////////////////// /// \brief HostInfo handler (should convert it to module too) struct THostInfoHandler: public TBusServerHandlerError { TBusServerSessionPtr Session; TBusServerSessionConfig HostInfoConfig; THostInfoProtocol HostInfoProto; - + THostInfoHandler(TBusMessageQueue* queue) { Session = TBusServerSession::Create(&HostInfoProto, this, HostInfoConfig, queue); } - + void OnMessage(TOnMessageContext& mess) override { usleep(10 * 1000); /// pretend we are doing something - + TAutoPtr<THostInfoReply> reply(new THostInfoReply()); - + mess.SendReplyMove(reply); } @@ -105,7 +105,7 @@ namespace NBus { return TNetAddr("localhost", Session->GetActualListenPort()); } }; - + ////////////////////////////////////////////////////////////// /// \brief DupDetect handler (should convert it to module too) struct TDupDetectHandler: public TBusClientHandlerError { @@ -114,34 +114,34 @@ namespace NBus { TBusClientSessionPtr DupDetect; TBusClientSessionConfig DupDetectConfig; TExampleProtocol DupDetectProto; - + int NumMessages; int NumReplies; - + TDupDetectHandler(const TNetAddr& serverAddr, TBusMessageQueuePtr queue) : ServerAddr(serverAddr) { DupDetect = TBusClientSession::Create(&DupDetectProto, this, DupDetectConfig, queue); DupDetect->RegisterService("localhost"); } - + void Work() { NumMessages = 10; NumReplies = 0; - + for (int i = 0; i < NumMessages; i++) { TExampleRequest* mess = new TExampleRequest(&DupDetectProto.RequestCount); DupDetect->SendMessage(mess, &ServerAddr); } } - + void OnReply(TAutoPtr<TBusMessage> mess, TAutoPtr<TBusMessage> reply) override { Y_UNUSED(mess); Y_UNUSED(reply); NumReplies++; } }; - + ///////////////////////////////////////////////////////////////// /// \brief DupDetect module @@ -151,12 +151,12 @@ namespace NBus { TBusClientSessionPtr HostInfoClientSession; TBusClientSessionConfig HostInfoConfig; THostInfoProtocol HostInfoProto; - + TExampleProtocol DupDetectProto; TBusServerSessionConfig DupDetectConfig; - + TNetAddr ListenAddr; - + TDupDetectModule(const TNetAddr& hostInfoAddr) : TBusModule("DUPDETECTMODULE") , HostInfoAddr(hostInfoAddr) @@ -166,10 +166,10 @@ namespace NBus { bool Init(TBusMessageQueue* queue) { HostInfoClientSession = CreateDefaultSource(*queue, &HostInfoProto, HostInfoConfig); HostInfoClientSession->RegisterService("localhost"); - + return TBusModule::CreatePrivateSessions(queue); } - + TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override { TBusServerSessionPtr session = CreateDefaultDestination(queue, &DupDetectProto, DupDetectConfig); @@ -177,14 +177,14 @@ namespace NBus { return session; } - + /// entry point into module, first function to call TJobHandler Start(TBusJob* job, TBusMessage* mess) override { TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); Y_UNUSED(dmess); - + THostInfoMessage* hmess = new THostInfoMessage(); - + /// send message to imaginary hostinfo server job->Send(hmess, HostInfoClientSession, TReplyHandler(), 0, HostInfoAddr); @@ -195,27 +195,27 @@ namespace NBus { TJobHandler ProcessHostInfo(TBusJob* job, TBusMessage* mess) { TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); Y_UNUSED(dmess); - + THostInfoMessage* hmess = job->Get<THostInfoMessage>(); THostInfoReply* hreply = job->Get<THostInfoReply>(); EMessageStatus hstatus = job->GetStatus<THostInfoMessage>(); Y_ASSERT(hmess != nullptr); Y_ASSERT(hreply != nullptr); Y_ASSERT(hstatus == MESSAGE_OK); - + return TJobHandler(&TDupDetectModule::Finish); } /// last handler sends reply and returns NULL TJobHandler Finish(TBusJob* job, TBusMessage* mess) { Y_UNUSED(mess); - + TExampleResponse* reply = new TExampleResponse(&DupDetectProto.ResponseCount); job->SendReply(reply); - + return nullptr; } }; - - } + + } } diff --git a/library/cpp/messagebus/test/ut/one_way_ut.cpp b/library/cpp/messagebus/test/ut/one_way_ut.cpp index 9c21227e2bd..61d3a465a7d 100644 --- a/library/cpp/messagebus/test/ut/one_way_ut.cpp +++ b/library/cpp/messagebus/test/ut/one_way_ut.cpp @@ -1,134 +1,134 @@ -/////////////////////////////////////////////////////////////////// -/// \file +/////////////////////////////////////////////////////////////////// +/// \file /// \brief Example of reply-less communication -/// This example demostrates how asynchronous message passing library -/// can be used to send message and do not wait for reply back. -/// The usage of reply-less communication should be restricted to -/// low-throughput clients and high-throughput server to provide reasonable -/// utility. Removing replies from the communication removes any restriction -/// on how many message can be send to server and rougue clients may overwelm -/// server without thoughtput control. +/// This example demostrates how asynchronous message passing library +/// can be used to send message and do not wait for reply back. +/// The usage of reply-less communication should be restricted to +/// low-throughput clients and high-throughput server to provide reasonable +/// utility. Removing replies from the communication removes any restriction +/// on how many message can be send to server and rougue clients may overwelm +/// server without thoughtput control. -/// 1) To implement reply-less client \n +/// 1) To implement reply-less client \n /// Call NBus::TBusSession::AckMessage() /// from within NBus::IMessageHandler::OnSent() handler when message has /// gone into wire on client end. See example in NBus::NullClient::OnMessageSent(). -/// Discard identity for reply message. +/// Discard identity for reply message. -/// 2) To implement reply-less server \n +/// 2) To implement reply-less server \n -/// Call NBus::TBusSession::AckMessage() from within NBus::IMessageHandler::OnMessage() +/// Call NBus::TBusSession::AckMessage() from within NBus::IMessageHandler::OnMessage() /// handler when message has been received on server end. /// See example in NBus::NullServer::OnMessage(). -/// Discard identity for reply message. - +/// Discard identity for reply message. + #include <library/cpp/messagebus/test/helper/alloc_counter.h> #include <library/cpp/messagebus/test/helper/example.h> #include <library/cpp/messagebus/test/helper/hanging_server.h> #include <library/cpp/messagebus/test/helper/message_handler_error.h> #include <library/cpp/messagebus/test/helper/object_count_check.h> #include <library/cpp/messagebus/test/helper/wait_for.h> - + #include <library/cpp/messagebus/ybus.h> -using namespace std; +using namespace std; using namespace NBus; using namespace NBus::NPrivate; using namespace NBus::NTest; - -//////////////////////////////////////////////////////////////////// -//////////////////////////////////////////////////////////////////// -/// \brief Reply-less client and handler + +//////////////////////////////////////////////////////////////////// +//////////////////////////////////////////////////////////////////// +/// \brief Reply-less client and handler struct NullClient : TBusClientHandlerError { TNetAddr ServerAddr; TBusMessageQueuePtr Queue; TBusClientSessionPtr Session; TExampleProtocol Proto; - - /// constructor creates instances of protocol and session + + /// constructor creates instances of protocol and session NullClient(const TNetAddr& serverAddr, const TBusClientSessionConfig& sessionConfig = TBusClientSessionConfig()) : ServerAddr(serverAddr) { UNIT_ASSERT(serverAddr.GetPort() > 0); - - /// create or get instance of message queue, need one per application + + /// create or get instance of message queue, need one per application Queue = CreateMessageQueue(); - - /// register source/client session + + /// register source/client session Session = TBusClientSession::Create(&Proto, this, sessionConfig, Queue); /// register service, announce to clients via LocatorService Session->RegisterService("localhost"); - } - + } + ~NullClient() override { Session->Shutdown(); } - /// dispatch of requests is done here + /// dispatch of requests is done here void Work() { - int batch = 10; - + int batch = 10; + for (int i = 0; i < batch; i++) { TExampleRequest* mess = new TExampleRequest(&Proto.RequestCount); mess->Data = "TADA"; Session->SendMessageOneWay(mess, &ServerAddr); - } - } - + } + } + void OnMessageSentOneWay(TAutoPtr<TBusMessage>) override { - } -}; - -///////////////////////////////////////////////////////////////////// -/// \brief Reply-less server and handler + } +}; + +///////////////////////////////////////////////////////////////////// +/// \brief Reply-less server and handler class NullServer: public TBusServerHandlerError { -public: - /// session object to maintian +public: + /// session object to maintian TBusMessageQueuePtr Queue; TBusServerSessionPtr Session; TExampleProtocol Proto; - -public: + +public: TAtomic NumMessages; - - NullServer() { - NumMessages = 0; - - /// create or get instance of single message queue, need one for application + + NullServer() { + NumMessages = 0; + + /// create or get instance of single message queue, need one for application Queue = CreateMessageQueue(); - - /// register destination session + + /// register destination session TBusServerSessionConfig sessionConfig; Session = TBusServerSession::Create(&Proto, this, sessionConfig, Queue); - } - + } + ~NullServer() override { Session->Shutdown(); } - /// when message comes do not send reply, just acknowledge + /// when message comes do not send reply, just acknowledge void OnMessage(TOnMessageContext& mess) override { TExampleRequest* fmess = static_cast<TExampleRequest*>(mess.GetMessage()); - + Y_ASSERT(fmess->Data == "TADA"); - - /// tell session to forget this message and never expect any reply + + /// tell session to forget this message and never expect any reply mess.ForgetRequest(); - + AtomicIncrement(NumMessages); - } - - /// this handler should not be called because this server does not send replies + } + + /// this handler should not be called because this server does not send replies void OnSent(TAutoPtr<TBusMessage> mess) override { Y_UNUSED(mess); Y_FAIL("This server does not sent replies"); } -}; - +}; + Y_UNIT_TEST_SUITE(TMessageBusTests_OneWay) { Y_UNIT_TEST(Simple) { TObjectCountCheck objectCountCheck; |