• This repository has been archived on 18/May/2023
  • Stars
    star
    677
  • Rank 66,694 (Top 2 %)
  • Language
    Go
  • License
    MIT License
  • Created about 10 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Clock is a small library for mocking time in Go.

clock

go.dev reference

Clock is a small library for mocking time in Go. It provides an interface around the standard library's time package so that the application can use the realtime clock while tests can use the mock clock.

This module is no longer maintained.

Usage

Realtime Clock

Your application can maintain a Clock variable that will allow realtime and mock clocks to be interchangeable. For example, if you had an Application type:

import "github.com/benbjohnson/clock"

type Application struct {
	Clock clock.Clock
}

You could initialize it to use the realtime clock like this:

var app Application
app.Clock = clock.New()
...

Then all timers and time-related functionality should be performed from the Clock variable.

Mocking time

In your tests, you will want to use a Mock clock:

import (
	"testing"

	"github.com/benbjohnson/clock"
)

func TestApplication_DoSomething(t *testing.T) {
	mock := clock.NewMock()
	app := Application{Clock: mock}
	...
}

Now that you've initialized your application to use the mock clock, you can adjust the time programmatically. The mock clock always starts from the Unix epoch (midnight UTC on Jan 1, 1970).

Controlling time

The mock clock provides the same functions that the standard library's time package provides. For example, to find the current time, you use the Now() function:

mock := clock.NewMock()

// Find the current time.
mock.Now().UTC() // 1970-01-01 00:00:00 +0000 UTC

// Move the clock forward.
mock.Add(2 * time.Hour)

// Check the time again. It's 2 hours later!
mock.Now().UTC() // 1970-01-01 02:00:00 +0000 UTC

Timers and Tickers are also controlled by this same mock clock. They will only execute when the clock is moved forward:

mock := clock.NewMock()
count := 0

// Kick off a timer to increment every 1 mock second.
go func() {
    ticker := mock.Ticker(1 * time.Second)
    for {
        <-ticker.C
        count++
    }
}()
runtime.Gosched()

// Move the clock forward 10 seconds.
mock.Add(10 * time.Second)

// This prints 10.
fmt.Println(count)

More Repositories

1

litestream

Streaming replication for SQLite.
Go
10,958
star
2

thesecretlivesofdata

Understanding what your bits do when you're not looking.
JavaScript
3,440
star
3

wtf

WTF Dial is an example web application written in Go.
Go
1,682
star
4

postlite

Postgres wire compatible SQLite proxy.
Go
1,216
star
5

immutable

Immutable collections for Go
Go
713
star
6

ego

An ERB-style templating language for Go.
Go
581
star
7

testing

A small collection of functions for Go testing.
Go
529
star
8

megajson

A JSON parser generator for high performance encoding and decoding in Go.
Go
466
star
9

hashfs

Implementation of io/fs.FS that appends SHA256 hashes to filenames to allow for aggressive HTTP caching.
Go
358
star
10

sql-parser

Toy SQL parser example for Gopher Academy
Go
330
star
11

genesis

A simple tool for embedding assets in a Go binary.
Go
299
star
12

phantomjs

Go client for PhantomJS.
Go
293
star
13

jmphash

Implementation of the Jump Consistent Hash algorithm in Go.
Go
154
star
14

scuttlebutt

A daemon for tracking and tweeting trending Github repositories by language.
Go
151
star
15

llvm-c-kaleidoscope

An implementation of the Kaleidoscope language using Flex, Bison & the LLVM-C bindings.
C
129
star
16

playback.js

A library for dynamic timeline playback.
JavaScript
118
star
17

litestream-docker-example

An example of using Litestream within a Docker container.
Go
97
star
18

litestream-s6-example

Example repository for building a multi-process Docker container.
Dockerfile
87
star
19

tmpl

Command line interface to Go's text/template library.
Go
84
star
20

css

W3C-compliant CSS3 parser and scanner
Go
83
star
21

grapevine

Trending topics for stuff you care about
Ruby
80
star
22

slowweb

An HTTP request governor
Ruby
75
star
23

litestream-read-replica-demo

A demo application for running live read replication on fly.io with Litestream
Go
69
star
24

ghfs

FUSE Filesystem for the GitHub API
Go
61
star
25

agency

A fast user agent string parser for Go.
Go
60
star
26

litestream-library-example

Example repository for embedding Litestream in a Go application.
Go
56
star
27

litestream-read-replica-example

An example of using Litestream's live read replication feature.
Go
52
star
28

melomel

External ActionScript Interface.
ActionScript
42
star
29

litestream.io

SCSS
40
star
30

pprofdump

A simple utility for collecting net/http/pprof profiles.
Go
28
star
31

peapod

A personal podcast service.
Go
26
star
32

application-development-using-boltdb

Repository for my "Application Development Using BoltDB" talk
Go
26
star
33

sieve

A command line utility for graphing piped data.
Go
23
star
34

production-sqlite-go

