mclock

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2022 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Overview

Package mclock is a wrapper for a monotonic clock source

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AbsTime

type AbsTime int64

AbsTime represents absolute monotonic time.

func Now

func Now() AbsTime

Now returns the current absolute monotonic time. 获取当前系统启动到现在的绝对时间

func (AbsTime) Add

func (t AbsTime) Add(d time.Duration) AbsTime

对AbsTime实现加减操作 Add returns t + d as absolute time.

func (AbsTime) Sub

func (t AbsTime) Sub(t2 AbsTime) time.Duration

Sub returns t - t2 as a duration. 减法返回time.Duration对象

type ChanTimer

type ChanTimer interface {
	Timer

	// The channel returned by C receives a value when the timer expires.
	// 返回一个只读管道,当ChanTimer时间到了就会返回向管道输入一个值
	C() <-chan AbsTime
	// Reset reschedules the timer with a new timeout.
	// It should be invoked only on stopped or expired timers with drained channels.
	Reset(time.Duration)
}

ChanTimer is a cancellable event created by NewTimer. systemTimer和simTimer实现了这个接口

type Clock

type Clock interface {
	Now() AbsTime
	Sleep(time.Duration)
	NewTimer(time.Duration) ChanTimer
	// After返回一个管道,当输入时间到了管道内输出结束时间
	After(time.Duration) <-chan AbsTime
	AfterFunc(d time.Duration, f func()) Timer
}

The Clock interface makes it possible to replace the monotonic system clock with a simulated clock. System对象和Simulated对象实现了这个接口 System使用的系统内置接口 Simulated应该是重写了一个

type Simulated

type Simulated struct {
	// contains filtered or unexported fields
}

Simulated implements a virtual Clock for reproducible time-sensitive tests. It simulates a scheduler on a virtual timescale where actual processing takes zero time.

The virtual clock doesn't advance on its own, call Run to advance it and execute timers. Since there is no way to influence the Go scheduler, testing timeout behaviour involving goroutines needs special care. A good way to test such timeouts is as follows: First perform the action that is supposed to time out. Ensure that the timer you want to test is created. Then run the clock until after the timeout. Finally observe the effect of the timeout using a channel or semaphore.

func (*Simulated) ActiveTimers

func (s *Simulated) ActiveTimers() int

ActiveTimers returns the number of timers that haven't fired. 返回当前scheduled的长度

func (*Simulated) After

func (s *Simulated) After(d time.Duration) <-chan AbsTime

After returns a channel which receives the current time after the clock has advanced by d.

func (*Simulated) AfterFunc

func (s *Simulated) AfterFunc(d time.Duration, fn func()) Timer

AfterFunc runs fn after the clock has advanced by d. Unlike with the system clock, fn runs on the goroutine that calls Run.

func (*Simulated) NewTimer

func (s *Simulated) NewTimer(d time.Duration) ChanTimer

NewTimer creates a timer which fires when the clock has advanced by d.

func (*Simulated) Now

func (s *Simulated) Now() AbsTime

Now returns the current virtual time.

func (*Simulated) Run

func (s *Simulated) Run(d time.Duration)

Run moves the clock by the given duration, executing all timers before that duration. 调用s.scheduled里面所有到期的函数

func (*Simulated) Sleep

func (s *Simulated) Sleep(d time.Duration)

Sleep blocks until the clock has advanced by d.

func (*Simulated) WaitForTimers

func (s *Simulated) WaitForTimers(n int)

WaitForTimers waits until the clock has at least n scheduled timers.

type System

type System struct{}

System implements Clock using the system clock. 使用系统内置方法实现了Clock接口的5个函数

func (System) After

func (c System) After(d time.Duration) <-chan AbsTime

After returns a channel which receives the current time after d has elapsed. 等待指定时间管道可以读出时间

func (System) AfterFunc

func (c System) AfterFunc(d time.Duration, f func()) Timer

AfterFunc runs f on a new goroutine after the duration has elapsed. 等待指定时间执行函数

func (System) NewTimer

func (c System) NewTimer(d time.Duration) ChanTimer

NewTimer creates a timer which can be rescheduled. 返回一个ChanTimer,等待指定的d时间后timer.C返回的管道会输出结束时的时间

func (System) Now

func (c System) Now() AbsTime

Now returns the current monotonic time.

func (System) Sleep

func (c System) Sleep(d time.Duration)

Sleep blocks for the given duration.

type Timer

type Timer interface {
	// Stop cancels the timer. It returns false if the timer has already
	// expired or been stopped.
	Stop() bool
}

Timer is a cancellable event created by AfterFunc. Clock.AfterFunc返回的对象

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL