|
| 1 | +package uuid |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | +) |
| 7 | + |
| 8 | +func TestNewV6WithTime(t *testing.T) { |
| 9 | + testCases := map[string]string{ |
| 10 | + "test with current date": time.Now().Format(time.RFC3339), // now |
| 11 | + "test with past date": time.Now().Add(-1 * time.Hour * 24 * 365).Format(time.RFC3339), // 1 year ago |
| 12 | + "test with future date": time.Now().Add(time.Hour * 24 * 365).Format(time.RFC3339), // 1 year from now |
| 13 | + "test with different timezone": "2021-09-01T12:00:00+04:00", |
| 14 | + "test with negative timezone": "2021-09-01T12:00:00-12:00", |
| 15 | + "test with future date in different timezone": "2124-09-23T12:43:30+09:00", |
| 16 | + } |
| 17 | + |
| 18 | + for testName, inputTime := range testCases { |
| 19 | + t.Run(testName, func(t *testing.T) { |
| 20 | + customTime, err := time.Parse(time.RFC3339, inputTime) |
| 21 | + if err != nil { |
| 22 | + t.Errorf("time.Parse returned unexpected error %v", err) |
| 23 | + } |
| 24 | + id, err := NewV6WithTime(&customTime) |
| 25 | + if err != nil { |
| 26 | + t.Errorf("NewV6WithTime returned unexpected error %v", err) |
| 27 | + } |
| 28 | + |
| 29 | + if id.Version() != 6 { |
| 30 | + t.Errorf("got %d, want version 6", id.Version()) |
| 31 | + } |
| 32 | + unixTime := time.Unix(id.Time().UnixTime()) |
| 33 | + // Compare the times in UTC format, since the input time might have different timezone, |
| 34 | + // and the result is always in system timezone |
| 35 | + if customTime.UTC().Format(time.RFC3339) != unixTime.UTC().Format(time.RFC3339) { |
| 36 | + t.Errorf("got %s, want %s", unixTime.Format(time.RFC3339), customTime.Format(time.RFC3339)) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func TestNewV6FromTimeGeneratesUniqueUUIDs(t *testing.T) { |
| 43 | + now := time.Now() |
| 44 | + ids := make([]string, 0) |
| 45 | + runs := 26000 |
| 46 | + |
| 47 | + for i := 0; i < runs; i++ { |
| 48 | + now = now.Add(time.Nanosecond) // Without this line, we can generate only 16384 UUIDs for the same timestamp |
| 49 | + id, err := NewV6WithTime(&now) |
| 50 | + if err != nil { |
| 51 | + t.Errorf("NewV6WithTime returned unexpected error %v", err) |
| 52 | + } |
| 53 | + if id.Version() != 6 { |
| 54 | + t.Errorf("got %d, want version 6", id.Version()) |
| 55 | + } |
| 56 | + |
| 57 | + // Make sure we add only unique values |
| 58 | + if !contains(t, ids, id.String()) { |
| 59 | + ids = append(ids, id.String()) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + // Check we added all the UIDs |
| 64 | + if len(ids) != runs { |
| 65 | + t.Errorf("got %d UUIDs, want %d", len(ids), runs) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func BenchmarkNewV6WithTime(b *testing.B) { |
| 70 | + b.RunParallel(func(pb *testing.PB) { |
| 71 | + for pb.Next() { |
| 72 | + now := time.Now() |
| 73 | + _, err := NewV6WithTime(&now) |
| 74 | + if err != nil { |
| 75 | + b.Fatal(err) |
| 76 | + } |
| 77 | + } |
| 78 | + }) |
| 79 | +} |
| 80 | + |
| 81 | +func contains(t *testing.T, arr []string, str string) bool { |
| 82 | + t.Helper() |
| 83 | + |
| 84 | + for _, a := range arr { |
| 85 | + if a == str { |
| 86 | + return true |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + return false |
| 91 | +} |
0 commit comments