fakeclock

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package fakeclock 是测试专用的假时钟:时间只在调 Advance 时前进, 测试不真 sleep,时序完全确定(宪法第 V 条)。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

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

Clock 实现 taskgate.Clock。并发安全,所有等待者靠 Advance 手动唤醒。

func New

func New(start time.Time) *Clock

New 从 start 时刻起步。

func (*Clock) Advance

func (c *Clock) Advance(d time.Duration)

Advance 把时间往前拨 d,唤醒所有到点的等待者,给到点的 ticker 补上滴答。

func (*Clock) After

func (c *Clock) After(d time.Duration) <-chan time.Time

After 注册一个到点唤醒;d<=0 立即就绪。channel 带 1 缓冲,Advance 发的时候不会卡住。

func (*Clock) NewTicker

func (c *Clock) NewTicker(d time.Duration) taskgate.Ticker

NewTicker 假滴答:每次 Advance 跨过一个周期就发一格(channel 满了就丢,和真 Ticker 一样)。

func (*Clock) Now

func (c *Clock) Now() time.Time

Now 当前假时刻。

func (*Clock) Sleep

func (c *Clock) Sleep(ctx context.Context, d time.Duration) error

Sleep 等到假时间推进过 d,或 ctx 先取消。

func (*Clock) TickerCount

func (c *Clock) TickerCount() int

TickerCount 当前注册的 ticker 数(含已 Stop 未清理的)。给测试做同步用: 被测代码的 ticker 常由后台 goroutine 异步创建,先等它挂上再 Advance, 否则拨钟发生在注册之前,那一格滴答就永远丢了(假时钟只在 Advance 时发滴答)。

Jump to

Keyboard shortcuts

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