From 96f084308091254f89b7ba1c860b2fe99cd2f054 Mon Sep 17 00:00:00 2001 From: laches1sm Date: Wed, 8 Jul 2020 11:10:34 +0100 Subject: [PATCH 1/3] Adding new function to factor out server creation, and also added it to problematic areas of the tests --- cmd/frontend/main.go | 10 +++++----- internal/frontend/server.go | 15 +++++++++++++++ internal/frontend/server_test.go | 8 ++++---- internal/testing/integration/frontend_test.go | 7 ++++--- 4 files changed, 28 insertions(+), 12 deletions(-) diff --git a/cmd/frontend/main.go b/cmd/frontend/main.go index 7c4f0fb7d..f7a82ee34 100644 --- a/cmd/frontend/main.go +++ b/cmd/frontend/main.go @@ -92,7 +92,7 @@ func main() { Addr: cfg.RedisHAHost + ":" + cfg.RedisHAPort, }) } - server, err := frontend.NewServer(frontend.ServerConfig{ + config := frontend.ServerConfig{ DataSource: ds, Queue: fetchQueue, CompletionClient: haClient, @@ -101,9 +101,6 @@ func main() { ThirdPartyPath: *thirdPartyPath, DevMode: *devMode, AppVersionLabel: cfg.AppVersionLabel(), - }) - if err != nil { - log.Fatalf(ctx, "frontend.NewServer: %v", err) } router := dcensus.NewRouter(frontend.TagRoute) var cacheClient *redis.Client @@ -112,7 +109,10 @@ func main() { Addr: cfg.RedisCacheHost + ":" + cfg.RedisCachePort, }) } - server.Install(router.Handle, cacheClient) + server, err := frontend.CreateAndInstallServer(config, router.Handle, cacheClient) + if err != nil { + log.Fatalf(ctx, "frontend.NewServer: %v", err) + } views := append(dcensus.ServerViews, postgres.SearchLatencyDistribution, postgres.SearchResponseCount, diff --git a/internal/frontend/server.go b/internal/frontend/server.go index cc54d5765..bd699f69e 100644 --- a/internal/frontend/server.go +++ b/internal/frontend/server.go @@ -449,3 +449,18 @@ func parsePageTemplates(base template.TrustedSource) (map[string]*template.Templ } return templates, nil } + +// CreateAndInstallServer creates a new server object, and installs and registers the routes given to it. +func CreateAndInstallServer(config ServerConfig, handle func(string, http.Handler), redisClient *redis.Client)(*Server, error){ + server, err := NewServer(config) + if err != nil{ + return nil, err + } + if redisClient != nil{ + server.Install(handle, redisClient) + }else{ + server.Install(handle, nil) + } + + return server, nil +} diff --git a/internal/frontend/server_test.go b/internal/frontend/server_test.go index fb67bb12f..45147b42e 100644 --- a/internal/frontend/server_test.go +++ b/internal/frontend/server_test.go @@ -997,19 +997,19 @@ func newTestServer(t *testing.T, proxyModules []*proxy.TestModule, experimentNam return FetchAndUpdateState(ctx, mpath, version, proxyClient, sourceClient, testDB) }) - s, err := NewServer(ServerConfig{ + config := ServerConfig{ DataSource: testDB, Queue: q, TaskIDChangeInterval: 10 * time.Minute, StaticPath: template.TrustedSourceFromConstant("../../content/static"), ThirdPartyPath: "../../third_party", AppVersionLabel: "", - }) + } + mux := http.NewServeMux() + s, err := CreateAndInstallServer(config, mux.Handle, nil) if err != nil { t.Fatal(err) } - mux := http.NewServeMux() - s.Install(mux.Handle, nil) var exps []*internal.Experiment for _, n := range experimentNames { diff --git a/internal/testing/integration/frontend_test.go b/internal/testing/integration/frontend_test.go index f8c974e9f..b2c816733 100644 --- a/internal/testing/integration/frontend_test.go +++ b/internal/testing/integration/frontend_test.go @@ -170,7 +170,7 @@ func TestModulePackageDirectoryResolution(t *testing.T) { // duplication func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.Server { t.Helper() - s, err := frontend.NewServer(frontend.ServerConfig{ + config := frontend.ServerConfig{ DataSource: testDB, TaskIDChangeInterval: 10 * time.Minute, StaticPath: template.TrustedSourceFromConstant("../../../content/static"), @@ -178,11 +178,12 @@ func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.S AppVersionLabel: "", Queue: q, }) + + mux := http.NewServeMux() + s, err := frontend.CreateAndInstallServer(config, mux.Handle, nil) if err != nil { t.Fatal(err) } - mux := http.NewServeMux() - s.Install(mux.Handle, nil) experimenter, err := middleware.NewExperimenter(ctx, 1*time.Minute, testDB) if err != nil { From 28c961048678f946e51b0d5334351a9231cf2fe5 Mon Sep 17 00:00:00 2001 From: laches1sm Date: Wed, 8 Jul 2020 11:18:04 +0100 Subject: [PATCH 2/3] Removed TODO comment --- internal/testing/integration/frontend_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/testing/integration/frontend_test.go b/internal/testing/integration/frontend_test.go index b2c816733..b595bc98d 100644 --- a/internal/testing/integration/frontend_test.go +++ b/internal/testing/integration/frontend_test.go @@ -166,8 +166,7 @@ func TestModulePackageDirectoryResolution(t *testing.T) { } } -// TODO(https://github.com/golang/go/issues/40096): factor out this code reduce -// duplication + func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.Server { t.Helper() config := frontend.ServerConfig{ From 904b87117e4117441f41d8bc5bfe4fe730014b8c Mon Sep 17 00:00:00 2001 From: laches1sm Date: Wed, 8 Jul 2020 11:18:40 +0100 Subject: [PATCH 3/3] Removed rouge paren --- internal/testing/integration/frontend_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/internal/testing/integration/frontend_test.go b/internal/testing/integration/frontend_test.go index b595bc98d..889c94c2a 100644 --- a/internal/testing/integration/frontend_test.go +++ b/internal/testing/integration/frontend_test.go @@ -166,7 +166,6 @@ func TestModulePackageDirectoryResolution(t *testing.T) { } } - func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.Server { t.Helper() config := frontend.ServerConfig{ @@ -176,7 +175,7 @@ func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.S ThirdPartyPath: "../../../third_party", AppVersionLabel: "", Queue: q, - }) + } mux := http.NewServeMux() s, err := frontend.CreateAndInstallServer(config, mux.Handle, nil)