github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/util/do_test.go (about)

     1  package util
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/ipfs/go-ipfs/Godeps/_workspace/src/golang.org/x/net/context"
     8  )
     9  
    10  func TestDoReturnsContextErr(t *testing.T) {
    11  	ctx, cancel := context.WithCancel(context.Background())
    12  	ch := make(chan struct{})
    13  	err := ContextDo(ctx, func() error {
    14  		cancel()
    15  		ch <- struct{}{} // won't return
    16  		return nil
    17  	})
    18  	if err != ctx.Err() {
    19  		t.Fail()
    20  	}
    21  }
    22  
    23  func TestDoReturnsFuncError(t *testing.T) {
    24  	ctx := context.Background()
    25  	expected := errors.New("expected to be returned by ContextDo")
    26  	err := ContextDo(ctx, func() error {
    27  		return expected
    28  	})
    29  	if err != expected {
    30  		t.Fail()
    31  	}
    32  }
    33  
    34  func TestDoReturnsNil(t *testing.T) {
    35  	ctx := context.Background()
    36  	err := ContextDo(ctx, func() error {
    37  		return nil
    38  	})
    39  	if err != nil {
    40  		t.Fail()
    41  	}
    42  }