Backfilling a long window (an early --since, or a long time offline) hit the API's rate limit, which could still be exceeded after waiting once, and then everything fetched so far was lost. Before each Find a Tender source's worker starts, a start point more than 7 days ago is now backfilled in 7-day windows bounded on both sides, oldest first, each saved before the next is fetched. A window that fails is saved as far as it got and fetched again from its start after the source's interval. The worker then carries on from the end of the last window. A 429 is now waited out up to sources.<source>.rate_limit_retries times (default 5) rather than once; fts-once --ignore-rate-limit uses 0. FetchAll returns what it fetched before an error along with it, and the workers save it, so a failed run no longer loses its data. findatender's FetchAll and FetchOnce take RequestOptions, which gains Until (updatedTo), and RateLimitRetries replaces AllowRateLimit.
135 lines
2.8 KiB
Go
135 lines
2.8 KiB
Go
package findatender
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"git.avsdev.uk/Go/job-search/internal/types"
|
|
)
|
|
|
|
func TestNew(t *testing.T) {
|
|
ft := New("https://example/api")
|
|
defer ft.Shutdown()
|
|
|
|
if ft.apiRoot != "https://example/api" {
|
|
t.Errorf("New did not keep its API root: %+v", ft)
|
|
}
|
|
if ft.httpClient == nil {
|
|
t.Fatal("no http client")
|
|
}
|
|
if ft.httpClient.Timeout() != 60*time.Second ||
|
|
ft.httpClient.RetryCount() != 1 {
|
|
t.Errorf(
|
|
"client timeout %v, retries %d",
|
|
ft.httpClient.Timeout(),
|
|
ft.httpClient.RetryCount(),
|
|
)
|
|
}
|
|
if ft.ctx != nil {
|
|
t.Error("New should not set a context")
|
|
}
|
|
}
|
|
|
|
func TestWith(t *testing.T) {
|
|
ft := New("")
|
|
defer ft.Shutdown()
|
|
|
|
// Passing nil on purpose to test that case.
|
|
//nolint:staticcheck // SA1012
|
|
if got := ft.With(nil); got != ft {
|
|
t.Error("With(nil) should return the receiver")
|
|
}
|
|
|
|
type key struct{}
|
|
ctx := context.WithValue(context.Background(), key{}, "v")
|
|
got := ft.With(ctx)
|
|
if got == ft {
|
|
t.Fatal("With should return a copy")
|
|
}
|
|
if got.ctx != ctx {
|
|
t.Error("copy does not carry the context")
|
|
}
|
|
if ft.ctx != nil {
|
|
t.Error("With changed the original")
|
|
}
|
|
if got.httpClient != ft.httpClient {
|
|
t.Error("copy should share the client")
|
|
}
|
|
}
|
|
|
|
func opportunityIDs(ops []*types.Opportunity) []string {
|
|
var ids []string
|
|
for _, op := range ops {
|
|
ids = append(ids, op.UUID)
|
|
}
|
|
slices.Sort(ids)
|
|
return ids
|
|
}
|
|
|
|
func TestFetchOncePublic(t *testing.T) {
|
|
ft, _ := pagedServer(
|
|
t,
|
|
[]string{
|
|
releaseJSON("ocds-1", "1"),
|
|
releaseJSON("ocds-1", "2"),
|
|
releaseJSON("ocds-2", "1"),
|
|
},
|
|
[]string{releaseJSON("ocds-3", "1")},
|
|
)
|
|
ops, err := ft.FetchOnce(RequestOptions{
|
|
Since: time.Now(),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(opportunityIDs(ops), ","); got != "ocds-1,ocds-2" {
|
|
t.Errorf("opportunities = %s", got)
|
|
}
|
|
|
|
ft, _ = pagedServer(t, []string{})
|
|
if _, err := ft.FetchOnce(RequestOptions{
|
|
Since: time.Now(),
|
|
}); !errors.Is(err, ErrNoReleases) {
|
|
t.Errorf("err = %v, want ErrNoReleases", err)
|
|
}
|
|
}
|
|
|
|
func TestFetchAllPublic(t *testing.T) {
|
|
ft, _ := pagedServer(
|
|
t,
|
|
[]string{releaseJSON("ocds-1", "1"), releaseJSON("ocds-2", "1")},
|
|
[]string{releaseJSON("ocds-1", "2"), releaseJSON("ocds-3", "1")},
|
|
)
|
|
ops, err := ft.FetchAll(RequestOptions{
|
|
Since: time.Now(),
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := strings.Join(
|
|
opportunityIDs(ops), ",",
|
|
); got != "ocds-1,ocds-2,ocds-3" {
|
|
t.Errorf("opportunities = %s", got)
|
|
}
|
|
for _, op := range ops {
|
|
if op.UUID == "ocds-1" && len(op.Releases) != 2 {
|
|
t.Errorf("ocds-1 has %d releases, want 2", len(op.Releases))
|
|
}
|
|
}
|
|
|
|
ft, _ = newTestFT(t, false, func(w http.ResponseWriter, r *http.Request) {
|
|
io.WriteString(w, "not json")
|
|
})
|
|
if _, err := ft.FetchAll(RequestOptions{
|
|
Since: time.Now(),
|
|
}); err == nil {
|
|
t.Error("expected an error")
|
|
}
|
|
}
|