|
| 1 | +// Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. 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 main |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + stderrors "errors" |
| 20 | + "testing" |
| 21 | + "time" |
| 22 | +) |
| 23 | + |
| 24 | +// A representative all_reduce_perf results block (one data row + summary trailer). |
| 25 | +const ncclCompleteLog = `# size count type redop root time algbw busbw #wrong |
| 26 | +# (B) (elements) (us) (GB/s) (GB/s) |
| 27 | + 8589934592 2147483648 float sum -1 48298 177.85 333.47 0 48292 177.87 333.51 0 |
| 28 | +# Out of bounds values : 0 OK |
| 29 | +# Avg bus bandwidth : 333.49 |
| 30 | +` |
| 31 | + |
| 32 | +func TestNcclLauncherLogComplete(t *testing.T) { |
| 33 | + tests := []struct { |
| 34 | + name string |
| 35 | + logs string |
| 36 | + want bool |
| 37 | + }{ |
| 38 | + {"empty", "", false}, |
| 39 | + {"header only (no data rows, no trailer)", "# size count type\n# (B)\n", false}, |
| 40 | + {"init noise only", "NCCL INFO Bootstrap : Using eth0\nsome mpirun warmup line\n", false}, |
| 41 | + // An early data row without the trailer must NOT count as complete: the |
| 42 | + // parser keys on the *last* row, so a log truncated before the largest |
| 43 | + // size would otherwise short-circuit the retry loop (CodeRabbit #1691). |
| 44 | + {"early data row but no trailer", "# size ...\n 1024 256 float sum -1 12 0.10 0.09 0 12 0.10 0.09 0\n", false}, |
| 45 | + {"full sweep with trailer", ncclCompleteLog, true}, |
| 46 | + {"summary trailer only", "# Avg bus bandwidth : 333.49\n", true}, |
| 47 | + } |
| 48 | + for _, tt := range tests { |
| 49 | + t.Run(tt.name, func(t *testing.T) { |
| 50 | + if got := ncclLauncherLogComplete(tt.logs); got != tt.want { |
| 51 | + t.Errorf("ncclLauncherLogComplete() = %v, want %v", got, tt.want) |
| 52 | + } |
| 53 | + }) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestReadLauncherLogsUntilComplete_ReturnsWhenComplete(t *testing.T) { |
| 58 | + // First read is already complete — returns immediately, no retries. |
| 59 | + calls := 0 |
| 60 | + fetch := func(context.Context) (string, error) { calls++; return ncclCompleteLog, nil } |
| 61 | + logs, err := readLauncherLogsUntilComplete(context.Background(), fetch, 5, time.Millisecond) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("unexpected error: %v", err) |
| 64 | + } |
| 65 | + if !ncclLauncherLogComplete(logs) { |
| 66 | + t.Errorf("expected complete logs, got %q", logs) |
| 67 | + } |
| 68 | + if calls != 1 { |
| 69 | + t.Errorf("expected 1 fetch, got %d", calls) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestReadLauncherLogsUntilComplete_RetriesUntilComplete(t *testing.T) { |
| 74 | + // Truncated (empty) reads twice, then the full results table lands. |
| 75 | + calls := 0 |
| 76 | + fetch := func(context.Context) (string, error) { |
| 77 | + calls++ |
| 78 | + if calls < 3 { |
| 79 | + return "", nil |
| 80 | + } |
| 81 | + return ncclCompleteLog, nil |
| 82 | + } |
| 83 | + logs, err := readLauncherLogsUntilComplete(context.Background(), fetch, 5, time.Millisecond) |
| 84 | + if err != nil { |
| 85 | + t.Fatalf("unexpected error: %v", err) |
| 86 | + } |
| 87 | + if !ncclLauncherLogComplete(logs) { |
| 88 | + t.Errorf("expected complete logs after retries, got %q", logs) |
| 89 | + } |
| 90 | + if calls != 3 { |
| 91 | + t.Errorf("expected 3 fetches, got %d", calls) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestReadLauncherLogsUntilComplete_ReturnsLastReadWhenNeverComplete(t *testing.T) { |
| 96 | + // Never completes: must return the last (incomplete) read after the budget, |
| 97 | + // not an error — so the caller's parse-failure path can surface it. |
| 98 | + calls := 0 |
| 99 | + fetch := func(context.Context) (string, error) { calls++; return "partial noise, no table", nil } |
| 100 | + logs, err := readLauncherLogsUntilComplete(context.Background(), fetch, 3, time.Millisecond) |
| 101 | + if err != nil { |
| 102 | + t.Fatalf("expected no error (last read returned for diagnosis), got %v", err) |
| 103 | + } |
| 104 | + if ncclLauncherLogComplete(logs) { |
| 105 | + t.Errorf("expected incomplete logs to be returned as-is, got complete %q", logs) |
| 106 | + } |
| 107 | + if calls != 3 { |
| 108 | + t.Errorf("expected exactly the attempt budget (3) fetches, got %d", calls) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestReadLauncherLogsUntilComplete_PropagatesFetchError(t *testing.T) { |
| 113 | + sentinel := stderrors.New("logs api unavailable") |
| 114 | + fetch := func(context.Context) (string, error) { return "", sentinel } |
| 115 | + _, err := readLauncherLogsUntilComplete(context.Background(), fetch, 5, time.Millisecond) |
| 116 | + if !stderrors.Is(err, sentinel) { |
| 117 | + t.Errorf("expected the fetch error to propagate, got %v", err) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +func TestReadLauncherLogsUntilComplete_ReturnsOnContextCancel(t *testing.T) { |
| 122 | + ctx, cancel := context.WithCancel(context.Background()) |
| 123 | + cancel() // canceled before the retry sleep |
| 124 | + calls := 0 |
| 125 | + fetch := func(context.Context) (string, error) { calls++; return "still no table", nil } |
| 126 | + logs, err := readLauncherLogsUntilComplete(ctx, fetch, 5, time.Hour) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("expected no error on cancel (last read returned), got %v", err) |
| 129 | + } |
| 130 | + if ncclLauncherLogComplete(logs) { |
| 131 | + t.Errorf("expected incomplete logs, got complete %q", logs) |
| 132 | + } |
| 133 | + // One fetch, then the canceled context short-circuits the sleep. |
| 134 | + if calls != 1 { |
| 135 | + t.Errorf("expected 1 fetch before cancel short-circuit, got %d", calls) |
| 136 | + } |
| 137 | +} |
0 commit comments