github.com/bazelbuild/remote-apis-sdks@v0.0.0-20240425170053-8a36686a6350/go/pkg/cas/client_test.go (about)

     1  package cas
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestPerCallTimeout(t *testing.T) {
    10  	t.Parallel()
    11  
    12  	ctx := context.Background()
    13  	ctx, cancel, withTimeout := withPerCallTimeout(ctx, time.Millisecond)
    14  	defer cancel()
    15  
    16  	t.Run("succeeded", func(t *testing.T) {
    17  		withTimeout(func() {})
    18  		if ctx.Err() != nil {
    19  			t.Fatalf("want nil, got %s", ctx.Err())
    20  		}
    21  	})
    22  
    23  	t.Run("canceled", func(t *testing.T) {
    24  		withTimeout(func() {
    25  			select {
    26  			case <-ctx.Done():
    27  			case <-time.After(time.Second):
    28  			}
    29  		})
    30  
    31  		if ctx.Err() != context.Canceled {
    32  			t.Fatalf("want %s, got %s", context.Canceled, ctx.Err())
    33  		}
    34  	})
    35  }