Skip to content

Commit

Permalink
Make mock sink thread safe (#23)
Browse files Browse the repository at this point in the history
* race-safe

* remove nl
  • Loading branch information
parkwherever authored Mar 2, 2018
1 parent 2069cc4 commit 9c23c53
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions mock_sink.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package stats

import "sync"

// MockSink describes an in-memory Sink used for testing.
type MockSink struct {
Counters map[string]uint64
Timers map[string]uint64
Gauges map[string]uint64

cLock sync.Mutex
tLock sync.Mutex
gLock sync.Mutex
}

// NewMockSink returns a MockSink that flushes stats to in-memory maps. An
Expand All @@ -21,15 +27,21 @@ func NewMockSink() (m *MockSink) {

// FlushCounter satisfies the Sink interface.
func (m *MockSink) FlushCounter(name string, value uint64) {
m.cLock.Lock()
defer m.cLock.Unlock()
m.Counters[name] += value
}

// FlushGauge satisfies the Sink interface.
func (m *MockSink) FlushGauge(name string, value uint64) {
m.gLock.Lock()
defer m.gLock.Unlock()
m.Gauges[name] = value
}

// FlushTimer satisfies the Sink interface.
func (m *MockSink) FlushTimer(name string, value float64) {
m.tLock.Lock()
defer m.tLock.Unlock()
m.Timers[name]++
}

0 comments on commit 9c23c53

Please sign in to comment.