Skip to content

Commit

Permalink
Fix missing disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
kuznetsss committed Nov 20, 2024
1 parent 908b7e3 commit 27e9ed1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
1 change: 0 additions & 1 deletion src/rpc/common/Types.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@

#include <cstdint>
#include <expected>
#include <memory>
#include <string>
#include <utility>
#include <variant>
Expand Down
4 changes: 4 additions & 0 deletions src/web/ng/impl/ConnectionHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ ConnectionHandler::processConnection(ConnectionPtr connectionPtr, boost::asio::y
shouldCloseGracefully = parallelRequestResponseLoop(connectionRef, subscriptionContextInterfacePtr, yield);
break;
}

if (subscriptionContext != nullptr)
subscriptionContext->disconnect(yield);

if (shouldCloseGracefully)
connectionRef.close(yield);

Expand Down
30 changes: 30 additions & 0 deletions tests/unit/web/ng/impl/ConnectionHandlerTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,36 @@ TEST_F(ConnectionHandlerSequentialProcessingTest, SendSubscriptionMessage)
});
}

TEST_F(ConnectionHandlerSequentialProcessingTest, SubscriptionContextIsDisconnectedAfterProcessingFinished)
{
testing::StrictMock<testing::MockFunction<
Response(Request const&, ConnectionMetadata const&, web::SubscriptionContextPtr, boost::asio::yield_context)>>
wsHandlerMock;
connectionHandler_.onWs(wsHandlerMock.AsStdFunction());

testing::StrictMock<testing::MockFunction<void(web::SubscriptionContextInterface*)>> onDisconnectHook;

EXPECT_CALL(*mockWsConnection_, wasUpgraded).WillOnce(Return(true));
testing::Expectation const expectationReceiveCalled = EXPECT_CALL(*mockWsConnection_, receive)
.WillOnce(Return(makeRequest("", Request::HttpHeaders{})))
.WillOnce(Return(makeError(websocket::error::closed)));

EXPECT_CALL(wsHandlerMock, Call)
.WillOnce([&](Request const& request, auto&&, web::SubscriptionContextPtr subscriptionContext, auto&&) {
EXPECT_NE(subscriptionContext, nullptr);
subscriptionContext->onDisconnect(onDisconnectHook.AsStdFunction());
return Response(http::status::ok, "", request);
});

EXPECT_CALL(*mockWsConnection_, send).WillOnce(Return(std::nullopt));

EXPECT_CALL(onDisconnectHook, Call).After(expectationReceiveCalled);

runSpawn([this](boost::asio::yield_context yield) {
connectionHandler_.processConnection(std::move(mockWsConnection_), yield);
});
}

TEST_F(ConnectionHandlerSequentialProcessingTest, SubscriptionContextIsNullForHttpConnection)
{
std::string const target = "/some/target";
Expand Down

0 comments on commit 27e9ed1

Please sign in to comment.