github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/worker/worker_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 worker_test
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	. "github.com/pingcap/check"
    21  
    22  	"github.com/pingcap/br/pkg/lightning/worker"
    23  )
    24  
    25  type testWorkerPool struct{}
    26  
    27  func (s *testWorkerPool) SetUpSuite(c *C)    {}
    28  func (s *testWorkerPool) TearDownSuite(c *C) {}
    29  
    30  var _ = Suite(&testWorkerPool{})
    31  
    32  func TestNewRestoreWorkerPool(t *testing.T) {
    33  	TestingT(t)
    34  }
    35  
    36  func (s *testWorkerPool) TestApplyRecycle(c *C) {
    37  	pool := worker.NewPool(context.Background(), 3, "test")
    38  
    39  	w1, w2, w3 := pool.Apply(), pool.Apply(), pool.Apply()
    40  	c.Assert(w1.ID, Equals, int64(1))
    41  	c.Assert(w2.ID, Equals, int64(2))
    42  	c.Assert(w3.ID, Equals, int64(3))
    43  	c.Assert(pool.HasWorker(), Equals, false)
    44  
    45  	pool.Recycle(w3)
    46  	c.Assert(pool.HasWorker(), Equals, true)
    47  	c.Assert(pool.Apply(), Equals, w3)
    48  	pool.Recycle(w2)
    49  	c.Assert(pool.Apply(), Equals, w2)
    50  	pool.Recycle(w1)
    51  	c.Assert(pool.Apply(), Equals, w1)
    52  
    53  	c.Assert(pool.HasWorker(), Equals, false)
    54  
    55  	c.Assert(func() { pool.Recycle(nil) }, PanicMatches, "invalid restore worker")
    56  }