Companion repository for GopherCon presentation on "Production Applications Using SQLite & Go"
Go
23
star
35

goo

Thin wrapper for the Go toolchain.
Go
20
star
36

burger-stack

Presentation for "The Burger Stack"
19
star
37

structuring-applications-for-growth

GopherCon 2016 presentation for "Structuring Applications for Growth"
18
star
38

stack

Go debug/stack utility functions.
Go
18
star
39

myapp

An simple application with an HTTP server & SQLite database.
Go
14
star
40

glee

Incomplete Go port of the KLEE SymEx system.
Go
14
star
41

melomel.rb

An external interface to Flash from Ruby.
Ruby
13
star
42

raft.js

An experimental Raft implementation in Javascript.
13
star
43

vex

Variable-length, lexicographically-sortable hex format for uint64 values.
Go
13
star
44

gha

SQLite load testing application using GitHub Archive data.
Go
13
star
45

writing-a-distributed-systems-library

Companion code for the Gopher Academy blog post.
Go
11
star
46

roommate

A conference room scheduling application.
Go
10
star
47

tiny-ego

A toy application showcasing ego templates & components.
Go
10
star
48

boxer

A little app that boxes my time.
Go
10
star
49

bootstrap-ego

An ego template component library for Bootstrap 4.
Go
10
star
50

seppuku

To be executed upon implementation of generics in Go.
Go
10
star
51

syncutil

A collection of utility functions for Go synchronization.
Go
9
star
52

gist

Gist hosting and embedding.
Go
8
star
53

melomel-examples

Examples of using Melomel.
Ruby
8
star
54

describe.today

A web site to describe today.
JavaScript
7
star
55

minipack

A lightweight C MessagePack parser.
C
7
star
56

skybox

An open source funnel analysis application.
Go
7
star
57

go-raft-runner

A test runner application for the go-raft library.
Go
6
star
58

http-wiretap

It's like Charles Proxy for your Rubies!
Ruby
5
star
59

sqlite-bench

Miscellaneous Go/SQLite benchmarks
Go
5
star
60

mincore

Example usage for using mincore() in Go.
Go
5
star
61

miniviz

A simplified interface to GraphViz for laying out clusters, nodes and edges.
Ruby
5
star
62

graphviz-as3

An interface to the graphviz CLI using Adobe AIR.
ActionScript
5
star
63

edb

A simple database for tracking events.
Go
5
star
64

rationl

Online journal for tracking experiments.
Go
4
star
65

hackerbeeper

A dumb utility for playing generated notes when you type.
Go
4
star
66

opus

Command line utility for printing columnized code.
4
star
67

mockdown.as

A Markdown-inspired Mockup Language
ActionScript
4
star
68

serialkiller

ActionScript JSON & XML serialization library
ActionScript
4
star
69

whodump

A command line interface for checking domain name availability.
Ruby
4
star
70

tsld.js

Core library for "The Secret Lives of Data" project.
JavaScript
3
star
71

bandicoot

A crash reporter library for C applications.
C
3
star
72

skydb.io

The Official Sky Web Site
CSS
2
star
73

chatter

Demo application using Server Side Events (SSE) and written in Go.
Go
2
star
74

cine

A movie search application.
Go
2
star
75

authoritarian

Command line utility for authorizing Twitter users to an application.
2
star
76

constdump

Utility for printing a list of package-level constants.
Go
2
star
77

matlock

Simple name extraction utility.
Ruby
2
star
78

d3.jquery.js

A collection of D3.js charts made available as jQuery plugins.
JavaScript
1
star
79

mockdown.rb

Mockups for hackers
Ruby
1
star
80

landmarkd

The Landmark Tracking Server.
Go
1
star
81

httpng

A local server for saving HTML elements as PNG files.
JavaScript
1
star
82

ldbchk

Runs concurrency tests against LevelDB & Levigo.
Go
1
star
83

timeshifter

A Ruby library for shifting time.
Ruby
1
star
84

whollydeliciousfoods.com

The home page for Wholly Delicious Foods.
JavaScript
1
star
85

homebrew-litestream

Homebrew tap for litestream.
Ruby
1
star
86

bench-c

A small collection of benchmarks for the C programming language.
C
1
star
87

unistat

A utility for calculating simple statistics on unicode characters.
Go
1
star
88

gitcoin

Turing gitcoin repository
Go
1
star
89

termgraf

Terminal chronograf
Go
1
star
90

locald

A simple HTTP server for serving static files out of the current directory.
1
star
91

skylandlabs.github.com

Skyland Labs Blog
JavaScript
1
star
92

fql-editor

An editor for FQL queries.
ActionScript
1
star
93

tip.litestream.io

Mirror of litestream.io repository for latest changes.
HTML
1
star
94

prog

Go testing
Go
1
star
95

fslice

A small utility for extracting delimited sections of a file.
Go
1
star
96

ego-example

A simple ego templating example.
Go
1
star