Skip to content

Commit

Permalink
Fix for fetcher warning at server startup (#1914)
Browse files Browse the repository at this point in the history
Co-authored-by: Veronika Solovei <[email protected]>
  • Loading branch information
VeronikaSolovei9 and Veronika Solovei authored Jul 15, 2021
1 parent 09291bb commit aee52e3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 5 deletions.
3 changes: 3 additions & 0 deletions stored_requests/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ func newFetcher(cfg *config.StoredRequests, client *http.Client, db *sql.DB) (fe
if cfg.Postgres.FetcherQueries.QueryTemplate != "" {
glog.Infof("Loading Stored %s data via Postgres.\nQuery: %s", cfg.DataType(), cfg.Postgres.FetcherQueries.QueryTemplate)
idList = append(idList, db_fetcher.NewFetcher(db, cfg.Postgres.FetcherQueries.MakeQuery))
} else if cfg.Postgres.CacheInitialization.Query != "" && cfg.Postgres.PollUpdates.Query != "" {
//in this case data will be loaded to cache via poll for updates event
idList = append(idList, empty_fetcher.EmptyFetcher{})
}
if cfg.HTTP.Endpoint != "" {
glog.Infof("Loading Stored %s data via HTTP. endpoint=%s", cfg.DataType(), cfg.HTTP.Endpoint)
Expand Down
78 changes: 73 additions & 5 deletions stored_requests/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"context"
"database/sql"
"encoding/json"
"errors"
"net/http"
Expand Down Expand Up @@ -41,12 +42,79 @@ func isMemoryCacheType(cache stored_requests.CacheJSON) bool {
}

func TestNewEmptyFetcher(t *testing.T) {
fetcher := newFetcher(&config.StoredRequests{}, nil, nil)
if fetcher == nil {
t.Errorf("The fetcher should be non-nil, even with an empty config.")

type testCase struct {
config *config.StoredRequests
emptyFetcher bool
description string
}
if _, ok := fetcher.(empty_fetcher.EmptyFetcher); !ok {
t.Errorf("If the config is empty, and EmptyFetcher should be returned")
testCases := []testCase{
{
config: &config.StoredRequests{},
emptyFetcher: true,
description: "If the config is empty, an EmptyFetcher should be returned",
},
{
config: &config.StoredRequests{
Postgres: config.PostgresConfig{
CacheInitialization: config.PostgresCacheInitializer{
Query: "test query",
},
PollUpdates: config.PostgresUpdatePolling{
Query: "test poll query",
},
FetcherQueries: config.PostgresFetcherQueries{
QueryTemplate: "",
},
},
},
emptyFetcher: true,
description: "If Postgres fetcher query is not defined, but Postgres Cache init query and Postgres update polling query are defined EmptyFetcher should be returned",
},
{
config: &config.StoredRequests{
Postgres: config.PostgresConfig{
CacheInitialization: config.PostgresCacheInitializer{
Query: "",
},
PollUpdates: config.PostgresUpdatePolling{
Query: "",
},
FetcherQueries: config.PostgresFetcherQueries{
QueryTemplate: "test fetcher query",
},
},
},
emptyFetcher: false,
description: "If Postgres fetcher query is defined, but Postgres Cache init query and Postgres update polling query are not defined not EmptyFetcher (DBFetcher) should be returned",
},
{
config: &config.StoredRequests{
Postgres: config.PostgresConfig{
CacheInitialization: config.PostgresCacheInitializer{
Query: "test cache query",
},
PollUpdates: config.PostgresUpdatePolling{
Query: "test poll query",
},
FetcherQueries: config.PostgresFetcherQueries{
QueryTemplate: "test fetcher query",
},
},
},
emptyFetcher: false,
description: "If Postgres fetcher query is defined and Postgres Cache init query and Postgres update polling query are defined not EmptyFetcher (DBFetcher) should be returned",
},
}

for _, test := range testCases {
fetcher := newFetcher(test.config, nil, &sql.DB{})
assert.NotNil(t, fetcher, "The fetcher should be non-nil.")
if test.emptyFetcher {
assert.Equal(t, empty_fetcher.EmptyFetcher{}, fetcher, "Empty fetcher should be returned")
} else {
assert.NotEqual(t, empty_fetcher.EmptyFetcher{}, fetcher)
}
}
}

Expand Down

0 comments on commit aee52e3

Please sign in to comment.