Skip to content

Commit a3ad4c7

Browse files
committed
add ut
1 parent 22cb998 commit a3ad4c7

File tree

1 file changed

+52
-0
lines changed

1 file changed

+52
-0
lines changed

pkg/util/util_test.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,3 +600,55 @@ users:
600600
}
601601
}
602602
}
603+
604+
func TestWaitUntilTimeout(t *testing.T) {
605+
tests := []struct {
606+
desc string
607+
timeout time.Duration
608+
execFunc ExecFunc
609+
timeoutFunc TimeoutFunc
610+
expectedErr error
611+
}{
612+
{
613+
desc: "execFunc returns error",
614+
timeout: 1 * time.Second,
615+
execFunc: func() error {
616+
return fmt.Errorf("execFunc error")
617+
},
618+
timeoutFunc: func() error {
619+
return fmt.Errorf("timeout error")
620+
},
621+
expectedErr: fmt.Errorf("execFunc error"),
622+
},
623+
{
624+
desc: "execFunc timeout",
625+
timeout: 1 * time.Second,
626+
execFunc: func() error {
627+
time.Sleep(2 * time.Second)
628+
return nil
629+
},
630+
timeoutFunc: func() error {
631+
return fmt.Errorf("timeout error")
632+
},
633+
expectedErr: fmt.Errorf("timeout error"),
634+
},
635+
{
636+
desc: "execFunc completed successfully",
637+
timeout: 1 * time.Second,
638+
execFunc: func() error {
639+
return nil
640+
},
641+
timeoutFunc: func() error {
642+
return fmt.Errorf("timeout error")
643+
},
644+
expectedErr: nil,
645+
},
646+
}
647+
648+
for _, test := range tests {
649+
err := WaitUntilTimeout(test.timeout, test.execFunc, test.timeoutFunc)
650+
if err != nil && (err.Error() != test.expectedErr.Error()) {
651+
t.Errorf("unexpected error: %v, expected error: %v", err, test.expectedErr)
652+
}
653+
}
654+
}

0 commit comments

Comments
 (0)