66#include < gtest/gtest.h>
77#include < libp2p/protocol/kademlia/impl/peer_routing_table_impl.hpp>
88#include < qtils/test/outcome.hpp>
9+ #include < random>
910#include < unordered_set>
1011
1112#include < libp2p/common/literals.hpp>
1213#include " mock/libp2p/peer/identity_manager_mock.hpp"
1314#include " testutil/libp2p/peer.hpp"
15+ #include " testutil/libp2p/random.hpp"
1416#include " testutil/prepare_loggers.hpp"
1517
1618using namespace libp2p ;
@@ -50,8 +52,6 @@ bool hasPeer(A &peerset, PeerId &peer) {
5052}
5153
5254TEST_F (PeerRoutingTableTest, BusWorks) {
53- srand (0 ); // to make test deterministic
54-
5555 auto &addCh = bus_->getChannel <event::protocol::kademlia::PeerAddedChannel>();
5656 auto &remCh =
5757 bus_->getChannel <event::protocol::kademlia::PeerRemovedChannel>();
@@ -85,8 +85,6 @@ TEST_F(PeerRoutingTableTest, BusWorks) {
8585 * https://sourcegraph.com/github.com/libp2p/go-libp2p-kbucket@HEAD/-/blob/table_test.go#L168
8686 */
8787TEST_F (PeerRoutingTableTest, FindMultiple) {
88- srand (0 ); // to make test deterministic
89-
9088 std::vector<PeerId> peers;
9189 std::generate_n (std::back_inserter (peers), 18 , testutil::randomPeerId);
9290
@@ -106,7 +104,6 @@ TEST_F(PeerRoutingTableTest, FindMultiple) {
106104 */
107105TEST_F (PeerRoutingTableTest, RecyclingTest) {
108106 config_->maxBucketSize = 1 ;
109- srand (0 ); // to make test deterministic
110107 auto &addCh = bus_->getChannel <event::protocol::kademlia::PeerAddedChannel>();
111108 auto &remCh =
112109 bus_->getChannel <event::protocol::kademlia::PeerRemovedChannel>();
@@ -125,7 +122,7 @@ TEST_F(PeerRoutingTableTest, RecyclingTest) {
125122 std::vector<PeerId> peers;
126123
127124 // Generate peers for first bucket, in count more than bucket capacity
128- for ( int i = 0 ; i < 3 ; ++i ) {
125+ while (peers. size () != 3 ) {
129126 auto peer_id = testutil::randomPeerId ();
130127 NodeId node_id (peer_id);
131128 if (node_id.commonPrefixLen (NodeId (self_id)) == 0 ) {
@@ -163,7 +160,6 @@ TEST_F(PeerRoutingTableTest, RecyclingTest) {
163160
164161TEST_F (PeerRoutingTableTest, PreferLongLivedPeers) {
165162 config_->maxBucketSize = 2 ;
166- srand (0 ); // to make test deterministic
167163 auto &addCh = bus_->getChannel <event::protocol::kademlia::PeerAddedChannel>();
168164 auto &remCh =
169165 bus_->getChannel <event::protocol::kademlia::PeerRemovedChannel>();
@@ -210,7 +206,6 @@ TEST_F(PeerRoutingTableTest, PreferLongLivedPeers) {
210206
211207TEST_F (PeerRoutingTableTest, EldestRecycledIfNotPermanent) {
212208 config_->maxBucketSize = 3 ;
213- srand (0 ); // to make test deterministic
214209
215210 std::vector<PeerId> peers;
216211
@@ -239,7 +234,6 @@ TEST_F(PeerRoutingTableTest, EldestRecycledIfNotPermanent) {
239234
240235TEST_F (PeerRoutingTableTest, EldestPrefferedIfPermanent) {
241236 config_->maxBucketSize = 3 ;
242- srand (0 ); // to make test deterministic
243237
244238 std::vector<PeerId> peers;
245239
@@ -272,14 +266,15 @@ TEST_F(PeerRoutingTableTest, EldestPrefferedIfPermanent) {
272266 */
273267TEST_F (PeerRoutingTableTest, Update) {
274268 config_->maxBucketSize = 10 ;
275- srand (0 ); // to make test deterministic
276269
277270 std::vector<PeerId> peers;
278271 std::generate_n (std::back_inserter (peers), 100 , testutil::randomPeerId);
279272
273+ std::uniform_int_distribution<size_t > dist (0 , peers.size () - 1 );
274+ auto & rng = testutil::getTestRng ();
280275 // 10000 random updates among 100 existing peers
281276 for (int i = 0 ; i < 10000 ; i++) {
282- int index = rand () % peers. size ( );
277+ size_t index = dist (rng );
283278 [[maybe_unused]] auto result = table_->update (peers[index], false );
284279 }
285280
@@ -300,7 +295,6 @@ TEST_F(PeerRoutingTableTest, Update) {
300295 */
301296TEST_F (PeerRoutingTableTest, Find) {
302297 config_->maxBucketSize = 10 ;
303- srand (0 ); // to make test deterministic
304298
305299 const auto nPeers = 5 ;
306300
0 commit comments