An open API service indexing awesome lists of open source software.

https://github.com/elgohr/go-await

Testing concurrency in Go
https://github.com/elgohr/go-await

await concurrency go golang testing testing-tools

Last synced: about 1 year ago
JSON representation

Testing concurrency in Go

Awesome Lists containing this project

README

          

# go-await
Testing concurrency in Golang

`go get -u github.com/elgohr/go-await`

## Chuck Norris doesn't sleep, he waits
I saw so many test code, where people are waiting on async execution by sleeping (time.Sleep).
This is
* not efficient, as you may sleep longer for checking values then you would need
* not consistent , as you may sleep longer than your timeout
* dangerous, as some code didn't even have timeouts

In this way I'm trying to illustrate a way to do this easily, by using standard goroutines and channels.
You may also use this as a library for not playing copy cat :-)

## Example
```
import ("github.com/elgohr/go-await/wait")

awaiting := make(chan interface{}, 1)

remoteServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
awaiting <- r.Method
w.WriteHeader(http.StatusOK)
}))
defer remoteServer.Close()

thisCouldBeYourAsyncFunction := func() {
go func() {
res, _ := http.Get(remoteServer.URL)
defer res.Body.Close()
}()
}
thisCouldBeYourAsyncFunction()

returns := wait.For(awaiting, 1*time.Second)
if returns != "GET" {
t.Errorf("Expected GET, but got %v", returns)
}
```