From 4a9e00c3b74f0ff5ec8077c76c09d47b120844fc Mon Sep 17 00:00:00 2001 From: Veronika Solovei Date: Thu, 15 Jul 2021 11:43:57 -0700 Subject: [PATCH] Fix for fetcher warning at server startup (#1914) Co-authored-by: Veronika Solovei --- stored_requests/config/config.go | 3 ++ stored_requests/config/config_test.go | 78 +++++++++++++++++++++++++-- 2 files changed, 76 insertions(+), 5 deletions(-) diff --git a/stored_requests/config/config.go b/stored_requests/config/config.go index 7f92f2521cd..f682ff932f4 100644 --- a/stored_requests/config/config.go +++ b/stored_requests/config/config.go @@ -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) diff --git a/stored_requests/config/config_test.go b/stored_requests/config/config_test.go index 6c8cd612299..4a8d10a9382 100644 --- a/stored_requests/config/config_test.go +++ b/stored_requests/config/config_test.go @@ -2,6 +2,7 @@ package config import ( "context" + "database/sql" "encoding/json" "errors" "net/http" @@ -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) + } } }