github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/common/common_test.go (about)

     1  // Copyright 2019 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 common
    15  
    16  import (
    17  	"net"
    18  	"path"
    19  	"testing"
    20  
    21  	. "github.com/pingcap/check"
    22  )
    23  
    24  func TestCommon(t *testing.T) {
    25  	TestingT(t)
    26  }
    27  
    28  type testCommon struct{}
    29  
    30  var _ = Suite(&testCommon{})
    31  
    32  func (t *testCommon) TestKeyAdapter(c *C) {
    33  	testCases := []struct {
    34  		keys    []string
    35  		adapter KeyAdapter
    36  		want    string
    37  	}{
    38  		{
    39  			keys:    []string{"127.0.0.1:2382"},
    40  			adapter: WorkerRegisterKeyAdapter,
    41  			want:    "/dm-worker/r/3132372e302e302e313a32333832",
    42  		},
    43  		{
    44  			keys:    []string{"worker1"},
    45  			adapter: WorkerKeepAliveKeyAdapter,
    46  			want:    "/dm-worker/a/776f726b657231",
    47  		},
    48  		{
    49  			keys:    []string{"mysql1"},
    50  			adapter: UpstreamConfigKeyAdapter,
    51  			want:    "/dm-master/v2/upstream/config/6d7973716c31",
    52  		},
    53  		{
    54  			keys:    []string{"127.0.0.1:2382"},
    55  			adapter: UpstreamBoundWorkerKeyAdapter,
    56  			want:    "/dm-master/bound-worker/3132372e302e302e313a32333832",
    57  		},
    58  		{
    59  			keys:    []string{"mysql1", "test"},
    60  			adapter: UpstreamSubTaskKeyAdapter,
    61  			want:    "/dm-master/upstream/subtask/6d7973716c31/74657374",
    62  		},
    63  		{
    64  			keys:    []string{"test", "mysql_replica_01", "target_db", "target_table"},
    65  			adapter: ShardDDLOptimismInfoKeyAdapter,
    66  			want:    "/dm-master/shardddl-optimism/info/74657374/6d7973716c5f7265706c6963615f3031/7461726765745f6462/7461726765745f7461626c65",
    67  		},
    68  		{
    69  			keys:    []string{"mysql/01"},
    70  			adapter: StageRelayKeyAdapter,
    71  			want:    "/dm-master/v2/stage/relay/6d7973716c2f3031",
    72  		},
    73  		{
    74  			keys:    []string{"mysql1", "中文1🀄️"},
    75  			adapter: UpstreamSubTaskKeyAdapter,
    76  			want:    "/dm-master/upstream/subtask/6d7973716c31/e4b8ade6968731f09f8084efb88f",
    77  		},
    78  		{
    79  			keys:    []string{"task-1"},
    80  			adapter: OpenAPITaskTemplateKeyAdapter,
    81  			want:    "/dm-master/openapi-task-template/7461736b2d31",
    82  		},
    83  	}
    84  
    85  	for _, ca := range testCases {
    86  		encKey := ca.adapter.Encode(ca.keys...)
    87  		c.Assert(encKey, Equals, ca.want)
    88  		decKey, err := ca.adapter.Decode(encKey)
    89  		c.Assert(err, IsNil)
    90  		c.Assert(decKey, DeepEquals, ca.keys)
    91  	}
    92  }
    93  
    94  func (t *testCommon) TestEncodeAsPrefix(c *C) {
    95  	testCases := []struct {
    96  		keys    []string
    97  		adapter KeyAdapter
    98  		want    string
    99  	}{
   100  		{
   101  			keys:    []string{"mysql1"},
   102  			adapter: UpstreamSubTaskKeyAdapter,
   103  			want:    "/dm-master/upstream/subtask/6d7973716c31/",
   104  		},
   105  	}
   106  
   107  	for _, ca := range testCases {
   108  		encKey := ca.adapter.Encode(ca.keys...)
   109  		c.Assert(encKey, Equals, ca.want)
   110  		_, err := ca.adapter.Decode(encKey)
   111  		c.Assert(err, NotNil)
   112  	}
   113  }
   114  
   115  func (t *testCommon) TestIsErrNetClosing(c *C) {
   116  	server, err := net.Listen("tcp", "localhost:0")
   117  	c.Assert(err, IsNil)
   118  	err = server.Close()
   119  	c.Assert(IsErrNetClosing(err), IsFalse)
   120  	_, err = server.Accept()
   121  	c.Assert(IsErrNetClosing(err), IsTrue)
   122  }
   123  
   124  func (t *testCommon) TestJoinUseSlash(c *C) {
   125  	// because we use "/" in Encode
   126  	c.Assert(path.Join("a", "b"), Equals, "a/b")
   127  }