...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/mock/mock.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/mock

     1  // Copyright 2021 Chaos Mesh Authors.
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  //
    15  
    16  package mock
    17  
    18  import (
    19  	"path"
    20  	"reflect"
    21  	"sync"
    22  
    23  	"github.com/pingcap/failpoint"
    24  )
    25  
    26  // Finalizer represent the function that clean a mock point
    27  type Finalizer func() error
    28  
    29  type mockPoints struct {
    30  	m map[string]interface{}
    31  	l sync.Mutex
    32  }
    33  
    34  func (p *mockPoints) set(fpname string, value interface{}) {
    35  	p.l.Lock()
    36  	defer p.l.Unlock()
    37  
    38  	p.m[fpname] = value
    39  }
    40  
    41  func (p *mockPoints) get(fpname string) interface{} {
    42  	p.l.Lock()
    43  	defer p.l.Unlock()
    44  
    45  	return p.m[fpname]
    46  }
    47  
    48  func (p *mockPoints) clr(fpname string) {
    49  	p.l.Lock()
    50  	defer p.l.Unlock()
    51  
    52  	delete(p.m, fpname)
    53  }
    54  
    55  var points = mockPoints{m: make(map[string]interface{})}
    56  
    57  // On inject a failpoint
    58  func On(fpname string) interface{} {
    59  	var ret interface{}
    60  	failpoint.Inject(fpname, func() {
    61  		ret = points.get(fpname)
    62  	})
    63  	return ret
    64  }
    65  
    66  // With enable failpoint and provide a value
    67  func With(fpname string, value interface{}) Finalizer {
    68  	if err := failpoint.Enable(failpath(fpname), "return(true)"); err != nil {
    69  		panic(err)
    70  	}
    71  	points.set(fpname, value)
    72  	return func() error { return Reset(fpname) }
    73  }
    74  
    75  // Reset disable failpoint and remove mock value
    76  func Reset(fpname string) error {
    77  	if err := failpoint.Disable(failpath(fpname)); err != nil {
    78  		return err
    79  	}
    80  	points.clr(fpname)
    81  	return nil
    82  }
    83  
    84  func failpath(fpname string) string {
    85  	type em struct{}
    86  	return path.Join(reflect.TypeOf(em{}).PkgPath(), fpname)
    87  }
    88