github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/uuid/mock.go (about)

     1  // Copyright 2022 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 uuid
    15  
    16  import "github.com/pingcap/log"
    17  
    18  // MockGenerator is a mocked uuid generator
    19  type MockGenerator struct {
    20  	list []string
    21  }
    22  
    23  // NewMock creates a new MockGenerator instance
    24  func NewMock() *MockGenerator {
    25  	return &MockGenerator{}
    26  }
    27  
    28  // NewString implements Generator.NewString
    29  func (g *MockGenerator) NewString() (ret string) {
    30  	if len(g.list) == 0 {
    31  		log.Panic("Empty uuid list. Please use Push() to add a uuid to the list.")
    32  	}
    33  
    34  	ret, g.list = g.list[0], g.list[1:]
    35  	return
    36  }
    37  
    38  // Push adds a candidate uuid in FIFO list
    39  func (g *MockGenerator) Push(uuid string) {
    40  	g.list = append(g.list, uuid)
    41  }
    42  
    43  // ConstGenerator is a mocked uuid generator, which always generate a pre defined uuid
    44  type ConstGenerator struct {
    45  	uid string
    46  }
    47  
    48  // NewConstGenerator creates a new ConstGenerator instance
    49  func NewConstGenerator(uid string) *ConstGenerator {
    50  	return &ConstGenerator{uid: uid}
    51  }
    52  
    53  // NewString implements Generator.NewString
    54  func (g *ConstGenerator) NewString() string {
    55  	return g.uid
    56  }