-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #862 from umagnus/use_util_wait_func
fix: mount stuck issue
- Loading branch information
Showing
3 changed files
with
128 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// ExecFunc returns a exec function's output and error | ||
type ExecFunc func() (err error) | ||
|
||
// TimeoutFunc returns output and error if an ExecFunc timeout | ||
type TimeoutFunc func() (err error) | ||
|
||
// WaitUntilTimeout waits for the exec function to complete or return timeout error | ||
func WaitUntilTimeout(timeout time.Duration, execFunc ExecFunc, timeoutFunc TimeoutFunc) error { | ||
// Create a channel to receive the result of the exec function | ||
done := make(chan bool) | ||
var err error | ||
|
||
// Start the exec function in a goroutine | ||
go func() { | ||
err = execFunc() | ||
done <- true | ||
}() | ||
|
||
// Wait for the function to complete or time out | ||
select { | ||
case <-done: | ||
return err | ||
case <-time.After(timeout): | ||
return timeoutFunc() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
Copyright 2019 The Kubernetes Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func TestWaitUntilTimeout(t *testing.T) { | ||
tests := []struct { | ||
desc string | ||
timeout time.Duration | ||
execFunc ExecFunc | ||
timeoutFunc TimeoutFunc | ||
expectedErr error | ||
}{ | ||
{ | ||
desc: "execFunc returns error", | ||
timeout: 1 * time.Second, | ||
execFunc: func() error { | ||
return fmt.Errorf("execFunc error") | ||
}, | ||
timeoutFunc: func() error { | ||
return fmt.Errorf("timeout error") | ||
}, | ||
expectedErr: fmt.Errorf("execFunc error"), | ||
}, | ||
{ | ||
desc: "execFunc timeout", | ||
timeout: 1 * time.Second, | ||
execFunc: func() error { | ||
time.Sleep(2 * time.Second) | ||
return nil | ||
}, | ||
timeoutFunc: func() error { | ||
return fmt.Errorf("timeout error") | ||
}, | ||
expectedErr: fmt.Errorf("timeout error"), | ||
}, | ||
{ | ||
desc: "execFunc completed successfully", | ||
timeout: 1 * time.Second, | ||
execFunc: func() error { | ||
return nil | ||
}, | ||
timeoutFunc: func() error { | ||
return fmt.Errorf("timeout error") | ||
}, | ||
expectedErr: nil, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
err := WaitUntilTimeout(test.timeout, test.execFunc, test.timeoutFunc) | ||
if err != nil && (err.Error() != test.expectedErr.Error()) { | ||
t.Errorf("unexpected error: %v, expected error: %v", err, test.expectedErr) | ||
} | ||
} | ||
} |