From 51ad77e952be1f794faf95d0cd0b7dcca1581d6d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 5 Jul 2009 23:59:11 -0400 Subject: [PATCH] Bug Fix - Fixed bug that was causing more non-unique UUIDs. Here are the changes made: - Task.cpp, tasks are no longer provided with a UUID in Task::Task. This prevents the global context.task from being constructed before srandom/srand is called. - main.cpp, instead of srandom/srand (time (NULL)), it now uses struct timeval tv_usec member, which has a micro-second granularity, instead of time (NULL) which has a second granularity. When "task add ..." is called in a unit test, several calls are made per second, this the random number generator is seeded with the same value. - Modified the unit test to cover all 6 tasks created, instead of 5. --- src/Task.cpp | 2 -- src/main.cpp | 8 +++++--- src/tests/bug.uuid.t | 9 +++++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Task.cpp b/src/Task.cpp index 8aefd4956..fb565082e 100644 --- a/src/Task.cpp +++ b/src/Task.cpp @@ -38,8 +38,6 @@ Task::Task () : id (0) { - // Each new task gets a uuid. - set ("uuid", uuid ()); // No i18n } //////////////////////////////////////////////////////////////////////////////// diff --git a/src/main.cpp b/src/main.cpp index b5e956cbf..a85ee68c5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -27,7 +27,7 @@ #include #include -#include +#include #include "Context.h" #include "../auto.h" @@ -36,10 +36,12 @@ Context context; int main (int argc, char** argv) { // Set up randomness. + struct timeval tv; + ::gettimeofday (&tv, NULL); #ifdef HAVE_SRANDOM - srandom (time (NULL)); + srandom (tv.tv_usec); #else - srand (time (NULL)); + srand (tv.tv_usec); #endif int status = 0; diff --git a/src/tests/bug.uuid.t b/src/tests/bug.uuid.t index c1282b1a1..2a462fb8b 100755 --- a/src/tests/bug.uuid.t +++ b/src/tests/bug.uuid.t @@ -73,8 +73,13 @@ $output = qx{../task rc:uuid.rc 5 info}; push @all_uuids, $uuid; $unique_uuids{$uuid} = undef; -is (scalar (@all_uuids), 5, '5 tasks created'); -is (scalar (keys %unique_uuids), 5, '5 unique UUIDs'); +$output = qx{../task rc:uuid.rc 6 info}; +($uuid) = $output =~ /UUID\s+(\S+)/; +push @all_uuids, $uuid; +$unique_uuids{$uuid} = undef; + +is (scalar (@all_uuids), 6, '6 tasks created'); +is (scalar (keys %unique_uuids), 6, '6 unique UUIDs'); # Cleanup. unlink 'pending.data';