|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/opencsgs/csghub-lite/internal/config" |
| 11 | +) |
| 12 | + |
| 13 | +func TestRestartBackgroundServiceRestartsAndWaits(t *testing.T) { |
| 14 | + oldStop := stopBackgroundServiceForRestart |
| 15 | + oldStart := startBackgroundServerForRestart |
| 16 | + oldWait := waitForServerForRestart |
| 17 | + defer func() { |
| 18 | + stopBackgroundServiceForRestart = oldStop |
| 19 | + startBackgroundServerForRestart = oldStart |
| 20 | + waitForServerForRestart = oldWait |
| 21 | + }() |
| 22 | + |
| 23 | + cfg := &config.Config{ListenAddr: ":14567"} |
| 24 | + var calls []string |
| 25 | + |
| 26 | + stopBackgroundServiceForRestart = func() error { |
| 27 | + calls = append(calls, "stop") |
| 28 | + return nil |
| 29 | + } |
| 30 | + startBackgroundServerForRestart = func(got *config.Config) error { |
| 31 | + if got != cfg { |
| 32 | + t.Fatalf("start config = %#v, want original config", got) |
| 33 | + } |
| 34 | + calls = append(calls, "start") |
| 35 | + return nil |
| 36 | + } |
| 37 | + waitForServerForRestart = func(baseURL string, timeout time.Duration) error { |
| 38 | + if baseURL != "http://127.0.0.1:14567" { |
| 39 | + t.Fatalf("baseURL = %q, want http://127.0.0.1:14567", baseURL) |
| 40 | + } |
| 41 | + if timeout != 15*time.Second { |
| 42 | + t.Fatalf("timeout = %s, want 15s", timeout) |
| 43 | + } |
| 44 | + calls = append(calls, "wait") |
| 45 | + return nil |
| 46 | + } |
| 47 | + |
| 48 | + if err := restartBackgroundService(cfg); err != nil { |
| 49 | + t.Fatalf("restartBackgroundService returned error: %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + if want := []string{"stop", "start", "wait"}; !reflect.DeepEqual(calls, want) { |
| 53 | + t.Fatalf("calls = %#v, want %#v", calls, want) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestRestartBackgroundServiceWrapsStopError(t *testing.T) { |
| 58 | + oldStop := stopBackgroundServiceForRestart |
| 59 | + oldStart := startBackgroundServerForRestart |
| 60 | + oldWait := waitForServerForRestart |
| 61 | + defer func() { |
| 62 | + stopBackgroundServiceForRestart = oldStop |
| 63 | + startBackgroundServerForRestart = oldStart |
| 64 | + waitForServerForRestart = oldWait |
| 65 | + }() |
| 66 | + |
| 67 | + stopBackgroundServiceForRestart = func() error { |
| 68 | + return errors.New("boom") |
| 69 | + } |
| 70 | + startBackgroundServerForRestart = func(*config.Config) error { |
| 71 | + t.Fatal("start should not be called after stop failure") |
| 72 | + return nil |
| 73 | + } |
| 74 | + waitForServerForRestart = func(string, time.Duration) error { |
| 75 | + t.Fatal("wait should not be called after stop failure") |
| 76 | + return nil |
| 77 | + } |
| 78 | + |
| 79 | + err := restartBackgroundService(&config.Config{ListenAddr: config.DefaultListenAddr}) |
| 80 | + if err == nil || !strings.Contains(err.Error(), "stopping existing service: boom") { |
| 81 | + t.Fatalf("error = %v, want wrapped stop error", err) |
| 82 | + } |
| 83 | +} |
0 commit comments