|
| 1 | +// Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package server |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "strings" |
| 20 | + "sync/atomic" |
| 21 | + "testing" |
| 22 | + "time" |
| 23 | +) |
| 24 | + |
| 25 | +func TestPollingHealthChecker_InitiallyHealthy(t *testing.T) { |
| 26 | + hc := NewPollingHealthChecker(5 * time.Minute) |
| 27 | + |
| 28 | + if err := hc.Healthy(context.Background()); err != nil { |
| 29 | + t.Fatalf("expected healthy immediately after creation, got: %v", err) |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func TestPollingHealthChecker_MarkAliveResetsStale(t *testing.T) { |
| 34 | + var now atomic.Int64 |
| 35 | + hc := &PollingHealthChecker{ |
| 36 | + stalenessThreshold: 10 * time.Second, |
| 37 | + clock: func() int64 { return now.Load() }, |
| 38 | + } |
| 39 | + now.Store(0) |
| 40 | + hc.lastMonoNano.Store(hc.clock()) |
| 41 | + |
| 42 | + // Advance past threshold. |
| 43 | + now.Store((15 * time.Second).Nanoseconds()) |
| 44 | + if err := hc.Healthy(context.Background()); err == nil { |
| 45 | + t.Fatal("expected stale error after 15s with 10s threshold") |
| 46 | + } |
| 47 | + |
| 48 | + // MarkAlive resets. |
| 49 | + hc.MarkAlive() |
| 50 | + if err := hc.Healthy(context.Background()); err != nil { |
| 51 | + t.Fatalf("expected healthy after MarkAlive, got: %v", err) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestPollingHealthChecker_StaleAfterThreshold(t *testing.T) { |
| 56 | + var now atomic.Int64 |
| 57 | + hc := &PollingHealthChecker{ |
| 58 | + stalenessThreshold: 30 * time.Second, |
| 59 | + clock: func() int64 { return now.Load() }, |
| 60 | + } |
| 61 | + now.Store(0) |
| 62 | + hc.lastMonoNano.Store(hc.clock()) |
| 63 | + |
| 64 | + // At exactly threshold: not stale (> is the check, not >=). |
| 65 | + now.Store((30 * time.Second).Nanoseconds()) |
| 66 | + if err := hc.Healthy(context.Background()); err != nil { |
| 67 | + t.Fatalf("expected healthy at exact threshold, got: %v", err) |
| 68 | + } |
| 69 | + |
| 70 | + // One nanosecond past threshold: stale. |
| 71 | + now.Store((30 * time.Second).Nanoseconds() + 1) |
| 72 | + err := hc.Healthy(context.Background()) |
| 73 | + if err == nil { |
| 74 | + t.Fatal("expected stale error just past threshold") |
| 75 | + } |
| 76 | + if !strings.Contains(err.Error(), "polling loop stale") { |
| 77 | + t.Fatalf("unexpected error message: %v", err) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestPollingHealthChecker_StaleErrorContainsDetails(t *testing.T) { |
| 82 | + var now atomic.Int64 |
| 83 | + hc := &PollingHealthChecker{ |
| 84 | + stalenessThreshold: 1 * time.Minute, |
| 85 | + clock: func() int64 { return now.Load() }, |
| 86 | + } |
| 87 | + now.Store(0) |
| 88 | + hc.lastMonoNano.Store(hc.clock()) |
| 89 | + |
| 90 | + now.Store((90 * time.Second).Nanoseconds()) |
| 91 | + err := hc.Healthy(context.Background()) |
| 92 | + if err == nil { |
| 93 | + t.Fatal("expected error") |
| 94 | + } |
| 95 | + |
| 96 | + msg := err.Error() |
| 97 | + if !strings.Contains(msg, "1m30s") { |
| 98 | + t.Errorf("expected elapsed duration in error, got: %s", msg) |
| 99 | + } |
| 100 | + if !strings.Contains(msg, "1m0s") { |
| 101 | + t.Errorf("expected threshold in error, got: %s", msg) |
| 102 | + } |
| 103 | +} |
0 commit comments