github.com/NebulousLabs/Sia@v1.3.7/siatest/dependencies.go (about)

     1  package siatest
     2  
     3  import (
     4  	"sync"
     5  
     6  	"github.com/NebulousLabs/Sia/modules"
     7  )
     8  
     9  type (
    10  	// DependencyInterruptOnceOnKeyword is a generic dependency that interrupts
    11  	// the flow of the program if the argument passed to Disrupt equals str and
    12  	// if f was set to true by calling Fail.
    13  	DependencyInterruptOnceOnKeyword struct {
    14  		f bool // indicates if the next download should fail
    15  		modules.ProductionDependencies
    16  		mu  sync.Mutex
    17  		str string
    18  	}
    19  )
    20  
    21  // NewDependencyInterruptOnceOnKeyword creates a new
    22  // DependencyInterruptOnceOnKeyword from a given disrupt key.
    23  func NewDependencyInterruptOnceOnKeyword(str string) *DependencyInterruptOnceOnKeyword {
    24  	return &DependencyInterruptOnceOnKeyword{
    25  		str: str,
    26  	}
    27  }
    28  
    29  // Disrupt returns true if the correct string is provided and if the flag was
    30  // set to true by calling fail on the dependency beforehand. After simulating a
    31  // crash the flag will be set to false and fail has to be called again for
    32  // another disruption.
    33  func (d *DependencyInterruptOnceOnKeyword) Disrupt(s string) bool {
    34  	d.mu.Lock()
    35  	defer d.mu.Unlock()
    36  	if d.f && s == d.str {
    37  		d.f = false
    38  		return true
    39  	}
    40  	return false
    41  }
    42  
    43  // Fail causes the next call to Disrupt to return true if the correct string is
    44  // provided.
    45  func (d *DependencyInterruptOnceOnKeyword) Fail() {
    46  	d.mu.Lock()
    47  	d.f = true
    48  	d.mu.Unlock()
    49  }
    50  
    51  // Disable sets the flag to false to make sure that the dependency won't fail.
    52  func (d *DependencyInterruptOnceOnKeyword) Disable() {
    53  	d.mu.Lock()
    54  	d.f = false
    55  	d.mu.Unlock()
    56  }