lockstep

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: MIT Imports: 9 Imported by: 0

README

lockstep

Go Reference

A Go testing primitive for facilitating the testing of complex concurrent systems.

LockStep supports two operations: Emit and Wait. An Emit operation with a message x will block until a corresponding Wait operation with message x is processed. Likewise, a Wait operation with y will also block until the corresponding Emit operation with y is processed.

Example:

func TestExample(t *testing.T) {
  ls := lockstep.New(t)

  const d = 300 * time.Millisecond
  go func() {
    ls.Wait("go1")
    time.AfterFunc(d, func() {
      ls.Emit("done1")
    })
  }()
  go func() {
    ls.Wait("go2")
    <-time.After(d)
    ls.Emit("done2")
  }()

  begin := time.Now()
  ls.Emit("go1")
  ls.Emit("go2")
  ls.Wait("done1", "done2")
  dur := time.Since(begin)

  delta := dur - d
  if delta < 0 {
    delta *= -1
  }
  const e = 50 * time.Millisecond
  if delta > e {
    t.Fatalf("Expected callback in %v, actual was %v", d, dur)
  }
}

Documentation

Index

Constants

View Source
const DefaultTimeout = 10 * time.Second

Variables

This section is empty.

Functions

This section is empty.

Types

type LockStep

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

Lockstep is a testing primitive.

func New

func New(t testing.TB) *LockStep

New creates a LockStep instance. The provided test context will be used for logging and for timeout failures.

func (*LockStep) Emit

func (l *LockStep) Emit(m string)

Emit will emit the message m. It will block until a corresponding Wait operation for m is processed.

func (*LockStep) SetTimeout

func (l *LockStep) SetTimeout(d time.Duration)

SetTimeout overrides DefaultTimeout for Emit and Wait operations. Increase the timeout when debugging.

func (*LockStep) SetVerbose

func (l *LockStep) SetVerbose(v bool)

SetVerbose configures verbose mode. If enabled, LockStep will emit detailed logs using t.Logf. Useful for debugging.

func (*LockStep) Wait

func (l *LockStep) Wait(ms ...string)

Wait waits for all the provided messages. It will block until Emit operations corresponding to all messages have been processed.

The order of Emit operations does not matter.

ls.Wait("x", "y")

This Wait will be fulfilled if x and y are emitted in any order. Conversely:

ls.Wait("x")
ls.Wait("y")

This Wait will only be fulfilled if x and y are emitted in order.

Jump to

Keyboard shortcuts

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