本文整理匯總了Golang中github.com/benbjohnson/clock.NewMock函數的典型用法代碼示例。如果您正苦於以下問題:Golang NewMock函數的具體用法?Golang NewMock怎麽用?Golang NewMock使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了NewMock函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: TestAdvanceWithUpdateOffset
func TestAdvanceWithUpdateOffset(t *testing.T) {
offset := time.Duration(10) * time.Second
last := time.Unix(0, 0)
mock := clock.NewMock()
mock.Add(time.Duration(1) * time.Hour)
desiredLast := mock.Now().Add(-offset)
ticker := NewTicker(last, offset, mock)
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(10)*time.Millisecond, t)
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
// lowering offset should see a few more ticks
offset = time.Duration(5) * time.Second
ticker.updateOffset(offset)
desiredLast = mock.Now().Add(-offset)
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(9)*time.Millisecond, t)
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
// advancing clock should see even more ticks
mock.Add(time.Duration(1) * time.Hour)
desiredLast = mock.Now().Add(-offset)
last = assertAdvanceUntil(ticker, last, desiredLast, offset, time.Duration(8)*time.Millisecond, t)
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
}
示例2: TestTickerNoAdvance
func TestTickerNoAdvance(t *testing.T) {
// it's 00:01:00 now. what are some cases where we don't want the ticker to advance?
mock := clock.NewMock()
mock.Add(time.Duration(60) * time.Second)
type Case struct {
last int
offset int
}
// note that some cases add up to now, others go into the future
cases := []Case{
{50, 10},
{50, 30},
{59, 1},
{59, 10},
{59, 30},
{60, 1},
{60, 10},
{60, 30},
{90, 1},
{90, 10},
{90, 30},
}
for _, c := range cases {
last, offset := getCase(c.last, c.offset)
ticker := NewTicker(last, offset, mock)
assertNoAdvance(ticker, last, time.Duration(500)*time.Millisecond, t)
}
}
示例3: TestMock_Ticker_Multi
// Ensure that multiple tickers can be used together.
func TestMock_Ticker_Multi(t *testing.T) {
var n int32
clock := clock.NewMock()
go func() {
a := clock.Ticker(1 * time.Microsecond)
b := clock.Ticker(3 * time.Microsecond)
for {
select {
case <-a.C:
atomic.AddInt32(&n, 1)
case <-b.C:
atomic.AddInt32(&n, 100)
}
}
}()
gosched()
// Move clock forward.
clock.Add(10 * time.Microsecond)
gosched()
if atomic.LoadInt32(&n) != 310 {
t.Fatalf("unexpected: %d", n)
}
}
示例4: TestMock_Ticker_Stop
// Ensure that the mock's Ticker can be stopped.
func TestMock_Ticker_Stop(t *testing.T) {
var n int32
clock := clock.NewMock()
// Create a channel to increment every second.
ticker := clock.Ticker(1 * time.Second)
go func() {
for {
<-ticker.C
atomic.AddInt32(&n, 1)
}
}()
gosched()
// Move clock forward.
clock.Add(5 * time.Second)
if atomic.LoadInt32(&n) != 5 {
t.Fatalf("expected 5, got: %d", n)
}
ticker.Stop()
// Move clock forward again.
clock.Add(5 * time.Second)
if atomic.LoadInt32(&n) != 5 {
t.Fatalf("still expected 5, got: %d", n)
}
}
示例5: ExampleMock_Ticker
func ExampleMock_Ticker() {
// Create a new mock clock.
clock := clock.NewMock()
count := 0
// Increment count every mock second.
go func() {
ticker := clock.Ticker(1 * time.Second)
for {
<-ticker.C
count++
}
}()
runtime.Gosched()
// Move the clock forward 10 seconds and print the new value.
clock.Add(10 * time.Second)
fmt.Printf("Count is %d after 10 seconds\n", count)
// Move the clock forward 5 more seconds and print the new value.
clock.Add(5 * time.Second)
fmt.Printf("Count is %d after 15 seconds\n", count)
// Output:
// Count is 10 after 10 seconds
// Count is 15 after 15 seconds
}
示例6: ExampleMock_After
func ExampleMock_After() {
// Create a new mock clock.
clock := clock.NewMock()
count := 0
// Create a channel to execute after 10 mock seconds.
go func() {
<-clock.After(10 * time.Second)
count = 100
}()
runtime.Gosched()
// Print the starting value.
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
// Move the clock forward 5 seconds and print the value again.
clock.Add(5 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
// Move the clock forward 5 seconds to the tick time and check the value.
clock.Add(5 * time.Second)
fmt.Printf("%s: %d\n", clock.Now().UTC(), count)
// Output:
// 1970-01-01 00:00:00 +0000 UTC: 0
// 1970-01-01 00:00:05 +0000 UTC: 0
// 1970-01-01 00:00:10 +0000 UTC: 100
}
示例7: TestEtcdKeyMaintainer
func TestEtcdKeyMaintainer(t *testing.T) {
api := new(testEtcdAPI)
clk := clock.NewMock()
m := NewKeyMaintainer(api, "/test", "asdf")
m.clock = clk
api.On("Set", mock.Anything, "/test", "asdf", mock.Anything).Return(nil, nil)
go m.Maintain()
pause()
api.AssertNumberOfCalls(t, "Set", 1)
clk.Add(m.Interval - time.Nanosecond)
api.AssertNumberOfCalls(t, "Set", 1)
clk.Add(time.Nanosecond + 100)
api.AssertNumberOfCalls(t, "Set", 2)
api.On("Delete", mock.Anything, "/test", mock.Anything).Return(nil, nil)
m.Close()
pause()
clk.Add(m.Interval)
api.AssertNumberOfCalls(t, "Set", 2)
del := api.Calls[2]
assert.Equal(t, "Delete", del.Method)
assert.Equal(t, "/test", del.Arguments.String(1))
}
示例8: TestSchedCallsGap
// Ensure we can create a 100 seconds gap in the middle of the time travel
func TestSchedCallsGap(t *testing.T) {
EnableDebugLogging(testing.Verbose())
Log.Infoln("TestSchedCallsGap starting")
const testSecs = 1000
clk := clock.NewMock()
schedQueue := NewSchedQueue(clk)
schedQueue.Start()
defer schedQueue.Stop()
c2 := func() time.Time {
if schedQueue.Count() == testSecs/2 {
return clk.Now().Add(time.Duration(100) * time.Second)
}
return clk.Now().Add(time.Second)
}
schedQueue.Add(c2, clk.Now().Add(time.Second))
schedQueue.Flush()
for i := 0; i < testSecs; i++ {
clk.Add(time.Second)
schedQueue.Flush()
}
t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count())
require.Equal(t, testSecs-100+1, (int)(schedQueue.Count()), "Number of calls")
}
示例9: TestSchedCallsStop
func TestSchedCallsStop(t *testing.T) {
InitDefaultLogging(testing.Verbose())
Info.Println("TestSchedCallsStop starting")
const testSecs = 1000
clk := clock.NewMock()
schedQueue := NewSchedQueue(clk)
schedQueue.Start()
defer schedQueue.Stop()
c2 := func() time.Time {
if schedQueue.Count() == testSecs/2 {
return time.Time{}
}
return clk.Now().Add(time.Second)
}
schedQueue.Add(c2, clk.Now().Add(time.Second))
schedQueue.Flush()
for i := 0; i < testSecs; i++ {
clk.Add(time.Second)
schedQueue.Flush()
}
t.Logf("Now: %s - calls: %d", clk.Now(), schedQueue.Count())
require.Equal(t, testSecs/2, (int)(schedQueue.Count()), "Number of calls")
}
示例10: TestMock_Tick
// Ensure that the mock's Tick channel sends at the correct time.
func TestMock_Tick(t *testing.T) {
var n int32
clock := clock.NewMock()
// Create a channel to increment every 10 seconds.
go func() {
tick := clock.Tick(10 * time.Second)
for {
<-tick
atomic.AddInt32(&n, 1)
}
}()
gosched()
// Move clock forward to just before the first tick.
clock.Add(9 * time.Second)
if atomic.LoadInt32(&n) != 0 {
t.Fatalf("expected 0, got %d", n)
}
// Move clock forward to the start of the first tick.
clock.Add(1 * time.Second)
if atomic.LoadInt32(&n) != 1 {
t.Fatalf("expected 1, got %d", n)
}
// Move clock forward over several ticks.
clock.Add(30 * time.Second)
if atomic.LoadInt32(&n) != 4 {
t.Fatalf("expected 4, got %d", n)
}
}
示例11: BenchmarkIncomingMetricAmounts
func BenchmarkIncomingMetricAmounts(b *testing.B) {
daemon := New("test", "rates.", "timers.", "gauges.", "counters.", timers.Percentiles{}, 10, 1000, 1000, nil, false, true, true, false)
daemon.Clock = clock.NewMock()
daemon.submitFunc = func(c *counters.Counters, g *gauges.Gauges, t *timers.Timers, deadline time.Time) {
}
go daemon.RunBare()
b.ResetTimer()
counters := make([]*common.Metric, 10)
for i := 0; i < 10; i++ {
counters[i] = &common.Metric{
Bucket: "test-counter",
Value: float64(1),
Modifier: "c",
Sampling: float32(1),
}
}
// each operation consists of 100x write (1k * 10 metrics + move clock by 1second)
// simulating a fake 10k metrics/s load, 1M metrics in total over 100+10s, so 11 flushes
for n := 0; n < b.N; n++ {
for j := 0; j < 100; j++ {
for i := 0; i < 1000; i++ {
daemon.metricAmounts <- counters
}
daemon.Clock.(*clock.Mock).Add(1 * time.Second)
}
daemon.Clock.(*clock.Mock).Add(10 * time.Second)
}
}
示例12: init
func init() {
clock := clock.NewMock()
backend := memory.NewWithClock(clock)
check.Suite(&coordinatetest.Suite{
Coordinate: backend,
Clock: clock,
})
}
示例13: newChannelNodeWithHostPort
// newChannelNodeWithHostPort creates a testNode with the address specified by
// the hostport parameter.
func newChannelNodeWithHostPort(t *testing.T, hostport string) *testNode {
ch, err := tchannel.NewChannel("test", nil)
require.NoError(t, err, "channel must create successfully")
node := NewNode("test", hostport, ch.GetSubChannel("test"), &Options{
Clock: clock.NewMock(),
})
return &testNode{node, ch}
}
示例14: TestMock_Now
// Ensure that the mock's current time can be changed.
func TestMock_Now(t *testing.T) {
clock := clock.NewMock()
if now := clock.Now(); !now.Equal(time.Unix(0, 0)) {
t.Fatalf("expected epoch, got: ", now)
}
// Add 10 seconds and check the time.
clock.Add(10 * time.Second)
if now := clock.Now(); !now.Equal(time.Unix(10, 0)) {
t.Fatalf("expected epoch, got: ", now)
}
}
示例15: TestProfileSample
func TestProfileSample(t *testing.T) {
wantProfile := TimeProfile{
Total: 10 * time.Second,
WaitRead: 1 * time.Second,
WaitWrite: 9 * time.Second,
}
clk := clock.NewMock()
start := clk.Now()
sleepRead := readFunc(func(p []byte) (int, error) {
var err error
if clk.Now().Sub(start) >= wantProfile.Total {
err = io.EOF
}
clk.Sleep(wantProfile.WaitRead / 1000)
return len(p), err
})
sleepWrite := writeFunc(func(p []byte) (int, error) {
clk.Sleep(wantProfile.WaitWrite / 1000)
return len(p), nil
})
res := make(chan TimeProfile, 1)
go func() {
w, r, done := profileSample(clk, sleepWrite, sleepRead, time.Millisecond)
io.Copy(w, r)
res <- done().TimeProfile
}()
var gotProfile TimeProfile
loop:
for {
select {
case gotProfile = <-res:
break loop
default:
clk.Add(1 * time.Millisecond)
}
}
wantReadRatio := float64(wantProfile.WaitRead) / float64(wantProfile.WaitRead+wantProfile.WaitWrite)
gotReadRatio := float64(gotProfile.WaitRead) / float64(gotProfile.WaitRead+gotProfile.WaitWrite)
diff := (math.Max(wantReadRatio, gotReadRatio) - math.Min(wantReadRatio, gotReadRatio)) / math.Max(wantReadRatio, gotReadRatio)
if diff > 0.05 {
t.Logf("want=%#v", wantProfile)
t.Logf(" got=%#v", gotProfile)
t.Fatalf("profiles are too different: %.2f%% different", 100*diff)
}
}