Fix unstable unittest (#1102)

Properly mock wsbase
This commit is contained in:
cyan317
2024-01-15 12:06:14 +00:00
committed by GitHub
parent ce86572274
commit 350a45e7e2
12 changed files with 140 additions and 261 deletions

View File

@@ -17,9 +17,8 @@
*/
//==============================================================================
#include "feed/FeedBaseTest.h"
#include "feed/FeedTestUtil.h"
#include "feed/impl/LedgerFeed.h"
#include "util/Fixtures.h"
#include "util/TestObject.h"
#include <boost/asio/io_context.hpp>
@@ -29,12 +28,11 @@
#include <gtest/gtest.h>
#include <ripple/protocol/Fees.h>
#include <memory>
constexpr static auto LEDGERHASH = "4BC50C9B0D8515D3EAAE1E74B29A95804346C491EE1A95BF25E4AAB854A6A652";
using namespace feed::impl;
namespace json = boost::json;
using namespace testing;
using FeedLedgerTest = FeedBaseTest<LedgerFeed>;
@@ -68,11 +66,6 @@ TEST_F(FeedLedgerTest, SubPub)
ctx.run();
EXPECT_EQ(testFeedPtr->count(), 1);
// test publish
auto const ledgerinfo2 = CreateLedgerInfo(LEDGERHASH, 31);
auto fee2 = ripple::Fees();
fee2.reserve = 10;
testFeedPtr->pub(ledgerinfo2, fee2, "10-31", 8);
constexpr static auto ledgerPub =
R"({
"type":"ledgerClosed",
@@ -85,20 +78,23 @@ TEST_F(FeedLedgerTest, SubPub)
"validated_ledgers":"10-31",
"txn_count":8
})";
ctx.restart();
ctx.run();
EXPECT_EQ(json::parse(receivedFeedMessage()), json::parse(ledgerPub));
// test unsub
cleanReceivedFeed();
testFeedPtr->unsub(sessionPtr);
EXPECT_EQ(testFeedPtr->count(), 0);
// test publish
EXPECT_CALL(*mockSessionPtr, send(SharedStringJsonEq(ledgerPub))).Times(1);
auto const ledgerinfo2 = CreateLedgerInfo(LEDGERHASH, 31);
auto fee2 = ripple::Fees();
fee2.reserve = 10;
testFeedPtr->pub(ledgerinfo2, fee2, "10-31", 8);
ctx.restart();
ctx.run();
// test unsub, after unsub the send should not be called
testFeedPtr->unsub(sessionPtr);
EXPECT_EQ(testFeedPtr->count(), 0);
EXPECT_CALL(*mockSessionPtr, send(_)).Times(0);
testFeedPtr->pub(ledgerinfo2, fee2, "10-31", 8);
ctx.restart();
ctx.run();
EXPECT_TRUE(receivedFeedMessage().empty());
}
TEST_F(FeedLedgerTest, AutoDisconnect)
@@ -126,8 +122,8 @@ TEST_F(FeedLedgerTest, AutoDisconnect)
});
ctx.run();
EXPECT_EQ(testFeedPtr->count(), 1);
EXPECT_CALL(*mockSessionPtr, send(_)).Times(0);
// destroy the session
sessionPtr.reset();
EXPECT_EQ(testFeedPtr->count(), 0);