attempt to make unit tests more deterministic

This commit is contained in:
arvidn 2015-06-05 23:02:07 -04:00
parent b2937712a2
commit 48bc00cbb7
3 changed files with 24 additions and 12 deletions

View File

@ -77,9 +77,13 @@ namespace lt = libtorrent;
#include <conio.h>
#endif
boost::uint32_t g_addr = 0x92343023;
address rand_v4()
{
return address_v4(((boost::uint32_t(rand()) << 16) | rand()) & 0xffffffff);
do {
g_addr += 0x3080ca;
} while (g_addr == 0);
return address_v4(g_addr);
}
#if TORRENT_USE_IPV6
@ -91,14 +95,19 @@ address rand_v6()
}
#endif
static boost::uint16_t g_port = 0;
tcp::endpoint rand_tcp_ep()
{
return tcp::endpoint(rand_v4(), rand() + 1024);
// make sure we don't procude the same "random" port twice
g_port = (g_port + 1) % 14038;
return tcp::endpoint(rand_v4(), g_port + 1024);
}
udp::endpoint rand_udp_ep()
{
return udp::endpoint(rand_v4(), rand() + 1024);
g_port = (g_port + 1) % 14037;
return udp::endpoint(rand_v4(), g_port + 1024);
}
std::map<std::string, boost::int64_t> get_counters(libtorrent::session& s)

View File

@ -39,6 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/ip_filter.hpp"
#include "libtorrent/peer_info.hpp"
#include "libtorrent/random.hpp"
#include "libtorrent/socket_io.hpp"
#include "test.hpp"
#include "setup_transfer.hpp"
@ -513,13 +514,15 @@ TORRENT_TEST(erase_peers)
for (int i = 0; i < 100; ++i)
{
TEST_EQUAL(st.erased.size(), 0);
torrent_peer* peer = add_peer(p, st, rand_tcp_ep());
tcp::endpoint ep = rand_tcp_ep();
torrent_peer* peer = add_peer(p, st, ep);
TEST_CHECK(peer);
if (peer == NULL || st.erased.size() > 0)
{
fprintf(stderr, "unexpected rejection of peer: %d in list. "
fprintf(stderr, "unexpected rejection of peer: %s | %d in list. "
"added peer %p, erased %d peers\n"
, p.num_peers(), peer, int(st.erased.size()));
, print_endpoint(ep).c_str(), p.num_peers(), peer
, int(st.erased.size()));
}
}
TEST_EQUAL(p.num_peers(), 100);