diff options
Diffstat (limited to 'internal/discovery')
| -rw-r--r-- | internal/discovery/discovery.go | 19 | ||||
| -rw-r--r-- | internal/discovery/discovery_test.go | 29 |
2 files changed, 34 insertions, 14 deletions
diff --git a/internal/discovery/discovery.go b/internal/discovery/discovery.go index daa08e8..3994aba 100644 --- a/internal/discovery/discovery.go +++ b/internal/discovery/discovery.go @@ -285,10 +285,11 @@ func (discovery *Discovery) previousServers() (*Servers, error) { } // Organizations returns the discovery organizations +// The second return value is a boolean that indicates whether a fresh list was updated internally // If there was an error, a cached copy is returned if available. -func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations, error) { +func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations, bool, error) { if !discovery.DetermineOrganizationsUpdate() { - return &discovery.OrganizationList, nil + return &discovery.OrganizationList, false, nil } file := "organization_list.json" err := discovery.file(ctx, file, discovery.OrganizationList.Version, &discovery.OrganizationList) @@ -298,30 +299,30 @@ func (discovery *Discovery) Organizations(ctx context.Context) (*Organizations, if perr != nil { log.Logger.Warningf("failed to get previous discovery organizations: %v", perr) } - return orgs, err + return orgs, false, err } discovery.OrganizationList.Timestamp = time.Now() - return &discovery.OrganizationList, nil + return &discovery.OrganizationList, true, nil } // Servers returns the discovery servers +// The second return value is a boolean that indicates whether a fresh list was updated internally // If there was an error, a cached copy is returned if available. -func (discovery *Discovery) Servers(ctx context.Context) (*Servers, error) { +func (discovery *Discovery) Servers(ctx context.Context) (*Servers, bool, error) { if !discovery.DetermineServersUpdate() { - return &discovery.ServerList, nil + return &discovery.ServerList, false, nil } file := "server_list.json" err := discovery.file(ctx, file, discovery.ServerList.Version, &discovery.ServerList) if err != nil { // Return previous with an error - // TODO: Log here if we fail to get previous srvs, perr := discovery.previousServers() if perr != nil { log.Logger.Warningf("failed to get previous discovery servers: %v", perr) } - return srvs, err + return srvs, false, err } // Update servers timestamp discovery.ServerList.Timestamp = time.Now() - return &discovery.ServerList, nil + return &discovery.ServerList, true, nil } diff --git a/internal/discovery/discovery_test.go b/internal/discovery/discovery_test.go index 03ea109..42191be 100644 --- a/internal/discovery/discovery_test.go +++ b/internal/discovery/discovery_test.go @@ -23,7 +23,10 @@ func TestServers(t *testing.T) { } d := &Discovery{httpClient: c} // get servers - s1, err := d.Servers(context.Background()) + s1, fresh, err := d.Servers(context.Background()) + if !fresh { + t.Fatalf("Did not obtain the server list fresh") + } if err != nil { t.Fatalf("Failed getting servers: %v", err) } @@ -31,8 +34,11 @@ func TestServers(t *testing.T) { // Shutdown the server s.Close() // Test if we get the same cached copy - s2, err := d.Servers(context.Background()) + s2, fresh, err := d.Servers(context.Background()) // We should not get an error as the timestamp is not expired + if fresh { + t.Fatalf("The server list was obtained fresh") + } if err != nil { t.Fatalf("Got a servers error after shutting down server: %v", err) } @@ -41,9 +47,13 @@ func TestServers(t *testing.T) { } // Force expired, 1 hour in the past + // we should return the previous with an error d.ServerList.Timestamp = time.Now().Add(-1 * time.Hour) - s3, err := d.Servers(context.Background()) + s3, fresh, err := d.Servers(context.Background()) + if fresh { + t.Fatalf("Server list was gotten fresh") + } // Now we expect an error with the cached copy if err == nil { t.Fatalf("Got a servers nil error after shutting down file server and expired") @@ -65,16 +75,25 @@ func TestOrganizations(t *testing.T) { } d := &Discovery{httpClient: c} // get servers - s1, err := d.Organizations(context.Background()) + s1, fresh, err := d.Organizations(context.Background()) + if !fresh { + t.Fatalf("The organization list was not obtained fresh") + } if err != nil { t.Fatalf("Failed getting organizations: %v", err) } + if !fresh { + t.Fatalf("Did not get a fresh organization list") + } // Shutdown the server s.Close() // Test if we get the same cached copy // We should not get an error as the timestamp is not zero - s2, err := d.Organizations(context.Background()) + s2, fresh, err := d.Organizations(context.Background()) + if fresh { + t.Fatalf("The organization list is freshly obtained") + } if err != nil { t.Fatalf("Got an organizations error after shutting down file server: %v", err) } |
