-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
63 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package stats | ||
|
||
import ( | ||
"time" | ||
) | ||
|
||
// DefaultHeartbeatInterval is the default heartbeat ticker interval. | ||
var DefaultHeartbeatInterval = time.Second | ||
|
||
// Heartbeat enters a loop, reporting a heartbeat counter periodically. | ||
func Heartbeat(stats Statter) { | ||
HeartbeatEvery(stats, DefaultHeartbeatInterval) | ||
} | ||
|
||
// HeartbeatEvery enters a loop, reporting a heartbeat counter at the specified interval. | ||
func HeartbeatEvery(stats Statter, t time.Duration) { | ||
c := time.Tick(t) | ||
for range c { | ||
stats.Inc("heartbeat", 1, 1.0) | ||
} | ||
} | ||
|
||
// HeartbeatFromStatable is the same as HeartbeatEvery but from context. | ||
func HeartbeatFromStatable(sable Statable, t time.Duration) { | ||
HeartbeatEvery(sable.Statter(), t) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package stats_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/hamba/pkg/stats" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
func TestHeartbeat(t *testing.T) { | ||
m := new(MockStats) | ||
m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil) | ||
stats.DefaultHeartbeatInterval = time.Millisecond | ||
|
||
go stats.Heartbeat(m) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything) | ||
} | ||
|
||
func TestHeartbeatFromStatable(t *testing.T) { | ||
m := new(MockStats) | ||
m.On("Inc", "heartbeat", int64(1), float32(1.0), mock.Anything).Return(nil) | ||
sable := stats.NewMockStatable(m) | ||
|
||
go stats.HeartbeatFromStatable(sable, time.Millisecond) | ||
|
||
time.Sleep(100 * time.Millisecond) | ||
|
||
m.AssertCalled(t, "Inc", "heartbeat", int64(1), float32(1.0), mock.Anything) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters