pbacsko commented on code in PR #888:
URL: https://github.com/apache/yunikorn-core/pull/888#discussion_r1634989508
##########
pkg/common/utils_test.go:
##########
@@ -380,3 +383,127 @@ func TestGetConfigurationInt(t *testing.T) {
})
}
}
+
+func TestZeroTimeInUnixNano(t *testing.T) {
+ tests := []struct {
+ name string
+ input time.Time
+ expected *int64
+ }{
+ {
+ name: "zero time",
+ input: time.Time{},
+ expected: nil,
+ },
+ {
+ name: "non-zero time",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.UTC),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.UTC).UnixNano()
+ return &tInt
+ }(),
+ },
+ {
+ name: "time in different timezone",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.FixedZone("UTC+8", 8*60*60)),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.FixedZone("UTC+8", 8*60*60)).UnixNano()
+ return &tInt
+ }(),
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := ZeroTimeInUnixNano(tc.input)
+ if tc.expected == nil {
+ assert.Equal(t, tc.expected, result)
+ } else {
+ assert.Equal(t, *tc.expected, *result)
+ }
+ })
+ }
+}
+
+func TestSynchronizeGetNewUUID(t *testing.T) {
+ const numUUIDs = 1000
+ uuids := make(map[string]struct{}, numUUIDs)
+
+ for i := 0; i < numUUIDs; i++ {
+ newUUID := GetNewUUID()
+ if _, err := uuid.Parse(newUUID); err != nil {
+ t.Errorf("Generated UUID is not valid: %s", newUUID)
+ }
+ if _, exists := uuids[newUUID]; exists {
+ t.Errorf("Generated UUID is duplicated: %s", newUUID)
+ }
+ uuids[newUUID] = struct{}{}
+ }
+}
+
+func TestAsynchronousGetNewUUID(t *testing.T) {
+ const numUUIDs = 1000
+ uuids := make(map[string]struct{}, numUUIDs)
+ var mu sync.Mutex
+ var wg sync.WaitGroup
+
+ for i := 0; i < numUUIDs; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ newUUID := GetNewUUID()
+ if _, err := uuid.Parse(newUUID); err != nil {
+ t.Errorf("Generated UUID is not valid: %s",
newUUID)
+ }
+ mu.Lock()
+ if _, exists := uuids[newUUID]; exists {
+ t.Errorf("Generated UUID is duplicated: %s",
newUUID)
+ }
+ uuids[newUUID] = struct{}{}
+ mu.Unlock()
+ }()
+ }
+
+ wg.Wait()
+}
+
+func TestIsRecoveryQueue(t *testing.T) {
+ tests := []struct {
+ name string
+ queueName string
+ expected bool
+ }{
+ {
+ name: "lowercase",
+ queueName: "root.@recovery@",
+ expected: true,
+ },
+ {
+ name: "uppercase",
+ queueName: "ROOT.@RECOVERY@",
+ expected: true,
+ },
+ {
+ name: "both lowercase and uppercase",
+ queueName: "RoOT.@rECoVeRY@",
+ expected: true,
+ },
+ {
+ name: "non-recovery queue",
+ queueName: "otherQueue",
+ expected: false,
+ },
+ {
+ name: "empty string",
+ queueName: "",
+ expected: false,
+ },
+ }
+
Review Comment:
Convert this to a series of checks. That's much more readable:
```
assert.Assert(t, IsRecoveryQueue("root.@recovery@")
assert.Assert(t, IsRecoveryQueue("ROOT.@ReCovery@")
assert.Assert(t, !IsRecoveryQueue("non-recovery")
...
```
##########
pkg/common/utils_test.go:
##########
@@ -380,3 +383,127 @@ func TestGetConfigurationInt(t *testing.T) {
})
}
}
+
+func TestZeroTimeInUnixNano(t *testing.T) {
+ tests := []struct {
+ name string
+ input time.Time
+ expected *int64
+ }{
+ {
+ name: "zero time",
+ input: time.Time{},
+ expected: nil,
+ },
+ {
+ name: "non-zero time",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.UTC),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.UTC).UnixNano()
+ return &tInt
+ }(),
+ },
+ {
+ name: "time in different timezone",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.FixedZone("UTC+8", 8*60*60)),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.FixedZone("UTC+8", 8*60*60)).UnixNano()
+ return &tInt
+ }(),
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := ZeroTimeInUnixNano(tc.input)
+ if tc.expected == nil {
+ assert.Equal(t, tc.expected, result)
+ } else {
+ assert.Equal(t, *tc.expected, *result)
+ }
+ })
+ }
+}
+
+func TestSynchronizeGetNewUUID(t *testing.T) {
+ const numUUIDs = 1000
+ uuids := make(map[string]struct{}, numUUIDs)
+
+ for i := 0; i < numUUIDs; i++ {
+ newUUID := GetNewUUID()
+ if _, err := uuid.Parse(newUUID); err != nil {
+ t.Errorf("Generated UUID is not valid: %s", newUUID)
+ }
+ if _, exists := uuids[newUUID]; exists {
+ t.Errorf("Generated UUID is duplicated: %s", newUUID)
+ }
+ uuids[newUUID] = struct{}{}
+ }
+}
+
+func TestAsynchronousGetNewUUID(t *testing.T) {
+ const numUUIDs = 1000
+ uuids := make(map[string]struct{}, numUUIDs)
+ var mu sync.Mutex
+ var wg sync.WaitGroup
+
+ for i := 0; i < numUUIDs; i++ {
+ wg.Add(1)
+ go func() {
+ defer wg.Done()
+ newUUID := GetNewUUID()
+ if _, err := uuid.Parse(newUUID); err != nil {
+ t.Errorf("Generated UUID is not valid: %s",
newUUID)
+ }
+ mu.Lock()
+ if _, exists := uuids[newUUID]; exists {
+ t.Errorf("Generated UUID is duplicated: %s",
newUUID)
+ }
+ uuids[newUUID] = struct{}{}
+ mu.Unlock()
+ }()
+ }
+
+ wg.Wait()
+}
Review Comment:
This is completely unnecessary. Just create a very simply test which calls
`GetNewUUID()` and checks that it's a valid UUID. No need to overcomplicate.
##########
pkg/common/utils_test.go:
##########
@@ -380,3 +383,127 @@ func TestGetConfigurationInt(t *testing.T) {
})
}
}
+
+func TestZeroTimeInUnixNano(t *testing.T) {
+ tests := []struct {
+ name string
+ input time.Time
+ expected *int64
+ }{
+ {
+ name: "zero time",
+ input: time.Time{},
+ expected: nil,
+ },
+ {
+ name: "non-zero time",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.UTC),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.UTC).UnixNano()
+ return &tInt
+ }(),
+ },
+ {
+ name: "time in different timezone",
+ input: time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.FixedZone("UTC+8", 8*60*60)),
+ expected: func() *int64 {
+ tInt := time.Date(2024, time.June, 6, 0, 0, 0,
0, time.FixedZone("UTC+8", 8*60*60)).UnixNano()
+ return &tInt
+ }(),
+ },
+ }
+
+ for _, tc := range tests {
+ t.Run(tc.name, func(t *testing.T) {
+ result := ZeroTimeInUnixNano(tc.input)
+ if tc.expected == nil {
+ assert.Equal(t, tc.expected, result)
+ } else {
+ assert.Equal(t, *tc.expected, *result)
+ }
+ })
+ }
Review Comment:
Convert this to a series of checks. We have 3 cases and the readability is
much better with this approach:
```
assert.Equal(t, ZeroTimeInUnixNano(time.Date(2024, time.June, 6, 0, 0, 0, 0,
time.UTC)), time.Date(2024, time.June, 6, 0, 0, 0, 0, time.UTC).UnixNano())
...
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]