acme: eliminate arbitrary timeouts in tests

Fixes golang/go#57107.

Change-Id: I20b1f6ca85170c6b4731d7c7ea06f4db742526cc
Reviewed-on: https://go-review.googlesource.com/c/crypto/+/456123
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Bryan Mills <bcmills@google.com>
Reviewed-by: Roland Shoemaker <roland@golang.org>
Auto-Submit: Bryan Mills <bcmills@google.com>
This commit is contained in:
Bryan C. Mills
2022-12-08 18:21:33 +00:00
committed by Gopher Robot
parent eb2c406296
commit f495dc37d5
3 changed files with 50 additions and 97 deletions
+9 -22
View File
@@ -354,11 +354,12 @@ func TestWaitAuthorization(t *testing.T) {
}) })
} }
t.Run("context cancel", func(t *testing.T) { t.Run("context cancel", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
_, err := runWaitAuthorization(ctx, t, func(w http.ResponseWriter, r *http.Request) { _, err := runWaitAuthorization(ctx, t, func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Retry-After", "60") w.Header().Set("Retry-After", "60")
fmt.Fprintf(w, `{"status":"pending"}`) fmt.Fprintf(w, `{"status":"pending"}`)
time.AfterFunc(1*time.Millisecond, cancel)
}) })
if err == nil { if err == nil {
t.Error("err is nil") t.Error("err is nil")
@@ -373,28 +374,14 @@ func runWaitAuthorization(ctx context.Context, t *testing.T, h http.HandlerFunc)
h(w, r) h(w, r)
})) }))
defer ts.Close() defer ts.Close()
type res struct {
authz *Authorization client := &Client{
err error Key: testKey,
DirectoryURL: ts.URL,
dir: &Directory{},
KID: "some-key-id", // set to avoid lookup attempt
} }
ch := make(chan res, 1) return client.WaitAuthorization(ctx, ts.URL)
go func() {
client := &Client{
Key: testKey,
DirectoryURL: ts.URL,
dir: &Directory{},
KID: "some-key-id", // set to avoid lookup attempt
}
a, err := client.WaitAuthorization(ctx, ts.URL)
ch <- res{a, err}
}()
select {
case <-time.After(3 * time.Second):
t.Fatal("WaitAuthorization took too long to return")
case v := <-ch:
return v.authz, v.err
}
panic("runWaitAuthorization: out of select")
} }
func TestRevokeAuthorization(t *testing.T) { func TestRevokeAuthorization(t *testing.T) {
+16 -25
View File
@@ -427,18 +427,7 @@ func TestGetCertificate(t *testing.T) {
man.Client = &acme.Client{DirectoryURL: s.URL()} man.Client = &acme.Client{DirectoryURL: s.URL()}
var tlscert *tls.Certificate tlscert, err := man.GetCertificate(tt.hello)
var err error
done := make(chan struct{})
go func() {
tlscert, err = man.GetCertificate(tt.hello)
close(done)
}()
select {
case <-time.After(time.Minute):
t.Fatal("man.GetCertificate took too long to return")
case <-done:
}
if tt.expectError != "" { if tt.expectError != "" {
if err == nil { if err == nil {
t.Fatal("expected error, got certificate") t.Fatal("expected error, got certificate")
@@ -515,15 +504,12 @@ func TestGetCertificate_failedAttempt(t *testing.T) {
if _, err := man.GetCertificate(hello); err == nil { if _, err := man.GetCertificate(hello); err == nil {
t.Error("GetCertificate: err is nil") t.Error("GetCertificate: err is nil")
} }
select {
case <-time.After(5 * time.Second): <-done
t.Errorf("took too long to remove the %q state", exampleCertKey) man.stateMu.Lock()
case <-done: defer man.stateMu.Unlock()
man.stateMu.Lock() if v, exist := man.state[exampleCertKey]; exist {
defer man.stateMu.Unlock() t.Errorf("state exists for %v: %+v", exampleCertKey, v)
if v, exist := man.state[exampleCertKey]; exist {
t.Errorf("state exists for %v: %+v", exampleCertKey, v)
}
} }
} }
@@ -542,8 +528,9 @@ func TestRevokeFailedAuthz(t *testing.T) {
t.Fatal("expected GetCertificate to fail") t.Fatal("expected GetCertificate to fail")
} }
start := time.Now() logTicker := time.NewTicker(3 * time.Second)
for time.Since(start) < 3*time.Second { defer logTicker.Stop()
for {
authz, err := m.Client.GetAuthorization(context.Background(), ca.URL()+"/authz/0") authz, err := m.Client.GetAuthorization(context.Background(), ca.URL()+"/authz/0")
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@@ -551,10 +538,14 @@ func TestRevokeFailedAuthz(t *testing.T) {
if authz.Status == acme.StatusDeactivated { if authz.Status == acme.StatusDeactivated {
return return
} }
select {
case <-logTicker.C:
t.Logf("still waiting on revocations")
default:
}
time.Sleep(50 * time.Millisecond) time.Sleep(50 * time.Millisecond)
} }
t.Error("revocations took too long")
} }
func TestHTTPHandlerDefaultFallback(t *testing.T) { func TestHTTPHandlerDefaultFallback(t *testing.T) {
+25 -50
View File
@@ -177,8 +177,7 @@ func TestRFC_postKID(t *testing.T) {
})) }))
defer ts.Close() defer ts.Close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx := context.Background()
defer cancel()
cl := &Client{ cl := &Client{
Key: testKey, Key: testKey,
DirectoryURL: ts.URL, DirectoryURL: ts.URL,
@@ -316,8 +315,7 @@ func TestRFC_Register(t *testing.T) {
s.start() s.start()
defer s.close() defer s.close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx := context.Background()
defer cancel()
cl := &Client{ cl := &Client{
Key: testKeyEC, Key: testKeyEC,
DirectoryURL: s.url("/"), DirectoryURL: s.url("/"),
@@ -454,8 +452,7 @@ func TestRFC_RegisterExternalAccountBinding(t *testing.T) {
s.start() s.start()
defer s.close() defer s.close()
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) ctx := context.Background()
defer cancel()
cl := &Client{ cl := &Client{
Key: testKeyEC, Key: testKeyEC,
DirectoryURL: s.url("/"), DirectoryURL: s.url("/"),
@@ -867,24 +864,13 @@ func testWaitOrderStatus(t *testing.T, okStatus string) {
s.start() s.start()
defer s.close() defer s.close()
var order *Order cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
var err error order, err := cl.WaitOrder(context.Background(), s.url("/orders/1"))
done := make(chan struct{}) if err != nil {
go func() { t.Fatalf("WaitOrder: %v", err)
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")} }
order, err = cl.WaitOrder(context.Background(), s.url("/orders/1")) if order.Status != okStatus {
close(done) t.Errorf("order.Status = %q; want %q", order.Status, okStatus)
}()
select {
case <-time.After(3 * time.Second):
t.Fatal("WaitOrder took too long to return")
case <-done:
if err != nil {
t.Fatalf("WaitOrder: %v", err)
}
if order.Status != okStatus {
t.Errorf("order.Status = %q; want %q", order.Status, okStatus)
}
} }
} }
@@ -909,30 +895,20 @@ func TestRFC_WaitOrderError(t *testing.T) {
s.start() s.start()
defer s.close() defer s.close()
var err error cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
done := make(chan struct{}) _, err := cl.WaitOrder(context.Background(), s.url("/orders/1"))
go func() { if err == nil {
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")} t.Fatal("WaitOrder returned nil error")
_, err = cl.WaitOrder(context.Background(), s.url("/orders/1")) }
close(done) e, ok := err.(*OrderError)
}() if !ok {
select { t.Fatalf("err = %v (%T); want OrderError", err, err)
case <-time.After(3 * time.Second): }
t.Fatal("WaitOrder took too long to return") if e.OrderURL != s.url("/orders/1") {
case <-done: t.Errorf("e.OrderURL = %q; want %q", e.OrderURL, s.url("/orders/1"))
if err == nil { }
t.Fatal("WaitOrder returned nil error") if e.Status != StatusInvalid {
} t.Errorf("e.Status = %q; want %q", e.Status, StatusInvalid)
e, ok := err.(*OrderError)
if !ok {
t.Fatalf("err = %v (%T); want OrderError", err, err)
}
if e.OrderURL != s.url("/orders/1") {
t.Errorf("e.OrderURL = %q; want %q", e.OrderURL, s.url("/orders/1"))
}
if e.Status != StatusInvalid {
t.Errorf("e.Status = %q; want %q", e.Status, StatusInvalid)
}
} }
} }
@@ -972,8 +948,7 @@ func TestRFC_CreateOrderCert(t *testing.T) {
}) })
s.start() s.start()
defer s.close() defer s.close()
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) ctx := context.Background()
defer cancel()
cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")} cl := &Client{Key: testKeyEC, DirectoryURL: s.url("/")}
cert, curl, err := cl.CreateOrderCert(ctx, s.url("/pleaseissue"), csr, true) cert, curl, err := cl.CreateOrderCert(ctx, s.url("/pleaseissue"), csr, true)