1. Patrick Mézard
  2. go-uuid

Commits

dsymonds  committed 5fac954

go-uuid: Make uuid_test.go independent of the system clock.

The test depends on an advancing clock, but it can sometimes run
fast enough that the clock has not advanced.

R=borman
CC=borman
http://codereview.appspot.com/6243069

Committer: Paul Borman <borman@google.com>

  • Participants
  • Parent commits 4f1c1d3
  • Branches default

Comments (0)

Files changed (2)

File uuid/time.go

View file
 var (
 	lasttime  uint64 // last time we returned
 	clock_seq uint16 // clock sequence for this run
+
+	timeNow = time.Now // for testing
 )
 
 // UnixTime converts t the number of seconds and nanoseconds using the Unix
 // adjusts the clock sequence as needed.  An error is returned if the current
 // time cannot be determined.
 func GetTime() (Time, error) {
-	t := time.Now()
+	t := timeNow()
 
 	// If we don't have a clock sequence already, set one.
 	if clock_seq == 0 {

File uuid/uuid_test.go

View file
 }
 
 func TestClockSeq(t *testing.T) {
+	// Fake time.Now for this test to return a monotonically advancing time; restore it at end.
+	defer func(orig func() time.Time) { timeNow = orig }(timeNow)
+	monTime := time.Now()
+	timeNow = func() time.Time {
+		monTime = monTime.Add(1 * time.Second)
+		return monTime
+	}
+
 	SetClockSequence(-1)
 	uuid1 := NewUUID()
 	uuid2 := NewUUID()