Skip to content

Commit

Permalink
Rewrite deadlocking test
Browse files Browse the repository at this point in the history
  • Loading branch information
tadovas committed Jul 11, 2018
1 parent 04bd619 commit 495513b
Showing 1 changed file with 28 additions and 13 deletions.
41 changes: 28 additions & 13 deletions utils/cancelable_test.go
Expand Up @@ -20,7 +20,6 @@ package utils
import (
"errors"
"github.com/stretchr/testify/assert"
"sync"
"testing"
"time"
)
Expand All @@ -37,27 +36,43 @@ func TestBlockingFunctionResultIsPropagatedToCaller(t *testing.T) {
}

func TestCleanupFunctionIsCalledWithReturnedValueIfCancelWasCalled(t *testing.T) {
var cleanupVal int
cleanupWaiter := sync.WaitGroup{}
cleanupWaiter.Add(1)
callErrorChan := make(chan error, 1)
cleanupVal := make(chan int, 1)
completeRequest := make(chan bool, 1)

cancelable := NewCancelable()
cancelable.Cancel()

_, err := cancelable.
cancelableRequest := cancelable.
NewRequest(func() (interface{}, error) {
<-completeRequest
return 1, nil
}).
Cleanup(func(val interface{}, err error) {
cleanupVal = val.(int)
cleanupWaiter.Done()
}).
Call()
cleanupVal <- val.(int)
})

go func() {
_, err := cancelableRequest.Call()
callErrorChan <- err
}()

cancelable.Cancel()

cleanupWaiter.Wait()
select {
case err := <-callErrorChan:
assert.Equal(t, ErrRequestCancelled, err)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "cancelable Call expected to return in 100 milliseconds")
}

assert.Equal(t, ErrRequestCancelled, err)
assert.Equal(t, 1, cleanupVal)
completeRequest <- true

select {
case val := <-cleanupVal:
assert.Equal(t, 1, val)
case <-time.After(100 * time.Millisecond):
assert.Fail(t, "cancelable cleanup expected to be called in 100 milliseconds")
}
}

func TestBlockingFunctionIsCancelled(t *testing.T) {
Expand Down

0 comments on commit 495513b

Please sign in to comment.