...

Source file src/oss.terrastruct.com/util-go/xcontext/xcontext_test.go

Documentation: oss.terrastruct.com/util-go/xcontext

     1  package xcontext_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"oss.terrastruct.com/util-go/assert"
     8  	"oss.terrastruct.com/util-go/xcontext"
     9  )
    10  
    11  func TestWithoutCancel(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	s := "meow"
    15  
    16  	ctx := context.Background()
    17  	ctx = stringWith(ctx, s)
    18  	assert.Success(t, ctx.Err())
    19  
    20  	t.Run("no_cancel", func(t *testing.T) {
    21  		t.Parallel()
    22  
    23  		ctx := xcontext.WithoutCancel(ctx)
    24  		assert.Success(t, ctx.Err())
    25  
    26  		s2 := stringFrom(ctx)
    27  		assert.String(t, s, s2)
    28  	})
    29  
    30  	t.Run("cancel_before", func(t *testing.T) {
    31  		t.Parallel()
    32  
    33  		ctx, cancel := context.WithCancel(ctx)
    34  		cancel()
    35  
    36  		assert.Error(t, ctx.Err())
    37  
    38  		ctx = xcontext.WithoutCancel(ctx)
    39  		assert.Success(t, ctx.Err())
    40  
    41  		s2 := stringFrom(ctx)
    42  		assert.String(t, s, s2)
    43  	})
    44  
    45  	t.Run("cancel_after", func(t *testing.T) {
    46  		t.Parallel()
    47  
    48  		ctx, cancel := context.WithCancel(ctx)
    49  		ctx = xcontext.WithoutCancel(ctx)
    50  		cancel()
    51  		assert.Success(t, ctx.Err())
    52  
    53  		s2 := stringFrom(ctx)
    54  		assert.String(t, s, s2)
    55  	})
    56  }
    57  
    58  func TestWithoutValues(t *testing.T) {
    59  	t.Parallel()
    60  
    61  	ctx := context.Background()
    62  
    63  	const k = "Death is nature's way of saying `Howdy'."
    64  	const exp = "Proposed Additions to the PDP-11 Instruction Set"
    65  	const exp2 = "character density, n.:"
    66  
    67  	t.Run("no_value", func(t *testing.T) {
    68  		t.Parallel()
    69  
    70  		v := ctx.Value(k)
    71  		assert.JSON(t, nil, v)
    72  
    73  		ctx := xcontext.WithoutValues(ctx)
    74  
    75  		v = ctx.Value(k)
    76  		assert.JSON(t, nil, v)
    77  	})
    78  
    79  	t.Run("with_value", func(t *testing.T) {
    80  		t.Parallel()
    81  
    82  		ctxv := context.WithValue(ctx, k, exp)
    83  		ctx := xcontext.WithoutValues(ctxv)
    84  
    85  		// ctxv contains k but ctx doesn't.
    86  		v := ctxv.Value(k)
    87  		assert.JSON(t, exp, v)
    88  
    89  		v = ctx.Value(k)
    90  		assert.JSON(t, nil, v)
    91  
    92  		ctx = context.WithValue(ctx, k, exp2)
    93  		v = ctx.Value(k)
    94  		assert.JSON(t, exp2, v)
    95  	})
    96  
    97  	t.Run("cancel", func(t *testing.T) {
    98  		t.Parallel()
    99  
   100  		t.Run("before", func(t *testing.T) {
   101  			t.Parallel()
   102  
   103  			ctx, cancel := context.WithCancel(ctx)
   104  			cancel()
   105  
   106  			ctx = xcontext.WithoutValues(ctx)
   107  			assert.Error(t, ctx.Err())
   108  		})
   109  
   110  		t.Run("after", func(t *testing.T) {
   111  			t.Parallel()
   112  
   113  			ctx, cancel := context.WithCancel(ctx)
   114  			defer cancel()
   115  
   116  			ctx = xcontext.WithoutValues(ctx)
   117  			assert.Success(t, ctx.Err())
   118  
   119  			cancel()
   120  			assert.Error(t, ctx.Err())
   121  		})
   122  	})
   123  }
   124  
   125  type stringKey struct{}
   126  
   127  func stringFrom(ctx context.Context) string {
   128  	return ctx.Value(stringKey{}).(string)
   129  }
   130  
   131  func stringWith(ctx context.Context, s string) context.Context {
   132  	return context.WithValue(ctx, stringKey{}, s)
   133  }
   134  

View as plain text