Skip to content
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

Remove mu Lock from WaitTimeOut #383

Merged
merged 1 commit into from Nov 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 0 additions & 3 deletions token.go
Expand Up @@ -64,9 +64,6 @@ func (b *baseToken) Wait() bool {
// returns false if the timeout occurred. In the case of a timeout the Token
// does not have an error set in case the caller wishes to wait again
func (b *baseToken) WaitTimeout(d time.Duration) bool {
b.m.Lock()
defer b.m.Unlock()

timer := time.NewTimer(d)
select {
case <-b.complete:
Expand Down
12 changes: 12 additions & 0 deletions token_test.go
@@ -1,6 +1,7 @@
package mqtt

import (
"errors"
"testing"
"time"
)
Expand All @@ -11,4 +12,15 @@ func TestWaitTimeout(t *testing.T) {
if b.WaitTimeout(time.Second) {
t.Fatal("Should have failed")
}

// Now lets confirm that WaitTimeout returns
// setError() grabs the mutex which previously caused issues
// when there is a result (it returns true in this case)
b = baseToken{complete: make(chan struct{})}
go func(bt *baseToken) {
bt.setError(errors.New("test error"))
}(&b)
if !b.WaitTimeout(5 * time.Second) {
t.Fatal("Should have succeeded")
}
}