github.com/billybanfield/evergreen@v0.0.0-20170525200750-eeee692790f7/util/func_test.go (about)

     1  package util
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/pkg/errors"
     8  	. "github.com/smartystreets/goconvey/convey"
     9  )
    10  
    11  func TestRunFunctionWithTimeout(t *testing.T) {
    12  
    13  	Convey("When running a function with a timeout", t, func() {
    14  
    15  		funcDone := false
    16  
    17  		Convey("if the function times out, ErrTimedOut should be"+
    18  			" returned", func() {
    19  			f := func() error {
    20  				time.Sleep(time.Minute)
    21  				funcDone = true
    22  				return nil
    23  			}
    24  			So(RunFunctionWithTimeout(f, time.Millisecond*500), ShouldResemble,
    25  				ErrTimedOut)
    26  			So(funcDone, ShouldBeFalse)
    27  		})
    28  
    29  		Convey("if the inner function returns an error, the error should not"+
    30  			" be swallowed", func() {
    31  			funcErr := errors.New("I'm an error")
    32  			f := func() error {
    33  				return funcErr
    34  			}
    35  			So(RunFunctionWithTimeout(f, time.Minute), ShouldResemble, funcErr)
    36  		})
    37  
    38  		Convey("if the function does not return an error, nil should be"+
    39  			" returned", func() {
    40  			f := func() error {
    41  				funcDone = true
    42  				return nil
    43  			}
    44  			So(RunFunctionWithTimeout(f, time.Minute), ShouldBeNil)
    45  			So(funcDone, ShouldBeTrue)
    46  		})
    47  
    48  	})
    49  
    50  }