NewTestServer is a simplified API to start and stop a test server.
(t *testing.T, handler Handler, logger log.Logger, config *config2.SSHConfig)
| 14 | |
| 15 | // NewTestServer is a simplified API to start and stop a test server. |
| 16 | func NewTestServer(t *testing.T, handler Handler, logger log.Logger, config *config2.SSHConfig) TestServer { |
| 17 | if config == nil { |
| 18 | config = &config2.SSHConfig{} |
| 19 | structutils.Defaults(config) |
| 20 | } |
| 21 | |
| 22 | port := test.GetNextPort(t, "SSH") |
| 23 | config.Listen = fmt.Sprintf("127.0.0.1:%d", port) |
| 24 | if err := config.GenerateHostKey(); err != nil { |
| 25 | panic(err) |
| 26 | } |
| 27 | svc, err := New(*config, handler, logger) |
| 28 | if err != nil { |
| 29 | panic(err) |
| 30 | } |
| 31 | lifecycle := service.NewLifecycle(svc) |
| 32 | started := make(chan struct{}) |
| 33 | lifecycle.OnRunning( |
| 34 | func(s service.Service, l service.Lifecycle) { |
| 35 | started <- struct{}{} |
| 36 | }) |
| 37 | |
| 38 | t.Cleanup(func() { |
| 39 | lifecycle.Stop(context.Background()) |
| 40 | _ = lifecycle.Wait() |
| 41 | }) |
| 42 | |
| 43 | return &testServerImpl{ |
| 44 | config: *config, |
| 45 | lifecycle: lifecycle, |
| 46 | started: started, |
| 47 | } |
| 48 | } |