Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add NewConnectionStrategy to SingleConnectionListener #747

Merged
merged 3 commits into from
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 18 additions & 8 deletions services/network/SingleConnectionListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace services
, listener(connectionFactory.Listen(port, *this))
{}

void SingleConnectionListener::SetNewConnectionStrategy(NewConnectionStrategy& newConnectionStrategy)
{
this->newConnectionStrategy = &newConnectionStrategy;
}

void SingleConnectionListener::Stop(const infra::Function<void()>& onDone)
{
listener = nullptr;
Expand All @@ -20,13 +25,25 @@ namespace services
this->createdObserver = std::move(createdObserver);
this->address = address;

newConnectionStrategy->StopCurrentConnection(*this);
}

void SingleConnectionListener::StopCurrentConnection(SingleConnectionListener& listener)
{
Stop([this]()
{
CreateObserver();
newConnectionStrategy->StartNewConnection();
},
false);
}

void SingleConnectionListener::StartNewConnection()
{
connection.OnAllocatable(infra::emptyFunction);
auto proxyPtr = connection.Emplace(connectionCreator, address);
this->createdObserver(infra::MakeContainedSharedObject(**proxyPtr, proxyPtr));
}

void SingleConnectionListener::Stop(const infra::Function<void()>& onDone, bool force)
{
if (connection.Allocatable())
Expand All @@ -44,11 +61,4 @@ namespace services
}
}
}

void SingleConnectionListener::CreateObserver()
{
connection.OnAllocatable(infra::emptyFunction);
auto proxyPtr = connection.Emplace(connectionCreator, address);
this->createdObserver(infra::MakeContainedSharedObject(**proxyPtr, proxyPtr));
}
}
23 changes: 22 additions & 1 deletion services/network/SingleConnectionListener.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@

namespace services
{
class SingleConnectionListener;

class NewConnectionStrategy
{
public:
NewConnectionStrategy() = default;
NewConnectionStrategy(const NewConnectionStrategy& other) = delete;
NewConnectionStrategy& operator=(const NewConnectionStrategy& other) = delete;
~NewConnectionStrategy() = default;

virtual void StopCurrentConnection(SingleConnectionListener& listener) = 0;
virtual void StartNewConnection() = 0;
};

class SingleConnectionListener
: public Stoppable
, public NewConnectionStrategy
, private ServerConnectionObserverFactory
{
public:
Expand All @@ -20,17 +35,23 @@ namespace services

SingleConnectionListener(ConnectionFactory& connectionFactory, uint16_t port, const Creators& creators);

void SetNewConnectionStrategy(NewConnectionStrategy& newConnectionStrategy);

// Implementation of Stoppable
void Stop(const infra::Function<void()>& onDone) override;

// Implementation of NewConnectionStrategy
void StopCurrentConnection(SingleConnectionListener& listener) override;
void StartNewConnection() override;

private:
// Implementation of ServerConnectionObserverFactory
void ConnectionAccepted(infra::AutoResetFunction<void(infra::SharedPtr<services::ConnectionObserver> connectionObserver)>&& createdObserver, IPAddress address) override;

void Stop(const infra::Function<void()>& onDone, bool force);
void CreateObserver();

private:
NewConnectionStrategy* newConnectionStrategy = this;
decltype(Creators::connectionCreator) connectionCreator;
infra::NotifyingSharedOptional<infra::ProxyCreator<decltype(Creators::connectionCreator)>> connection;
infra::SharedPtr<void> listener;
Expand Down
137 changes: 69 additions & 68 deletions services/network/test/TestSingleConnectionListener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ class SingleConnectionListenerTest
: public services::ConnectionObserverMock
{
public:
MOCK_METHOD1(Constructed, void(services::IPAddress address));
MOCK_METHOD0(Destructed, void());
MOCK_METHOD(void, Constructed, (services::IPAddress address), ());
MOCK_METHOD(void, Destructed, (), ());
};

class NewConnectionStrategyMock
: public services::NewConnectionStrategy
{
public:
MOCK_METHOD(void, StopCurrentConnection, (services::SingleConnectionListener & listener), (override));
MOCK_METHOD(void, StartNewConnection, (), (override));
};

testing::StrictMock<ConnectionObserverMock> connectionObserverMock;
Expand Down Expand Up @@ -47,6 +55,24 @@ class SingleConnectionListenerTest
ConnectionObserverMock& base;
};

void ConnectionAccepted()
{
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
}

void AcceptConnection()
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_)).WillOnce(testing::SaveArg<0>(&connectionObserver));
ConnectionAccepted();
}

