github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/config/configlist_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 config_test
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	. "github.com/pingcap/check"
    21  
    22  	"github.com/pingcap/br/pkg/lightning/config"
    23  )
    24  
    25  var _ = Suite(&configListTestSuite{})
    26  
    27  type configListTestSuite struct{}
    28  
    29  func (s *configListTestSuite) TestNormalPushPop(c *C) {
    30  	cl := config.NewConfigList()
    31  
    32  	cl.Push(&config.Config{TikvImporter: config.TikvImporter{Addr: "1.1.1.1:1111"}})
    33  	cl.Push(&config.Config{TikvImporter: config.TikvImporter{Addr: "2.2.2.2:2222"}})
    34  
    35  	startTime := time.Now()
    36  	cfg, err := cl.Pop(context.Background()) // these two should never block.
    37  	c.Assert(time.Since(startTime), Less, 100*time.Millisecond)
    38  	c.Assert(err, IsNil)
    39  	c.Assert(cfg.TikvImporter.Addr, Equals, "1.1.1.1:1111")
    40  
    41  	startTime = time.Now()
    42  	cfg, err = cl.Pop(context.Background())
    43  	c.Assert(time.Since(startTime), Less, 100*time.Millisecond)
    44  	c.Assert(err, IsNil)
    45  	c.Assert(cfg.TikvImporter.Addr, Equals, "2.2.2.2:2222")
    46  
    47  	startTime = time.Now()
    48  
    49  	go func() {
    50  		time.Sleep(400 * time.Millisecond)
    51  		cl.Push(&config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}})
    52  	}()
    53  
    54  	cfg, err = cl.Pop(context.Background()) // this should block for ≥400ms
    55  	c.Assert(time.Since(startTime), GreaterEqual, 400*time.Millisecond)
    56  	c.Assert(err, IsNil)
    57  	c.Assert(cfg.TikvImporter.Addr, Equals, "3.3.3.3:3333")
    58  }
    59  
    60  func (s *configListTestSuite) TestContextCancel(c *C) {
    61  	ctx, cancel := context.WithCancel(context.Background())
    62  	cl := config.NewConfigList()
    63  
    64  	go func() {
    65  		time.Sleep(400 * time.Millisecond)
    66  		cancel()
    67  	}()
    68  
    69  	startTime := time.Now()
    70  	_, err := cl.Pop(ctx)
    71  	c.Assert(time.Since(startTime), GreaterEqual, 400*time.Millisecond)
    72  	c.Assert(err, Equals, context.Canceled)
    73  }
    74  
    75  func (s *configListTestSuite) TestGetRemove(c *C) {
    76  	cl := config.NewConfigList()
    77  
    78  	cfg1 := &config.Config{TikvImporter: config.TikvImporter{Addr: "1.1.1.1:1111"}}
    79  	cl.Push(cfg1)
    80  	cfg2 := &config.Config{TikvImporter: config.TikvImporter{Addr: "2.2.2.2:2222"}}
    81  	cl.Push(cfg2)
    82  	cfg3 := &config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}}
    83  	cl.Push(cfg3)
    84  
    85  	cfg, ok := cl.Get(cfg2.TaskID)
    86  	c.Assert(ok, IsTrue)
    87  	c.Assert(cfg, Equals, cfg2)
    88  	_, ok = cl.Get(cfg3.TaskID + 1000)
    89  	c.Assert(ok, IsFalse)
    90  
    91  	ok = cl.Remove(cfg2.TaskID)
    92  	c.Assert(ok, IsTrue)
    93  	ok = cl.Remove(cfg3.TaskID + 1000)
    94  	c.Assert(ok, IsFalse)
    95  	_, ok = cl.Get(cfg2.TaskID)
    96  	c.Assert(ok, IsFalse)
    97  
    98  	var err error
    99  	cfg, err = cl.Pop(context.Background())
   100  	c.Assert(err, IsNil)
   101  	c.Assert(cfg, Equals, cfg1)
   102  
   103  	cfg, err = cl.Pop(context.Background())
   104  	c.Assert(err, IsNil)
   105  	c.Assert(cfg, Equals, cfg3)
   106  }
   107  
   108  func (s *configListTestSuite) TestMoveFrontBack(c *C) {
   109  	cl := config.NewConfigList()
   110  
   111  	cfg1 := &config.Config{TikvImporter: config.TikvImporter{Addr: "1.1.1.1:1111"}}
   112  	cl.Push(cfg1)
   113  	cfg2 := &config.Config{TikvImporter: config.TikvImporter{Addr: "2.2.2.2:2222"}}
   114  	cl.Push(cfg2)
   115  	cfg3 := &config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}}
   116  	cl.Push(cfg3)
   117  
   118  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg2.TaskID, cfg3.TaskID})
   119  
   120  	c.Assert(cl.MoveToFront(cfg2.TaskID), IsTrue)
   121  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID})
   122  	c.Assert(cl.MoveToFront(cfg2.TaskID), IsTrue)
   123  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID})
   124  	c.Assert(cl.MoveToFront(123456), IsFalse)
   125  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID})
   126  
   127  	c.Assert(cl.MoveToBack(cfg2.TaskID), IsTrue)
   128  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID})
   129  	c.Assert(cl.MoveToBack(cfg2.TaskID), IsTrue)
   130  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID})
   131  	c.Assert(cl.MoveToBack(123456), IsFalse)
   132  	c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID})
   133  }