blob: ab20e5555a18eaf883de19a88813ec9df962269e [file] [log] [blame]
iomodocf795602021-03-07 23:14:15 +04001package server
2
3import (
4 "testing"
5 "time"
6
7 "github.com/giolekva/pcloud/core/kg/app"
iomodo3b0b9512021-03-15 00:13:06 +04008 "github.com/giolekva/pcloud/core/kg/common"
iomodocf795602021-03-07 23:14:15 +04009 "github.com/giolekva/pcloud/core/kg/log"
10 "github.com/giolekva/pcloud/core/kg/model"
11)
12
13type MockServer struct {
iomodo3b0b9512021-03-15 00:13:06 +040014 App common.AppIface
iomodocf795602021-03-07 23:14:15 +040015 Servers []Server
16 Config *model.Config
17}
18
19func Setup(tb testing.TB) *MockServer {
20 if testing.Short() {
21 tb.SkipNow()
22 }
23 app := app.NewTestApp()
24 config := model.NewConfig()
25 logger := &log.NoOpLogger{}
26 grpcServer := NewGRPCServer(logger, config, app)
iomodoa19d4792021-03-26 00:27:25 +040027 httpServer := NewHTTPServer(logger, config, app)
iomodocf795602021-03-07 23:14:15 +040028 ts := &MockServer{
29 App: app,
30 Servers: []Server{grpcServer, httpServer},
31 Config: config,
32 }
iomodo838bd5c2021-03-21 15:37:03 +040033 go grpcServer.Start() // nolint:errcheck
34 go httpServer.Start() // nolint:errcheck
iomodocf795602021-03-07 23:14:15 +040035 time.Sleep(1 * time.Second)
36 return ts
37}
38
39func (ts *MockServer) ShutdownServers() {
40 done := make(chan bool)
41 go func() {
42 for _, server := range ts.Servers {
iomodo838bd5c2021-03-21 15:37:03 +040043 server.Shutdown() // nolint:errcheck
iomodocf795602021-03-07 23:14:15 +040044 }
45 close(done)
46 }()
47
48 select {
49 case <-done:
50 case <-time.After(30 * time.Second):
51 // panic instead of fatal to terminate all tests in this package, otherwise the
52 // still running server could spuriously fail subsequent tests.
53 panic("failed to shutdown server within 30 seconds")
54 }
55}