-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from mailgun/thrawn/develop
Added testutil.UntilPass and testutil.UntilConnect
- Loading branch information
Showing
5 changed files
with
222 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package testutil | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type TestingT interface { | ||
Errorf(format string, args ...interface{}) | ||
FailNow() | ||
} | ||
|
||
type TestResults struct { | ||
T TestingT | ||
Failures []string | ||
} | ||
|
||
func (s *TestResults) Errorf(format string, args ...interface{}) { | ||
s.Failures = append(s.Failures, fmt.Sprintf(format, args...)) | ||
} | ||
|
||
func (s *TestResults) FailNow() { | ||
s.Report(s.T) | ||
s.T.FailNow() | ||
} | ||
|
||
func (s *TestResults) Report(t TestingT) { | ||
for _, failure := range s.Failures { | ||
t.Errorf(failure) | ||
} | ||
} | ||
|
||
// Return true if the test eventually passed, false if the test failed | ||
func UntilPass(t TestingT, attempts int, duration time.Duration, callback func(t TestingT)) bool { | ||
results := TestResults{T: t} | ||
|
||
for i := 0; i < attempts; i++ { | ||
// Clear the failures before each attempt | ||
results.Failures = nil | ||
|
||
// Run the tests in the callback | ||
callback(&results) | ||
|
||
// If the test had no failures | ||
if len(results.Failures) == 0 { | ||
return true | ||
} | ||
// Sleep the duration | ||
time.Sleep(duration) | ||
} | ||
// We have exhausted our attempts and should report the failures and exit | ||
results.Report(t) | ||
return false | ||
} | ||
|
||
// Continues to attempt connecting to the specified tcp address until either | ||
// a successful connect or attempts are exhausted | ||
func UntilConnect(t TestingT, a int, d time.Duration, addr string) { | ||
for i := 0; i < a; i++ { | ||
conn, err := net.Dial("tcp", addr) | ||
if err != nil { | ||
continue | ||
} | ||
conn.Close() | ||
// Sleep the duration | ||
time.Sleep(d) | ||
return | ||
} | ||
t.Errorf("never connected to TCP server at '%s' after %d attempts", addr, a) | ||
t.FailNow() | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package testutil_test | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"math/rand" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/mailgun/holster/v3/testutil" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"golang.org/x/net/nettest" | ||
) | ||
|
||
func TestUntilConnect(t *testing.T) { | ||
ln, err := nettest.NewLocalListener("tcp") | ||
require.NoError(t, err) | ||
|
||
go func() { | ||
cn, err := ln.Accept() | ||
require.NoError(t, err) | ||
cn.Close() | ||
}() | ||
// Wait until we can connect, then continue with the test | ||
testutil.UntilConnect(t, 10, time.Millisecond*100, ln.Addr().String()) | ||
} | ||
|
||
func TestUntilPass(t *testing.T) { | ||
rand.Seed(time.Now().UnixNano()) | ||
var value string | ||
|
||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.Method == http.MethodPost { | ||
// Sleep some rand amount to time to simulate some | ||
// async process happening on the server | ||
time.Sleep(time.Duration(rand.Intn(10))*time.Millisecond) | ||
// Set the value | ||
value = r.FormValue("value") | ||
} else { | ||
fmt.Fprintln(w, value) | ||
} | ||
})) | ||
defer ts.Close() | ||
|
||
// Start the async process that produces a value on the server | ||
http.PostForm(ts.URL, url.Values{"value": []string{"batch job completed"}}) | ||
|
||
// Keep running this until the batch job completes or attempts are exhausted | ||
testutil.UntilPass(t, 10, time.Millisecond*100, func(t testutil.TestingT) { | ||
r, err := http.Get(ts.URL) | ||
|
||
// use of `require` will abort the current test here and tell UntilPass() to | ||
// run again after 100 milliseconds | ||
require.NoError(t, err) | ||
|
||
// Or you can check if the assert returned true or not | ||
if !assert.Equal(t, 200, r.StatusCode) { | ||
return | ||
} | ||
|
||
b, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, "batch job completed\n", string(b)) | ||
}) | ||
} |