github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/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/tidb-lightning/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 go func() { 48 time.Sleep(400 * time.Millisecond) 49 cl.Push(&config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}}) 50 }() 51 52 startTime = time.Now() 53 cfg, err = cl.Pop(context.Background()) // this should block for ≥400ms 54 c.Assert(time.Since(startTime), GreaterEqual, 400*time.Millisecond) 55 c.Assert(err, IsNil) 56 c.Assert(cfg.TikvImporter.Addr, Equals, "3.3.3.3:3333") 57 } 58 59 func (s *configListTestSuite) TestContextCancel(c *C) { 60 ctx, cancel := context.WithCancel(context.Background()) 61 cl := config.NewConfigList() 62 63 go func() { 64 time.Sleep(400 * time.Millisecond) 65 cancel() 66 }() 67 68 startTime := time.Now() 69 _, err := cl.Pop(ctx) 70 c.Assert(time.Since(startTime), GreaterEqual, 400*time.Millisecond) 71 c.Assert(err, Equals, context.Canceled) 72 } 73 74 func (s *configListTestSuite) TestGetRemove(c *C) { 75 cl := config.NewConfigList() 76 77 cfg1 := &config.Config{TikvImporter: config.TikvImporter{Addr: "1.1.1.1:1111"}} 78 cl.Push(cfg1) 79 cfg2 := &config.Config{TikvImporter: config.TikvImporter{Addr: "2.2.2.2:2222"}} 80 cl.Push(cfg2) 81 cfg3 := &config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}} 82 cl.Push(cfg3) 83 84 cfg, ok := cl.Get(cfg2.TaskID) 85 c.Assert(ok, IsTrue) 86 c.Assert(cfg, Equals, cfg2) 87 _, ok = cl.Get(cfg3.TaskID + 1000) 88 c.Assert(ok, IsFalse) 89 90 ok = cl.Remove(cfg2.TaskID) 91 c.Assert(ok, IsTrue) 92 ok = cl.Remove(cfg3.TaskID + 1000) 93 c.Assert(ok, IsFalse) 94 _, ok = cl.Get(cfg2.TaskID) 95 c.Assert(ok, IsFalse) 96 97 var err error 98 cfg, err = cl.Pop(context.Background()) 99 c.Assert(err, IsNil) 100 c.Assert(cfg, Equals, cfg1) 101 102 cfg, err = cl.Pop(context.Background()) 103 c.Assert(err, IsNil) 104 c.Assert(cfg, Equals, cfg3) 105 } 106 107 func (s *configListTestSuite) TestMoveFrontBack(c *C) { 108 cl := config.NewConfigList() 109 110 cfg1 := &config.Config{TikvImporter: config.TikvImporter{Addr: "1.1.1.1:1111"}} 111 cl.Push(cfg1) 112 cfg2 := &config.Config{TikvImporter: config.TikvImporter{Addr: "2.2.2.2:2222"}} 113 cl.Push(cfg2) 114 cfg3 := &config.Config{TikvImporter: config.TikvImporter{Addr: "3.3.3.3:3333"}} 115 cl.Push(cfg3) 116 117 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg2.TaskID, cfg3.TaskID}) 118 119 c.Assert(cl.MoveToFront(cfg2.TaskID), IsTrue) 120 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID}) 121 c.Assert(cl.MoveToFront(cfg2.TaskID), IsTrue) 122 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID}) 123 c.Assert(cl.MoveToFront(123456), IsFalse) 124 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg2.TaskID, cfg1.TaskID, cfg3.TaskID}) 125 126 c.Assert(cl.MoveToBack(cfg2.TaskID), IsTrue) 127 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID}) 128 c.Assert(cl.MoveToBack(cfg2.TaskID), IsTrue) 129 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID}) 130 c.Assert(cl.MoveToBack(123456), IsFalse) 131 c.Assert(cl.AllIDs(), DeepEquals, []int64{cfg1.TaskID, cfg3.TaskID, cfg2.TaskID}) 132 }