asyncwait

package module
v0.0.0-...-2a68517 Latest Latest
Warning

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

Go to latest
Published: May 21, 2025 License: Apache-2.0 Imports: 1 Imported by: 0

README

AsyncWait

Wait, don't sleep :)

Build Status

Helps in testing async methods in your Go program
If you need testing your async method, wait rather than sleep

Uses:

  • Helps avoid undeterministic tests
  • Helps have faster tests, because you wait enough (No more or less :))

Usage

go get github.com/rShetty/asyncwait
or, just add `github.com/rShetty/asyncwait` as dependency and preferably fix a version.

Code:

func doWork(workChan chan int, something Something)
  go func() {
    for i := 0; i < 5; i++ {
      workChan <- i
    }
  }

  something.call()
}
The above code pushes 5 int elements into a channel and the aim is to test this async job

Test:

workChan := make(chan int)
maxWaitInMillis := 100
pollIntervalInMillis := 20

doWork(workChan)

// Check for the length of channel to be 5
predicate := func() bool {
  return len(workChan) == 5 
}

successful := NewAsyncWait(maxWaitInMillis, pollIntervalInMillis).Check(predicate)

if successful != true {
  // You fail
}

// Assert call was called on something

Working examples of usage can be found here

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AsyncWait

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

AsyncWait Represents asyncwait

func NewAsyncWait

func NewAsyncWait(maxWaitInMillis int, pollIntervalInMillis int) AsyncWait

NewAsyncWait Creates a new instance of AsyncWait with maxWait and pollInterval as inputs in millis

func (AsyncWait) Check

func (aw AsyncWait) Check(predicate func() bool) bool

Check waits for specified maxWaitInMillis and polls every pollIntervalInMillis for the predicate truthiness

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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