From 0865e2e03651dcc7be91846a6e52125e56bcde31 Mon Sep 17 00:00:00 2001 From: Sam Anthony Date: Wed, 13 May 2026 18:28:09 -0400 Subject: poll for file validation status --- lulu_test.go | 36 ++++++++++++++---------------------- 1 file changed, 14 insertions(+), 22 deletions(-) (limited to 'lulu_test.go') diff --git a/lulu_test.go b/lulu_test.go index ceb0990..420033c 100644 --- a/lulu_test.go +++ b/lulu_test.go @@ -3,6 +3,7 @@ package lulu import ( "context" "encoding/json" + "flag" "fmt" "os" "strings" @@ -16,13 +17,10 @@ const ( clientKeyPath = "testdata/clientkey" clientSecretPath = "testdata/clientsecret" - apiKeyPage = "https://developers.sandbox.com/user-profile/api-keys" - interiorUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AACOUn3LFKsITDzylh13bQpsa/161025/thesis2.pdf?dl=1" coverUrl = "https://www.dropbox.com/sh/p3zh22vzsaegiri/AADP367j0bTWlt8fCu-_tm2ia/161025/139056_cover.pdf?dl=1" - pollTimeout = 15 * time.Second - pollPeriod = time.Second + timeout = 15 * time.Second ) var ( @@ -31,18 +29,21 @@ var ( ) func TestMain(m *testing.M) { - Debug = true + flag.BoolVar(&Debug, "d", false, "Print debug info to stderr.") + flag.Parse() + + Sandbox() if b, err := os.ReadFile(clientKeyPath); err == nil { clientKey = strings.TrimSpace(string(b)) } else { - fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client key\" from the API Keys page into %s\n%s\n", err, clientKeyPath, apiKeyPage) + fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client key\" from the API Keys page into %s\n%s\n", err, clientKeyPath, SandboxApiKeyPage) os.Exit(1) } if b, err := os.ReadFile(clientSecretPath); err == nil { clientSecret = strings.TrimSpace(string(b)) } else { - fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client secret\" from the API Keys page into %s\n%s\n", err, clientSecretPath, apiKeyPage) + fmt.Fprintf(os.Stderr, "%v\nCopy and paste your \"client secret\" from the API Keys page into %s\n%s\n", err, clientSecretPath, SandboxApiKeyPage) os.Exit(1) } @@ -86,23 +87,14 @@ func requireNotBeforeDate(t *testing.T, dt2, dt1 Date) { time.Date(y1, m1, d1, 0, 0, 0, 0, time.UTC).Format(time.DateOnly)) } -// poll periodically calls f() until it returns true or the deadline is exceeded. -func poll(t *testing.T, f func() bool) { +func tpoll(t *testing.T, f func() bool) { t.Helper() - ctx, cancel := context.WithTimeout(t.Context(), pollTimeout) + ctx, cancel := context.WithTimeout(t.Context(), timeout) defer cancel() - timer := time.NewTimer(pollPeriod) - for { - select { - case <-timer.C: - if f() { - return - } - timer.Reset(pollPeriod) - case <-ctx.Done(): - t.Errorf("timed out after %v", pollTimeout) - } - } + _, err := poll(ctx, func() (struct{}, bool, error) { + return struct{}{}, f(), nil + }) + require.NoError(t, err) } func mustParseTime(layout, value string) time.Time { -- cgit v1.2.3