|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log/slog" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + "sync" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/libops/ppb/pkg/config" |
| 14 | + "github.com/libops/ppb/pkg/machine" |
| 15 | +) |
| 16 | + |
| 17 | +func TestStartPingRoutine_Integration(t *testing.T) { |
| 18 | + // Track ping requests |
| 19 | + var pingCount int |
| 20 | + var pingURLs []string |
| 21 | + var mu sync.Mutex |
| 22 | + |
| 23 | + mux := http.NewServeMux() |
| 24 | + mux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) { |
| 25 | + mu.Lock() |
| 26 | + defer mu.Unlock() |
| 27 | + pingCount++ |
| 28 | + pingURLs = append(pingURLs, r.URL.Path) |
| 29 | + |
| 30 | + w.WriteHeader(http.StatusOK) |
| 31 | + _, err := w.Write([]byte("pong")) |
| 32 | + if err != nil { |
| 33 | + slog.Error("Unable to write ping response", "err", err) |
| 34 | + os.Exit(1) |
| 35 | + } |
| 36 | + }) |
| 37 | + server := &http.Server{ |
| 38 | + Addr: ":8808", |
| 39 | + Handler: mux, |
| 40 | + } |
| 41 | + go func() { |
| 42 | + if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed { |
| 43 | + // Ignore "address already in use" errors during testing |
| 44 | + if !strings.Contains(err.Error(), "address already in use") { |
| 45 | + t.Errorf("Mock server error: %v", err) |
| 46 | + } |
| 47 | + } |
| 48 | + }() |
| 49 | + |
| 50 | + time.Sleep(1 * time.Second) |
| 51 | + |
| 52 | + defer func() { |
| 53 | + ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) |
| 54 | + defer cancel() |
| 55 | + err := server.Shutdown(ctx) |
| 56 | + if err != nil { |
| 57 | + slog.Error("Server shutdown failed", "err", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | + }() |
| 61 | + |
| 62 | + mockMachine := machine.NewGceMachine() |
| 63 | + mockMachine.SetHostForTesting("127.0.0.1") |
| 64 | + |
| 65 | + config := &config.Config{ |
| 66 | + Machine: mockMachine, |
| 67 | + } |
| 68 | + |
| 69 | + ctx, cancel := context.WithTimeout(context.Background(), 350*time.Millisecond) |
| 70 | + defer cancel() |
| 71 | + |
| 72 | + var wg sync.WaitGroup |
| 73 | + wg.Add(1) |
| 74 | + |
| 75 | + go startPingRoutine(ctx, &wg, config, 100*time.Millisecond) |
| 76 | + wg.Wait() |
| 77 | + |
| 78 | + mu.Lock() |
| 79 | + defer mu.Unlock() |
| 80 | + |
| 81 | + if pingCount == 0 { |
| 82 | + t.Error("Expected at least one ping request, got none") |
| 83 | + } |
| 84 | + |
| 85 | + // Verify all requests were to /ping endpoint |
| 86 | + for _, url := range pingURLs { |
| 87 | + if url != "/ping" { |
| 88 | + t.Errorf("Expected ping to /ping endpoint, got %s", url) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Should have made multiple pings (at least 2-3 in 350ms with 100ms interval) |
| 93 | + if pingCount < 2 { |
| 94 | + t.Errorf("Expected at least 2 ping requests, got %d", pingCount) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func TestStartPingRoutine_ContextCancellation(t *testing.T) { |
| 99 | + mockMachine := machine.NewGceMachine() |
| 100 | + mockMachine.SetHostForTesting("127.0.0.1") |
| 101 | + |
| 102 | + config := &config.Config{ |
| 103 | + Machine: mockMachine, |
| 104 | + } |
| 105 | + |
| 106 | + ctx, cancel := context.WithCancel(context.Background()) |
| 107 | + var wg sync.WaitGroup |
| 108 | + wg.Add(1) |
| 109 | + |
| 110 | + routineFinished := make(chan bool, 1) |
| 111 | + |
| 112 | + go func() { |
| 113 | + startPingRoutine(ctx, &wg, config, 50*time.Millisecond) |
| 114 | + routineFinished <- true |
| 115 | + }() |
| 116 | + |
| 117 | + time.Sleep(100 * time.Millisecond) |
| 118 | + cancel() |
| 119 | + |
| 120 | + wg.Wait() |
| 121 | + |
| 122 | + // Verify the routine actually finished |
| 123 | + select { |
| 124 | + case <-routineFinished: |
| 125 | + case <-time.After(1 * time.Second): |
| 126 | + t.Error("Ping routine did not finish within expected time after context cancellation") |
| 127 | + } |
| 128 | +} |
0 commit comments