test: TestFXStateDoesNotWait waits on the state, not a 1s bar, so a busy machine doesn't turn it red
TestFXStateDoesNotWait held the rate's endpoint and required /api/state to come back within one second. Under load (~100-200 here) the state took longer than that on its own and the test went red with nothing waiting on the endpoint (seen in the full run of e085d6e92; it passed 5 of 5 alone on that tree and on main252bc61ce). The endpoint now answers only once the first look is back, so that look coming back at all is the proof that the state doesn't wait on it; the guard is a minute, and the new-rate loop's deadline is a minute too. A sync.Once releases the endpoint from the test and its cleanup alike. Run at this head (252bc61ce+ this change): - broken by hand, red both ways: currentFX on fx.Get with a 4s timeout -> "the new rate never came in"; fx.Get(context.Background()) -> "the state waited on the rate's endpoint" - go test -race -count=10 -cpu 1,2 -run TestFXStateDoesNotWait ./internal/gui: ok - go test -tags nogui -timeout 45m ./...: 64 packages ok, internal/gui ok; internal/agent (#2544) and internal/gateway (#2108, #2191) timed out at 45m at load ~190. Only internal/gui's test file changed, and neither package imports it. - gofmt -l clean; GOOS=linux and GOOS=windows go vet ./internal/gui ok
This commit is contained in:
+18
-12
@@ -6,6 +6,7 @@ import (
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -60,21 +61,22 @@ func TestFXInState(t *testing.T) {
|
||||
}
|
||||
|
||||
// The state never waits on the rate's endpoint (#541): with yesterday's
|
||||
// rate cached and the endpoint taking 3s, it answers at once with
|
||||
// yesterday's, and with the new one once that is in.
|
||||
// rate cached and the endpoint not answering, it answers with
|
||||
// yesterday's, and with the new one once that is in. The endpoint
|
||||
// answers only once the first look is back, so that look coming back
|
||||
// at all is the proof, not how long it took on a busy machine.
|
||||
func TestFXStateDoesNotWait(t *testing.T) {
|
||||
sandboxHome(t)
|
||||
fx.Reset()
|
||||
release := make(chan struct{})
|
||||
var once sync.Once
|
||||
let := func() { once.Do(func() { close(release) }) }
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case <-time.After(3 * time.Second):
|
||||
case <-release:
|
||||
}
|
||||
<-release
|
||||
w.Write([]byte(`{"result":"success","rates":{"CNY":7.31}}`))
|
||||
}))
|
||||
t.Cleanup(func() {
|
||||
close(release)
|
||||
let()
|
||||
fx.Settle()
|
||||
srv.Close()
|
||||
fx.Reset()
|
||||
@@ -89,15 +91,19 @@ func TestFXStateDoesNotWait(t *testing.T) {
|
||||
if err := settings.Save(settings.Settings{Currency: "cny"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
start := time.Now()
|
||||
got := state().FX
|
||||
if took := time.Since(start); took > time.Second {
|
||||
t.Fatalf("the state waited %v on the rate", took)
|
||||
first := make(chan fxJSON, 1)
|
||||
go func() { first <- state().FX }()
|
||||
var got fxJSON
|
||||
select {
|
||||
case got = <-first:
|
||||
case <-time.After(time.Minute):
|
||||
t.Fatal("the state waited on the rate's endpoint")
|
||||
}
|
||||
let()
|
||||
if got.Rate != 7.25 || !got.Stale {
|
||||
t.Fatalf("first look: %+v, want yesterday's 7.25, stale", got)
|
||||
}
|
||||
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(100 * time.Millisecond) {
|
||||
for deadline := time.Now().Add(time.Minute); ; time.Sleep(100 * time.Millisecond) {
|
||||
if got = state().FX; got.Rate == 7.31 && !got.Stale {
|
||||
break
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user