-
Notifications
You must be signed in to change notification settings - Fork 950
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
enhance: add retry helper method for Trylock #2319
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,54 @@ | ||
package ctrd | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_containerLock_TrylockWithRetry(t *testing.T) { | ||
l := &containerLock{ | ||
ids: make(map[string]struct{}), | ||
} | ||
|
||
// basically, if the releaseTimeout < the tryLockTimeout, | ||
// TrylockWithTimeout will lock successfully. If not, it will fail. | ||
runTrylockWithT := func(tryLockTimeout, releaseTimeout time.Duration) bool { | ||
id := "c" | ||
assert.Equal(t, l.Trylock(id), true) | ||
defer l.Unlock(id) | ||
|
||
var ( | ||
releaseCh = make(chan struct{}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the usage of this variable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
waitCh = make(chan bool) | ||
res bool | ||
) | ||
|
||
go func() { | ||
close(releaseCh) | ||
ctx, cancel := context.WithTimeout(context.TODO(), tryLockTimeout) | ||
defer cancel() | ||
waitCh <- l.TrylockWithRetry(ctx, id) | ||
}() | ||
|
||
<-releaseCh | ||
time.Sleep(releaseTimeout) | ||
l.Unlock(id) | ||
|
||
select { | ||
case <-time.After(3 * time.Second): | ||
t.Fatalf("timeout to get the Trylock result") | ||
case res = <-waitCh: | ||
} | ||
return res | ||
} | ||
|
||
assert.Equal(t, true, runTrylockWithT(5*time.Second, 200*time.Millisecond)) | ||
assert.Equal(t, false, runTrylockWithT(200*time.Millisecond, 500*time.Millisecond)) | ||
} | ||
|
||
func Test_containerLock_Trylock(t *testing.T) { | ||
l := &containerLock{ | ||
ids: make(map[string]struct{}), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This part would return an error of
While I think some users of PouchContainer feel a little bit unclear about the error message, could we add code like
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the error will be disappeared after this PR 😢