fetcher.Run(ctx, conf, since) becomes New(&conf.Sources, opts...) and Run(ctx, since). Each worker now takes only its own source's config, and Run starts each one through a closure that binds it. Cancelling ctx stops the workers through the Fetcher's background context, and Run always waits for every worker before returning their joined errors. The findatender client no longer holds the configuration: New takes only the API root, ReloadConfig is gone, and request tracing follows logging.IsDebugEnabled(). The FindAOpportunity worker is now FindATender. job-search serve runs the fetcher alongside the HTTP server. A source that fails stops the server, and the server stopping stops the sources; with no sources enabled the server keeps running. fetcher service and fetcher fts-once use the new APIs.
127 lines
2.7 KiB
Go
127 lines
2.7 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(time.Now(), false)
|
|
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(time.Now(), false); !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(time.Now(), false)
|
|
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(time.Now(), false); err == nil {
|
|
t.Error("expected an error")
|
|
}
|
|
}
|