Skip to content

Commit

Permalink
Merge pull request #383 from ChIoT-Tech/baseTokenRemoveLock
Browse files Browse the repository at this point in the history
Remove mu Lock from WaitTimeOut
  • Loading branch information
Al S-M committed Nov 15, 2019
2 parents 825e8b3 + a75ba2d commit 939f516
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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")
}
}

0 comments on commit 939f516

Please sign in to comment.