forked from opentracing/basictracer-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecorder_test.go
66 lines (58 loc) · 1.55 KB
/
recorder_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package basictracer
import (
"reflect"
"sync"
"sync/atomic"
"testing"
"time"
)
// InMemoryRecorder is a simple thread-safe implementation of
// SpanRecorder that stores all reported spans in memory, accessible
// via reporter.GetSpans()
type InMemoryRecorder struct {
spans []RawSpan
lock sync.Mutex
}
// NewInMemoryRecorder instantiates a new InMemoryRecorder for testing purposes.
func NewInMemoryRecorder() *InMemoryRecorder {
return &InMemoryRecorder{
spans: make([]RawSpan, 0),
}
}
// RecordSpan implements RecordSpan() of SpanRecorder.
//
// The recorded spans can be retrieved via recorder.Spans slice.
func (recorder *InMemoryRecorder) RecordSpan(span RawSpan) {
recorder.lock.Lock()
defer recorder.lock.Unlock()
recorder.spans = append(recorder.spans, span)
}
// GetSpans returns a snapshot of spans recorded so far.
func (recorder *InMemoryRecorder) GetSpans() []RawSpan {
recorder.lock.Lock()
defer recorder.lock.Unlock()
spans := make([]RawSpan, len(recorder.spans))
copy(spans, recorder.spans)
return spans
}
func TestInMemoryRecorderSpans(t *testing.T) {
recorder := NewInMemoryRecorder()
var apiRecorder SpanRecorder = recorder
span := RawSpan{
Context: Context{},
Operation: "test-span",
Start: time.Now(),
Duration: -1,
}
apiRecorder.RecordSpan(span)
if len(recorder.GetSpans()) != 1 {
t.Fatal("No spans recorded")
}
if !reflect.DeepEqual(recorder.GetSpans()[0], span) {
t.Fatal("Span not recorded")
}
}
type CountingRecorder int32
func (c *CountingRecorder) RecordSpan(r RawSpan) {
atomic.AddInt32((*int32)(c), 1)
}