Skip to content

Commit

Permalink
Switch TestReliableMessageProtocol to async message delivery (#13099)
Browse files Browse the repository at this point in the history
* Convert TestReliableMessageProtocol to using LoopbackMessagingContext.

* Use async message delivery in TestReliableMessageProtocol.
  • Loading branch information
bzbarsky-apple authored and pull[bot] committed Apr 7, 2022
1 parent 19d7d60 commit 1268729
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 62 deletions.
4 changes: 2 additions & 2 deletions src/app/tests/AppTestContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace Test {
* @brief The context of test cases for messaging layer. It wil initialize network layer and system layer, and create
* two secure sessions, connected with each other. Exchanges can be created for each secure session.
*/
class AppContext : public LoopbackMessagingContext<LoopbackTransport>
class AppContext : public LoopbackMessagingContext<>
{
typedef LoopbackMessagingContext<LoopbackTransport> Super;
typedef LoopbackMessagingContext<> Super;

public:
/// Initialize the underlying layers.
Expand Down
15 changes: 15 additions & 0 deletions src/messaging/tests/MessagingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CHIP_ERROR MessagingContext::Init(TransportMgrBase * transport, IOContext * ioCo
mInitialized = true;

mIOContext = ioContext;
mTransport = transport;

ReturnErrorOnFailure(mSessionManager.Init(&GetSystemLayer(), transport, &mMessageCounterManager));

Expand Down Expand Up @@ -56,6 +57,20 @@ CHIP_ERROR MessagingContext::Shutdown()
return CHIP_NO_ERROR;
}

CHIP_ERROR MessagingContext::InitFromExisting(const MessagingContext & existing)
{
return Init(existing.mTransport, existing.mIOContext);
}

CHIP_ERROR MessagingContext::ShutdownAndRestoreExisting(MessagingContext & existing)
{
CHIP_ERROR err = Shutdown();
// Point the transport back to the original session manager, since we had
// pointed it to ours.
existing.mTransport->SetSessionManager(&existing.GetSecureSessionManager());
return err;
}

SessionHandle MessagingContext::GetSessionBobToAlice()
{
return mSessionBobToAlice.Get();
Expand Down
13 changes: 12 additions & 1 deletion src/messaging/tests/MessagingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class MessagingContext
// Shutdown all layers, finalize operations
CHIP_ERROR Shutdown();

// Initialize from an existing messaging context. Useful if we want to
// share some state (like the transport).
CHIP_ERROR InitFromExisting(const MessagingContext & existing);

// The shutdown method to use if using InitFromExisting. Must pass in the
// same existing context as was passed to InitFromExisting.
CHIP_ERROR ShutdownAndRestoreExisting(MessagingContext & existing);

static Inet::IPAddress GetAddress()
{
Inet::IPAddress addr;
Expand Down Expand Up @@ -98,6 +106,7 @@ class MessagingContext
Messaging::ExchangeManager mExchangeManager;
secure_channel::MessageCounterManager mMessageCounterManager;
IOContext * mIOContext;
TransportMgrBase * mTransport; // Only needed for InitFromExisting.

NodeId mBobNodeId = 123654;
NodeId mAliceNodeId = 111222333;
Expand All @@ -115,7 +124,7 @@ class MessagingContext
FabricIndex mDestFabricIndex = 0;
};

template <typename Transport>
template <typename Transport = LoopbackTransport>
class LoopbackMessagingContext : public MessagingContext
{
public:
Expand Down Expand Up @@ -166,6 +175,8 @@ class LoopbackMessagingContext : public MessagingContext

Transport & GetLoopback() { return mTransportManager.GetTransport().template GetImplAtIndex<0>(); }

TransportMgrBase & GetTransportMgr() { return mTransportManager; }

/*
* For unit-tests that simulate end-to-end transmission and reception of messages in loopback mode,
* this mode better replicates a real-functioning stack that correctly handles the processing
Expand Down
2 changes: 1 addition & 1 deletion src/messaging/tests/TestExchangeMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ using namespace chip::Inet;
using namespace chip::Transport;
using namespace chip::Messaging;

using TestContext = chip::Test::LoopbackMessagingContext<Test::LoopbackTransport>;
using TestContext = Test::LoopbackMessagingContext<>;

enum : uint8_t
{
Expand Down
Loading

0 comments on commit 1268729

Please sign in to comment.