github.com/aavshr/aws-sdk-go@v1.41.3/aws/request/request_context_test.go (about)

     1  package request_test
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/aavshr/aws-sdk-go/aws/corehandlers"
     9  	"github.com/aavshr/aws-sdk-go/aws/request"
    10  	"github.com/aavshr/aws-sdk-go/awstesting"
    11  )
    12  
    13  func TestRequest_SetContext(t *testing.T) {
    14  	svc := awstesting.NewClient()
    15  	svc.Handlers.Clear()
    16  	svc.Handlers.Send.PushBackNamed(corehandlers.SendHandler)
    17  
    18  	r := svc.NewRequest(&request.Operation{Name: "Operation"}, nil, nil)
    19  	ctx := &awstesting.FakeContext{DoneCh: make(chan struct{})}
    20  	r.SetContext(ctx)
    21  
    22  	ctx.Error = fmt.Errorf("context canceled")
    23  	close(ctx.DoneCh)
    24  
    25  	err := r.Send()
    26  	if err == nil {
    27  		t.Fatalf("expected error, got none")
    28  	}
    29  
    30  	// Only check against canceled because go 1.6 will not use the context's
    31  	// Err().
    32  	if e, a := "canceled", err.Error(); !strings.Contains(a, e) {
    33  		t.Errorf("expect %q to be in %q, but was not", e, a)
    34  	}
    35  }
    36  
    37  func TestRequest_SetContextPanic(t *testing.T) {
    38  	defer func() {
    39  		if r := recover(); r == nil {
    40  			t.Fatalf("expect SetContext to panic, did not")
    41  		}
    42  	}()
    43  	r := &request.Request{}
    44  
    45  	r.SetContext(nil)
    46  }