Skip to content

Commit 96f0843

Browse files
committed
Adding new function to factor out server creation, and also added it to problematic areas of the tests
1 parent b999452 commit 96f0843

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

cmd/frontend/main.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func main() {
9292
Addr: cfg.RedisHAHost + ":" + cfg.RedisHAPort,
9393
})
9494
}
95-
server, err := frontend.NewServer(frontend.ServerConfig{
95+
config := frontend.ServerConfig{
9696
DataSource: ds,
9797
Queue: fetchQueue,
9898
CompletionClient: haClient,
@@ -101,9 +101,6 @@ func main() {
101101
ThirdPartyPath: *thirdPartyPath,
102102
DevMode: *devMode,
103103
AppVersionLabel: cfg.AppVersionLabel(),
104-
})
105-
if err != nil {
106-
log.Fatalf(ctx, "frontend.NewServer: %v", err)
107104
}
108105
router := dcensus.NewRouter(frontend.TagRoute)
109106
var cacheClient *redis.Client
@@ -112,7 +109,10 @@ func main() {
112109
Addr: cfg.RedisCacheHost + ":" + cfg.RedisCachePort,
113110
})
114111
}
115-
server.Install(router.Handle, cacheClient)
112+
server, err := frontend.CreateAndInstallServer(config, router.Handle, cacheClient)
113+
if err != nil {
114+
log.Fatalf(ctx, "frontend.NewServer: %v", err)
115+
}
116116
views := append(dcensus.ServerViews,
117117
postgres.SearchLatencyDistribution,
118118
postgres.SearchResponseCount,

internal/frontend/server.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,3 +449,18 @@ func parsePageTemplates(base template.TrustedSource) (map[string]*template.Templ
449449
}
450450
return templates, nil
451451
}
452+
453+
// CreateAndInstallServer creates a new server object, and installs and registers the routes given to it.
454+
func CreateAndInstallServer(config ServerConfig, handle func(string, http.Handler), redisClient *redis.Client)(*Server, error){
455+
server, err := NewServer(config)
456+
if err != nil{
457+
return nil, err
458+
}
459+
if redisClient != nil{
460+
server.Install(handle, redisClient)
461+
}else{
462+
server.Install(handle, nil)
463+
}
464+
465+
return server, nil
466+
}

internal/frontend/server_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -997,19 +997,19 @@ func newTestServer(t *testing.T, proxyModules []*proxy.TestModule, experimentNam
997997
return FetchAndUpdateState(ctx, mpath, version, proxyClient, sourceClient, testDB)
998998
})
999999

1000-
s, err := NewServer(ServerConfig{
1000+
config := ServerConfig{
10011001
DataSource: testDB,
10021002
Queue: q,
10031003
TaskIDChangeInterval: 10 * time.Minute,
10041004
StaticPath: template.TrustedSourceFromConstant("../../content/static"),
10051005
ThirdPartyPath: "../../third_party",
10061006
AppVersionLabel: "",
1007-
})
1007+
}
1008+
mux := http.NewServeMux()
1009+
s, err := CreateAndInstallServer(config, mux.Handle, nil)
10081010
if err != nil {
10091011
t.Fatal(err)
10101012
}
1011-
mux := http.NewServeMux()
1012-
s.Install(mux.Handle, nil)
10131013

10141014
var exps []*internal.Experiment
10151015
for _, n := range experimentNames {

internal/testing/integration/frontend_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,19 +170,20 @@ func TestModulePackageDirectoryResolution(t *testing.T) {
170170
// duplication
171171
func setupFrontend(ctx context.Context, t *testing.T, q queue.Queue) *httptest.Server {
172172
t.Helper()
173-
s, err := frontend.NewServer(frontend.ServerConfig{
173+
config := frontend.ServerConfig{
174174
DataSource: testDB,
175175
TaskIDChangeInterval: 10 * time.Minute,
176176
StaticPath: template.TrustedSourceFromConstant("../../../content/static"),
177177
ThirdPartyPath: "../../../third_party",
178178
AppVersionLabel: "",
179179
Queue: q,
180180
})
181+
182+
mux := http.NewServeMux()
183+
s, err := frontend.CreateAndInstallServer(config, mux.Handle, nil)
181184
if err != nil {
182185
t.Fatal(err)
183186
}
184-
mux := http.NewServeMux()
185-
s.Install(mux.Handle, nil)
186187

187188
experimenter, err := middleware.NewExperimenter(ctx, 1*time.Minute, testDB)
188189
if err != nil {

0 commit comments

Comments
 (0)