|
1 | 1 | using GraphRag.Callbacks; |
2 | 2 | using GraphRag.Config; |
3 | 3 | using GraphRag.Indexing.Runtime; |
| 4 | +using GraphRag.Logging; |
4 | 5 | using GraphRag.Storage; |
5 | 6 | using ManagedCode.GraphRag.Tests.Infrastructure; |
6 | 7 | using Microsoft.Extensions.DependencyInjection; |
@@ -75,4 +76,136 @@ public async Task ExecuteAsync_HonoursStopSignal() |
75 | 76 | Assert.Single(outputs); |
76 | 77 | Assert.Equal("first", outputs[0].Workflow); |
77 | 78 | } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public async Task ExecuteAsync_InvokesCallbacksAndUpdatesStats() |
| 82 | + { |
| 83 | + var services = new ServiceCollection().BuildServiceProvider(); |
| 84 | + var callbacks = new RecordingCallbacks(); |
| 85 | + var stats = new PipelineRunStats(); |
| 86 | + var context = new PipelineRunContext( |
| 87 | + new MemoryPipelineStorage(), |
| 88 | + new MemoryPipelineStorage(), |
| 89 | + new MemoryPipelineStorage(), |
| 90 | + new StubPipelineCache(), |
| 91 | + callbacks, |
| 92 | + stats, |
| 93 | + new PipelineState(), |
| 94 | + services); |
| 95 | + |
| 96 | + var pipeline = new WorkflowPipeline("stats", new[] |
| 97 | + { |
| 98 | + new WorkflowStep("first", async (cfg, ctx, token) => |
| 99 | + { |
| 100 | + await Task.Delay(5, token); |
| 101 | + return new WorkflowResult("ok"); |
| 102 | + }), |
| 103 | + new WorkflowStep("second", (cfg, ctx, token) => ValueTask.FromResult(new WorkflowResult("done"))) |
| 104 | + }); |
| 105 | + |
| 106 | + var executor = new PipelineExecutor(new NullLogger<PipelineExecutor>()); |
| 107 | + var results = new List<PipelineRunResult>(); |
| 108 | + |
| 109 | + await foreach (var result in executor.ExecuteAsync(pipeline, new GraphRagConfig(), context)) |
| 110 | + { |
| 111 | + results.Add(result); |
| 112 | + } |
| 113 | + |
| 114 | + Assert.Equal(new[] { "first", "second" }, callbacks.WorkflowStarts); |
| 115 | + Assert.Equal(callbacks.WorkflowStarts, callbacks.WorkflowEnds); |
| 116 | + Assert.Equal(2, callbacks.PipelineEndResults?.Count); |
| 117 | + Assert.True(callbacks.PipelineStartedWith?.SequenceEqual(pipeline.Names)); |
| 118 | + |
| 119 | + Assert.Equal(2, results.Count); |
| 120 | + Assert.All(results, r => Assert.Null(r.Errors)); |
| 121 | + |
| 122 | + Assert.True(stats.TotalRuntime >= 0); |
| 123 | + Assert.True(stats.Workflows.ContainsKey("first")); |
| 124 | + Assert.True(stats.Workflows["first"].ContainsKey("overall")); |
| 125 | + Assert.True(stats.Workflows.ContainsKey("second")); |
| 126 | + Assert.True(stats.Workflows["second"].ContainsKey("overall")); |
| 127 | + } |
| 128 | + |
| 129 | + [Fact] |
| 130 | + public async Task ExecuteAsync_RecordsExceptionInResultsAndStats() |
| 131 | + { |
| 132 | + var services = new ServiceCollection().BuildServiceProvider(); |
| 133 | + var stats = new PipelineRunStats(); |
| 134 | + var callbacks = new RecordingCallbacks(); |
| 135 | + var context = new PipelineRunContext( |
| 136 | + new MemoryPipelineStorage(), |
| 137 | + new MemoryPipelineStorage(), |
| 138 | + new MemoryPipelineStorage(), |
| 139 | + new StubPipelineCache(), |
| 140 | + callbacks, |
| 141 | + stats, |
| 142 | + new PipelineState(), |
| 143 | + services); |
| 144 | + |
| 145 | + var failure = new InvalidOperationException("fail"); |
| 146 | + var pipeline = new WorkflowPipeline("failing", new[] |
| 147 | + { |
| 148 | + new WorkflowStep("good", (cfg, ctx, token) => ValueTask.FromResult(new WorkflowResult("done"))), |
| 149 | + new WorkflowStep("bad", (cfg, ctx, token) => throw failure), |
| 150 | + new WorkflowStep("skipped", (cfg, ctx, token) => ValueTask.FromResult(new WorkflowResult("nope"))) |
| 151 | + }); |
| 152 | + |
| 153 | + var executor = new PipelineExecutor(new NullLogger<PipelineExecutor>()); |
| 154 | + var results = new List<PipelineRunResult>(); |
| 155 | + |
| 156 | + await foreach (var result in executor.ExecuteAsync(pipeline, new GraphRagConfig(), context)) |
| 157 | + { |
| 158 | + results.Add(result); |
| 159 | + } |
| 160 | + |
| 161 | + Assert.Equal(2, results.Count); |
| 162 | + Assert.Null(results[0].Errors); |
| 163 | + var errorResult = results[1]; |
| 164 | + Assert.NotNull(errorResult.Errors); |
| 165 | + var captured = Assert.Single(errorResult.Errors!); |
| 166 | + Assert.Same(failure, captured); |
| 167 | + |
| 168 | + Assert.Equal(new[] { "good", "bad" }, callbacks.WorkflowStarts); |
| 169 | + Assert.Equal(callbacks.WorkflowStarts, callbacks.WorkflowEnds); |
| 170 | + Assert.Equal(2, callbacks.PipelineEndResults?.Count); |
| 171 | + |
| 172 | + Assert.True(stats.Workflows.ContainsKey("good")); |
| 173 | + Assert.True(stats.Workflows.ContainsKey("bad")); |
| 174 | + Assert.False(stats.Workflows.ContainsKey("skipped")); |
| 175 | + Assert.True(stats.TotalRuntime >= 0); |
| 176 | + } |
| 177 | + |
| 178 | + private sealed class RecordingCallbacks : IWorkflowCallbacks |
| 179 | + { |
| 180 | + public IReadOnlyList<string>? PipelineStartedWith { get; private set; } |
| 181 | + public List<string> WorkflowStarts { get; } = new(); |
| 182 | + public List<string> WorkflowEnds { get; } = new(); |
| 183 | + public IReadOnlyList<PipelineRunResult>? PipelineEndResults { get; private set; } |
| 184 | + public List<ProgressSnapshot> ProgressUpdates { get; } = new(); |
| 185 | + |
| 186 | + public void PipelineStart(IReadOnlyList<string> names) |
| 187 | + { |
| 188 | + PipelineStartedWith = names.ToArray(); |
| 189 | + } |
| 190 | + |
| 191 | + public void PipelineEnd(IReadOnlyList<PipelineRunResult> results) |
| 192 | + { |
| 193 | + PipelineEndResults = results.ToArray(); |
| 194 | + } |
| 195 | + |
| 196 | + public void WorkflowStart(string name, object? instance) |
| 197 | + { |
| 198 | + WorkflowStarts.Add(name); |
| 199 | + } |
| 200 | + |
| 201 | + public void WorkflowEnd(string name, object? instance) |
| 202 | + { |
| 203 | + WorkflowEnds.Add(name); |
| 204 | + } |
| 205 | + |
| 206 | + public void ReportProgress(ProgressSnapshot progress) |
| 207 | + { |
| 208 | + ProgressUpdates.Add(progress); |
| 209 | + } |
| 210 | + } |
78 | 211 | } |
0 commit comments