...

Source file src/github.com/goph/emperror/handler_testing.go

Documentation: github.com/goph/emperror

     1  package emperror
     2  
     3  import "sync"
     4  
     5  // TestHandler is a simple stub for the handler interface recording every error.
     6  //
     7  // The TestHandler is safe for concurrent use.
     8  type TestHandler struct {
     9  	errors []error
    10  
    11  	mu sync.RWMutex
    12  }
    13  
    14  // NewTestHandler returns a new TestHandler.
    15  func NewTestHandler() *TestHandler {
    16  	return &TestHandler{}
    17  }
    18  
    19  // Count returns the number of events recorded in the logger.
    20  func (h *TestHandler) Count() int {
    21  	h.mu.RLock()
    22  	defer h.mu.RUnlock()
    23  
    24  	return len(h.errors)
    25  }
    26  
    27  // LastError returns the last handled error (if any).
    28  func (h *TestHandler) LastError() error {
    29  	h.mu.RLock()
    30  	defer h.mu.RUnlock()
    31  
    32  	if len(h.errors) < 1 {
    33  		return nil
    34  	}
    35  
    36  	return h.errors[len(h.errors)-1]
    37  }
    38  
    39  // Errors returns all handled errors.
    40  func (h *TestHandler) Errors() []error {
    41  	h.mu.RLock()
    42  	defer h.mu.RUnlock()
    43  
    44  	return h.errors
    45  }
    46  
    47  // Handle records the error.
    48  func (h *TestHandler) Handle(err error) {
    49  	h.mu.Lock()
    50  	defer h.mu.Unlock()
    51  
    52  	h.errors = append(h.errors, err)
    53  }
    54  

View as plain text