-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce an API to set a mock clock (#20)
- Loading branch information
1 parent
e6db3c8
commit 3f13d05
Showing
5 changed files
with
62 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ os: | |
language: go | ||
|
||
go: | ||
- 1.13.x | ||
- 1.17.x | ||
|
||
env: | ||
global: | ||
|
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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
module github.com/libp2p/go-flow-metrics | ||
|
||
go 1.12 | ||
go 1.17 | ||
|
||
require github.com/benbjohnson/clock v1.3.0 |
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,2 @@ | ||
github.com/benbjohnson/clock v1.3.0 h1:ip6w0uFQkncKQ979AypyG0ER7mqUSBdKLOgAle/AT8A= | ||
github.com/benbjohnson/clock v1.3.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA= |
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,43 @@ | ||
package mockclocktest | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/libp2p/go-flow-metrics" | ||
|
||
"github.com/benbjohnson/clock" | ||
) | ||
|
||
var cl = clock.NewMock() | ||
|
||
func init() { | ||
flow.SetClock(cl) | ||
} | ||
|
||
func TestBasic(t *testing.T) { | ||
m := new(flow.Meter) | ||
for i := 0; i < 300; i++ { | ||
m.Mark(1000) | ||
cl.Add(40 * time.Millisecond) | ||
} | ||
if rate := m.Snapshot().Rate; rate != 25000 { | ||
t.Errorf("expected rate 25000, got %f", rate) | ||
} | ||
|
||
for i := 0; i < 200; i++ { | ||
m.Mark(200) | ||
cl.Add(40 * time.Millisecond) | ||
} | ||
|
||
// Adjusts | ||
if rate := m.Snapshot().Rate; rate != 5017.776503840969 { | ||
t.Errorf("expected rate 5017.776503840969, got %f", rate) | ||
} | ||
|
||
// Let it settle. | ||
cl.Add(2 * time.Second) | ||
if total := m.Snapshot().Total; total != 340000 { | ||
t.Errorf("expected total 3400000, got %d", total) | ||
} | ||
} |
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