summaryrefslogtreecommitdiff
path: root/test/libapt/srvrecs_test.cc
diff options
context:
space:
mode:
authorDavid Kalnischkies <david@kalnischkies.de>2015-09-01 18:32:22 +0200
committerDavid Kalnischkies <david@kalnischkies.de>2015-09-01 19:01:45 +0200
commit76abe9a5aad69eb7e67295588c6825cdae0341af (patch)
treef118c70def8f789a07a2e81a3c8966fa56e322e7 /test/libapt/srvrecs_test.cc
parent63d609985eb7eefa5f2332bfe4fab96f017760a1 (diff)
use clock() as source for SRV randomness
Initializing a random number generator with the time since epoch could be good enough, but reaches its limits in test code as the 100 iterations might very well happen in the same second and hence the seed number is always the sameā€¦ clock() has a way lower resolution so it changes more often and not unimportant: If many users start the update at the same time it isn't to unlikely the SRV record will be ordered in the same second choosing the same for them all, but it seems less likely that the exact same clock() time has passed for them. And if I have to touch this, lets change a few other things as well to make me and/or compilers a bit happier (clang complained about the usage of a GNU extension in the testcase for example).
Diffstat (limited to 'test/libapt/srvrecs_test.cc')
-rw-r--r--test/libapt/srvrecs_test.cc24
1 files changed, 16 insertions, 8 deletions
diff --git a/test/libapt/srvrecs_test.cc b/test/libapt/srvrecs_test.cc
index 7e43cc757..4b63d2ccd 100644
--- a/test/libapt/srvrecs_test.cc
+++ b/test/libapt/srvrecs_test.cc
@@ -12,20 +12,28 @@ TEST(SrvRecTest, PopFromSrvRecs)
// the PopFromSrvRecs() is using a random number so we must
// run it a bunch of times to ensure we are not fooled by randomness
std::set<std::string> selected;
- for(int i=0;i<100;i++)
+ for(size_t i = 0; i < 100; ++i)
{
std::vector<SrvRec> Meep;
- SrvRec foo = {target:"foo", priority: 20, weight: 0, port: 80};
- Meep.push_back(foo);
-
- SrvRec bar = {target:"bar", priority: 20, weight: 0, port: 80};
- Meep.push_back(bar);
+ Meep.emplace_back("foo", 20, 0, 80);
+ Meep.emplace_back("bar", 20, 0, 80);
+ Meep.emplace_back("baz", 30, 0, 80);
- EXPECT_EQ(Meep.size(), 2);
- SrvRec result = PopFromSrvRecs(Meep);
+ EXPECT_EQ(Meep.size(), 3);
+ SrvRec const result = PopFromSrvRecs(Meep);
selected.insert(result.target);
// ensure that pop removed one element
+ EXPECT_EQ(Meep.size(), 2);
+ EXPECT_NE(result.target, "baz");
+
+ SrvRec const result2 = PopFromSrvRecs(Meep);
+ EXPECT_NE(result.target, result2.target);
+ EXPECT_NE(result2.target, "baz");
EXPECT_EQ(Meep.size(), 1);
+
+ SrvRec const result3 = PopFromSrvRecs(Meep);
+ EXPECT_EQ(result3.target, "baz");
+ EXPECT_TRUE(Meep.empty());
}
// ensure that after enough runs we end up with both selected