github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/util/ctx_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package util
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/pingcap/check"
    20  	"github.com/pingcap/ticdc/pkg/util/testleak"
    21  	"github.com/pingcap/tidb/store/mockstore"
    22  	"go.uber.org/zap"
    23  )
    24  
    25  type ctxValueSuite struct{}
    26  
    27  var _ = check.Suite(&ctxValueSuite{})
    28  
    29  func (s *ctxValueSuite) TestShouldReturnCaptureID(c *check.C) {
    30  	defer testleak.AfterTest(c)()
    31  	ctx := PutCaptureAddrInCtx(context.Background(), "ello")
    32  	c.Assert(CaptureAddrFromCtx(ctx), check.Equals, "ello")
    33  }
    34  
    35  func (s *ctxValueSuite) TestCaptureIDNotSet(c *check.C) {
    36  	defer testleak.AfterTest(c)()
    37  	c.Assert(CaptureAddrFromCtx(context.Background()), check.Equals, "")
    38  	captureAddr := CaptureAddrFromCtx(context.Background())
    39  	c.Assert(captureAddr, check.Equals, "")
    40  	ctx := context.WithValue(context.Background(), ctxKeyCaptureAddr, 1321)
    41  	c.Assert(CaptureAddrFromCtx(ctx), check.Equals, "")
    42  }
    43  
    44  func (s *ctxValueSuite) TestShouldReturnChangefeedID(c *check.C) {
    45  	defer testleak.AfterTest(c)()
    46  	ctx := PutChangefeedIDInCtx(context.Background(), "ello")
    47  	c.Assert(ChangefeedIDFromCtx(ctx), check.Equals, "ello")
    48  }
    49  
    50  func (s *ctxValueSuite) TestCanceledContext(c *check.C) {
    51  	defer testleak.AfterTest(c)()
    52  	ctx := PutChangefeedIDInCtx(context.Background(), "test-cf")
    53  	c.Assert(ChangefeedIDFromCtx(ctx), check.Equals, "test-cf")
    54  	ctx, cancel := context.WithCancel(ctx)
    55  	cancel()
    56  	c.Assert(ChangefeedIDFromCtx(ctx), check.Equals, "test-cf")
    57  }
    58  
    59  func (s *ctxValueSuite) TestChangefeedIDNotSet(c *check.C) {
    60  	defer testleak.AfterTest(c)()
    61  	c.Assert(ChangefeedIDFromCtx(context.Background()), check.Equals, "")
    62  	changefeedID := ChangefeedIDFromCtx(context.Background())
    63  	c.Assert(changefeedID, check.Equals, "")
    64  	ctx := context.WithValue(context.Background(), ctxKeyChangefeedID, 1321)
    65  	changefeedID = ChangefeedIDFromCtx(ctx)
    66  	c.Assert(changefeedID, check.Equals, "")
    67  }
    68  
    69  func (s *ctxValueSuite) TestShouldReturnTimezone(c *check.C) {
    70  	defer testleak.AfterTest(c)()
    71  	tz, _ := getTimezoneFromZonefile("UTC")
    72  	ctx := PutTimezoneInCtx(context.Background(), tz)
    73  	tz = TimezoneFromCtx(ctx)
    74  	c.Assert(tz.String(), check.Equals, "UTC")
    75  }
    76  
    77  func (s *ctxValueSuite) TestTimezoneNotSet(c *check.C) {
    78  	defer testleak.AfterTest(c)()
    79  	tz := TimezoneFromCtx(context.Background())
    80  	c.Assert(tz, check.IsNil)
    81  	ctx := context.WithValue(context.Background(), ctxKeyTimezone, 1321)
    82  	tz = TimezoneFromCtx(ctx)
    83  	c.Assert(tz, check.IsNil)
    84  }
    85  
    86  func (s *ctxValueSuite) TestShouldReturnTableInfo(c *check.C) {
    87  	defer testleak.AfterTest(c)()
    88  	ctx := PutTableInfoInCtx(context.Background(), 1321, "ello")
    89  	tableID, tableName := TableIDFromCtx(ctx)
    90  	c.Assert(tableID, check.Equals, int64(1321))
    91  	c.Assert(tableName, check.Equals, "ello")
    92  }
    93  
    94  func (s *ctxValueSuite) TestTableInfoNotSet(c *check.C) {
    95  	defer testleak.AfterTest(c)()
    96  	tableID, tableName := TableIDFromCtx(context.Background())
    97  	c.Assert(tableID, check.Equals, int64(0))
    98  	c.Assert(tableName, check.Equals, "")
    99  	ctx := context.WithValue(context.Background(), ctxKeyTableID, 1321)
   100  	tableID, tableName = TableIDFromCtx(ctx)
   101  	c.Assert(tableID, check.Equals, int64(0))
   102  	c.Assert(tableName, check.Equals, "")
   103  }
   104  
   105  func (s *ctxValueSuite) TestShouldReturnKVStorage(c *check.C) {
   106  	defer testleak.AfterTest(c)()
   107  	kvStorage, _ := mockstore.NewMockStore()
   108  	defer kvStorage.Close()
   109  	ctx := PutKVStorageInCtx(context.Background(), kvStorage)
   110  	kvStorage2, err := KVStorageFromCtx(ctx)
   111  	c.Assert(kvStorage2, check.DeepEquals, kvStorage)
   112  	c.Assert(err, check.IsNil)
   113  }
   114  
   115  func (s *ctxValueSuite) TestKVStorageNotSet(c *check.C) {
   116  	defer testleak.AfterTest(c)()
   117  	// Context not set value
   118  	kvStorage, err := KVStorageFromCtx(context.Background())
   119  	c.Assert(kvStorage, check.IsNil)
   120  	c.Assert(err, check.NotNil)
   121  	// Type of value is not kv.Storage
   122  	ctx := context.WithValue(context.Background(), ctxKeyKVStorage, 1321)
   123  	kvStorage, err = KVStorageFromCtx(ctx)
   124  	c.Assert(kvStorage, check.IsNil)
   125  	c.Assert(err, check.NotNil)
   126  }
   127  
   128  func (s *ctxValueSuite) TestZapFieldWithContext(c *check.C) {
   129  	defer testleak.AfterTest(c)()
   130  	var (
   131  		capture    string = "127.0.0.1:8200"
   132  		changefeed string = "test-cf"
   133  	)
   134  	ctx := context.Background()
   135  	ctx = PutCaptureAddrInCtx(ctx, capture)
   136  	ctx = PutChangefeedIDInCtx(ctx, changefeed)
   137  	c.Assert(ZapFieldCapture(ctx), check.DeepEquals, zap.String("capture", capture))
   138  	c.Assert(ZapFieldChangefeed(ctx), check.DeepEquals, zap.String("changefeed", changefeed))
   139  }