github.com/kaydxh/golang@v0.0.131/go/context/context_test.go (about)

     1  /*
     2   *Copyright (c) 2022, kaydxh
     3   *
     4   *Permission is hereby granted, free of charge, to any person obtaining a copy
     5   *of this software and associated documentation files (the "Software"), to deal
     6   *in the Software without restriction, including without limitation the rights
     7   *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     8   *copies of the Software, and to permit persons to whom the Software is
     9   *furnished to do so, subject to the following conditions:
    10   *
    11   *The above copyright notice and this permission notice shall be included in all
    12   *copies or substantial portions of the Software.
    13   *
    14   *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    15   *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    16   *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    17   *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    18   *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    19   *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    20   *SOFTWARE.
    21   */
    22  package context_test
    23  
    24  import (
    25  	"context"
    26  	"fmt"
    27  	"testing"
    28  	"time"
    29  
    30  	context_ "github.com/kaydxh/golang/go/context"
    31  )
    32  
    33  func withField(ctx context.Context) {
    34  	ctx = context.WithValue(ctx, "abc", "abc")
    35  	fmt.Printf("context: %+v\n", ctx)
    36  }
    37  
    38  func TestContext(t *testing.T) {
    39  	ctx := context.Background()
    40  	t.Logf("context: %v", ctx)
    41  	withField(ctx)
    42  	t.Logf("context: %v", ctx)
    43  }
    44  
    45  func doA(ctx context.Context) {
    46  
    47  	{
    48  		var cancel context.CancelFunc
    49  		ctx, cancel = context.WithTimeout(ctx, time.Second)
    50  		defer cancel()
    51  	}
    52  
    53  	timer := time.NewTimer(3 * time.Second)
    54  	defer timer.Stop()
    55  
    56  	select {
    57  	case <-ctx.Done():
    58  		fmt.Println("doA timeout")
    59  
    60  	case <-timer.C:
    61  		fmt.Println("doA finish")
    62  	}
    63  }
    64  
    65  func doB(ctx context.Context) {
    66  
    67  	{
    68  		var cancel context.CancelFunc
    69  		ctx, cancel = context.WithTimeout(ctx, 10*time.Second)
    70  		defer cancel()
    71  	}
    72  
    73  	timer := time.NewTimer(3 * time.Second)
    74  	defer timer.Stop()
    75  
    76  	select {
    77  	case <-ctx.Done():
    78  		fmt.Println("doB timeout")
    79  
    80  	case <-timer.C:
    81  		fmt.Println("doB finish")
    82  	}
    83  }
    84  
    85  func TestContextTimeout(t *testing.T) {
    86  	ctx := context.Background()
    87  	doA(ctx)
    88  	doB(ctx)
    89  }
    90  
    91  func TestExtractIntegerFromContext(t *testing.T) {
    92  	ctx := context.Background()
    93  
    94  	testCases := []struct {
    95  		key      string
    96  		value    string
    97  		expected string
    98  	}{
    99  		{
   100  			key:   "test-1",
   101  			value: "123",
   102  		},
   103  		{
   104  			key:   "test-2",
   105  			value: "test-123",
   106  		},
   107  	}
   108  
   109  	for _, testCase := range testCases {
   110  		t.Run(testCase.key, func(t *testing.T) {
   111  			ctx = context_.SetPairContext(ctx, testCase.key, testCase.value)
   112  
   113  			number, err := context_.ExtractIntegerFromContext(ctx, testCase.key)
   114  			if err != nil {
   115  				t.Errorf("expect nil, got %v", err)
   116  				return
   117  			}
   118  			t.Logf("extract value %v by key %v ", number, testCase.key)
   119  
   120  		})
   121  	}
   122  
   123  }
   124  
   125  func TestExtractStringFromContext(t *testing.T) {
   126  	ctx := context.Background()
   127  
   128  	testCases := []struct {
   129  		key      string
   130  		value    string
   131  		expected string
   132  	}{
   133  		{
   134  			key:   "test-1",
   135  			value: "123",
   136  		},
   137  		{
   138  			key:   "test-2",
   139  			value: "test-123",
   140  		},
   141  	}
   142  
   143  	for _, testCase := range testCases {
   144  		t.Run(testCase.key, func(t *testing.T) {
   145  			ctx = context_.SetPairContext(ctx, testCase.key, testCase.value)
   146  
   147  			value := context_.ExtractStringFromContext(ctx, testCase.key)
   148  			t.Logf("extract value %v by key %v ", value, testCase.key)
   149  
   150  		})
   151  	}
   152  
   153  }