infra::Creator<services::ConnectionObserver, ConnectionObserverStorage, void(services::IPAddress address)> connectionObserverCreator{ [this](infra::Optional<ConnectionObserverStorage>& connectionObserver, services::IPAddress address)
{
connectionObserver.Emplace(connectionObserverMock, address);
Expand All @@ -62,44 +88,26 @@ class SingleConnectionListenerTest
infra::MockCallback<void(infra::SharedPtr<services::ConnectionObserver>)> connectionAccepted;
infra::SharedPtr<services::ConnectionObserver> connectionObserver;
testing::StrictMock<services::ConnectionMock> connection;
testing::StrictMock<NewConnectionStrategyMock> newConnectionStrategy;
};

TEST_F(SingleConnectionListenerTest, create_connection)
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_)).WillOnce(testing::SaveArg<0>(&connectionObserver));
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
AcceptConnection();

EXPECT_CALL(connectionObserverMock, Destructed());
connectionObserver = nullptr;
}

TEST_F(SingleConnectionListenerTest, second_connection_cancels_first)
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_));
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
AcceptConnection();

EXPECT_CALL(connection, CloseAndDestroy());
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
EXPECT_CALL(connection, CloseAndDestroy()).WillOnce(testing::Invoke([this]()
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
connectionObserver = nullptr;
}));
ConnectionAccepted();

EXPECT_CALL(connectionObserverMock, Destructed());
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
Expand All @@ -111,28 +119,14 @@ TEST_F(SingleConnectionListenerTest, second_connection_cancels_first)

TEST_F(SingleConnectionListenerTest, second_connection_while_first_is_destroyed_but_not_allocatable)
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_)).WillOnce(testing::SaveArg<0>(&connectionObserver));
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
AcceptConnection();

infra::WeakPtr<services::ConnectionObserver> weakObserver = connectionObserver;
EXPECT_CALL(connectionObserverMock, Destructed());
connection.Detach();
connectionObserver = nullptr;

serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
ConnectionAccepted();

EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_));
Expand All @@ -143,15 +137,7 @@ TEST_F(SingleConnectionListenerTest, second_connection_while_first_is_destroyed_

TEST_F(SingleConnectionListenerTest, third_connection_while_second_has_not_yet_been_made)
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_)).WillOnce(testing::SaveArg<0>(&connectionObserver));
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
AcceptConnection();

infra::WeakPtr<services::ConnectionObserver> weakObserver = connectionObserver;
EXPECT_CALL(connectionObserverMock, Destructed());
Expand All @@ -165,13 +151,7 @@ TEST_F(SingleConnectionListenerTest, third_connection_while_second_has_not_yet_b
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });

serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
ConnectionAccepted();

EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_)).WillOnce(testing::SaveArg<0>(&connectionObserver));
Expand All @@ -183,15 +163,8 @@ TEST_F(SingleConnectionListenerTest, third_connection_while_second_has_not_yet_b

TEST_F(SingleConnectionListenerTest, Stop_aborts_connection)
{
EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_));
serverConnectionObserverFactory->ConnectionAccepted(
[this](infra::SharedPtr<services::ConnectionObserver> connectionObserver)
{
connection.Attach(connectionObserver);
connectionAccepted.callback(connectionObserver);
},
services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } });
AcceptConnection();
connectionObserver = nullptr;

infra::MockCallback<void()> onDone;
EXPECT_CALL(connection, AbortAndDestroy());
Expand All @@ -204,3 +177,31 @@ TEST_F(SingleConnectionListenerTest, Stop_aborts_connection)
EXPECT_CALL(onDone, callback());
connection.Detach();
}

TEST_F(SingleConnectionListenerTest, NewConnectionStrategy)
{
listener.SetNewConnectionStrategy(newConnectionStrategy);

EXPECT_CALL(newConnectionStrategy, StopCurrentConnection(testing::Ref(listener)));
AcceptConnection();
EXPECT_CALL(newConnectionStrategy, StartNewConnection());
listener.StopCurrentConnection(listener);
listener.StartNewConnection();
connectionObserver = nullptr;

EXPECT_CALL(newConnectionStrategy, StopCurrentConnection(testing::Ref(listener)));
ConnectionAccepted();

EXPECT_CALL(connection, CloseAndDestroy());
listener.StopCurrentConnection(listener);

EXPECT_CALL(connectionObserverMock, Destructed());
EXPECT_CALL(newConnectionStrategy, StartNewConnection());
connection.Detach();

EXPECT_CALL(connectionObserverMock, Constructed(services::IPAddress{ services::IPv4Address{ 1, 2, 3, 4 } }));
EXPECT_CALL(connectionAccepted, callback(testing::_));
listener.StartNewConnection();

EXPECT_CALL(connectionObserverMock, Destructed());
}
Loading