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
- Host: GitHub
- URL: https://github.com/elgohr/go-await
- Owner: elgohr
- License: mit
- Created: 2019-01-01T12:29:24.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-04T06:32:01.000Z (about 7 years ago)
- Last Synced: 2024-05-01T12:35:20.469Z (about 2 years ago)
- Topics: await, concurrency, go, golang, testing, testing-tools
- Language: Go
- Homepage:
- Size: 6.84 KB
- Stars: 0
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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)
}
```