github.com/epsagon/epsagon-go@v1.39.0/tracer/mocked_tracer.go (about)

     1  package tracer
     2  
     3  import (
     4  	"time"
     5  
     6  	"github.com/epsagon/epsagon-go/protocol"
     7  )
     8  
     9  // MockedEpsagonTracer will not send traces if closed
    10  type MockedEpsagonTracer struct {
    11  	Exceptions      *[]*protocol.Exception
    12  	Events          *[]*protocol.Event
    13  	Labels          map[string]interface{}
    14  	RunnerException *protocol.Exception
    15  	Config          *Config
    16  
    17  	PanicStart        bool
    18  	PanicAddEvent     bool
    19  	PanicAddException bool
    20  	PanicStop         bool
    21  	DelayAddEvent     bool
    22  	DelayedEventsChan chan bool
    23  	stopped           bool
    24  }
    25  
    26  // Start implementes mocked Start
    27  func (t *MockedEpsagonTracer) Start() {
    28  	if t.PanicStart {
    29  		panic("panic in Start()")
    30  	}
    31  	t.stopped = false
    32  }
    33  
    34  // Running implementes mocked Running
    35  func (t *MockedEpsagonTracer) Running() bool {
    36  	return false
    37  }
    38  
    39  // Stop implementes mocked Stop
    40  func (t *MockedEpsagonTracer) SendStopSignal() {
    41  	if t.PanicStop {
    42  		panic("panic in Stop()")
    43  	}
    44  	t.stopped = true
    45  }
    46  
    47  // Stop implementes mocked Stop
    48  func (t *MockedEpsagonTracer) Stop() {
    49  	if t.PanicStop {
    50  		panic("panic in Stop()")
    51  	}
    52  	t.stopped = true
    53  }
    54  
    55  // Stopped implementes mocked Stopped
    56  func (t *MockedEpsagonTracer) Stopped() bool {
    57  	return t.stopped
    58  }
    59  
    60  // AddEvent implementes mocked AddEvent
    61  func (t *MockedEpsagonTracer) AddEvent(e *protocol.Event) {
    62  	if t.PanicAddEvent {
    63  		panic("panic in AddEvent()")
    64  	}
    65  	if t.DelayAddEvent {
    66  		go func() {
    67  			time.Sleep(time.Second)
    68  			*t.Events = append(*t.Events, e)
    69  			t.DelayedEventsChan <- true
    70  		}()
    71  	} else {
    72  		*t.Events = append(*t.Events, e)
    73  	}
    74  }
    75  
    76  // AddException implementes mocked AddEvent
    77  func (t *MockedEpsagonTracer) AddException(e *protocol.Exception) {
    78  	if t.PanicAddException {
    79  		panic("panic in AddException()")
    80  	}
    81  	*t.Exceptions = append(*t.Exceptions, e)
    82  }
    83  
    84  // GetConfig implementes mocked AddEvent
    85  func (t *MockedEpsagonTracer) GetConfig() *Config {
    86  	return t.Config
    87  }
    88  
    89  // AddExceptionTypeAndMessage implements AddExceptionTypeAndMessage
    90  func (t *MockedEpsagonTracer) AddExceptionTypeAndMessage(exceptionType, msg string) {
    91  	t.AddException(&protocol.Exception{
    92  		Type:    exceptionType,
    93  		Message: msg,
    94  		Time:    GetTimestamp(),
    95  	})
    96  }
    97  
    98  // AddLabel implements AddLabel
    99  func (t *MockedEpsagonTracer) AddLabel(key string, value interface{}) {
   100  	t.Labels[key] = value
   101  }
   102  
   103  // verifyLabel implements verifyLabel
   104  func (t *MockedEpsagonTracer) verifyLabel(label epsagonLabel) bool {
   105  	return true
   106  }
   107  
   108  // AddError implements AddError
   109  func (t *MockedEpsagonTracer) AddError(errorType string, value interface{}) {
   110  	t.RunnerException = &protocol.Exception{
   111  		Type:    errorType,
   112  		Message: "test",
   113  	}
   114  }
   115  
   116  // GetRunnerEvent implements AddError
   117  func (t *MockedEpsagonTracer) GetRunnerEvent() *protocol.Event {
   118  	for _, event := range *t.Events {
   119  		if event.Origin == "runner" {
   120  			return event
   121  		}
   122  	}
   123  	return nil
   124  }