| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "testing" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/giolekva/pcloud/core/kg/app" |
| iomodo | 3b0b951 | 2021-03-15 00:13:06 +0400 | [diff] [blame] | 8 | "github.com/giolekva/pcloud/core/kg/common" |
| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 9 | "github.com/giolekva/pcloud/core/kg/log" |
| 10 | "github.com/giolekva/pcloud/core/kg/model" |
| 11 | ) |
| 12 | |
| 13 | type MockServer struct { |
| iomodo | 3b0b951 | 2021-03-15 00:13:06 +0400 | [diff] [blame] | 14 | App common.AppIface |
| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 15 | Servers []Server |
| 16 | Config *model.Config |
| 17 | } |
| 18 | |
| 19 | func 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) |
| iomodo | a19d479 | 2021-03-26 00:27:25 +0400 | [diff] [blame] | 27 | httpServer := NewHTTPServer(logger, config, app) |
| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 28 | ts := &MockServer{ |
| 29 | App: app, |
| 30 | Servers: []Server{grpcServer, httpServer}, |
| 31 | Config: config, |
| 32 | } |
| iomodo | 838bd5c | 2021-03-21 15:37:03 +0400 | [diff] [blame] | 33 | go grpcServer.Start() // nolint:errcheck |
| 34 | go httpServer.Start() // nolint:errcheck |
| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 35 | time.Sleep(1 * time.Second) |
| 36 | return ts |
| 37 | } |
| 38 | |
| 39 | func (ts *MockServer) ShutdownServers() { |
| 40 | done := make(chan bool) |
| 41 | go func() { |
| 42 | for _, server := range ts.Servers { |
| iomodo | 838bd5c | 2021-03-21 15:37:03 +0400 | [diff] [blame] | 43 | server.Shutdown() // nolint:errcheck |
| iomodo | cf79560 | 2021-03-07 23:14:15 +0400 | [diff] [blame] | 44 | } |
| 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 | } |