|
| 1 | +// Copyright 2025 Google LLC |
| 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 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package x |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "errors" |
| 22 | + "slices" |
| 23 | + "sync/atomic" |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/firebase/genkit/go/genkit" |
| 28 | +) |
| 29 | + |
| 30 | +func TestDefineStreamingFlow(t *testing.T) { |
| 31 | + t.Run("streams values via channel", func(t *testing.T) { |
| 32 | + ctx := context.Background() |
| 33 | + g := genkit.Init(ctx) |
| 34 | + |
| 35 | + flow := DefineStreamingFlow(g, "test/counter", func(ctx context.Context, n int, stream chan<- int) (string, error) { |
| 36 | + for i := 0; i < n; i++ { |
| 37 | + select { |
| 38 | + case stream <- i: |
| 39 | + case <-ctx.Done(): |
| 40 | + return "", ctx.Err() |
| 41 | + } |
| 42 | + } |
| 43 | + return "done", nil |
| 44 | + }) |
| 45 | + |
| 46 | + var streamedValues []int |
| 47 | + var finalOutput string |
| 48 | + |
| 49 | + for v, err := range flow.Stream(ctx, 3) { |
| 50 | + if err != nil { |
| 51 | + t.Fatalf("Stream error: %v", err) |
| 52 | + } |
| 53 | + if v.Done { |
| 54 | + finalOutput = v.Output |
| 55 | + } else { |
| 56 | + streamedValues = append(streamedValues, v.Stream) |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + wantStreamed := []int{0, 1, 2} |
| 61 | + if !slices.Equal(streamedValues, wantStreamed) { |
| 62 | + t.Errorf("streamed values = %v, want %v", streamedValues, wantStreamed) |
| 63 | + } |
| 64 | + if finalOutput != "done" { |
| 65 | + t.Errorf("final output = %q, want %q", finalOutput, "done") |
| 66 | + } |
| 67 | + }) |
| 68 | + |
| 69 | + t.Run("runs without streaming", func(t *testing.T) { |
| 70 | + ctx := context.Background() |
| 71 | + g := genkit.Init(ctx) |
| 72 | + |
| 73 | + flow := DefineStreamingFlow(g, "test/nostream", func(ctx context.Context, n int, stream chan<- int) (string, error) { |
| 74 | + for i := 0; i < n; i++ { |
| 75 | + stream <- i |
| 76 | + } |
| 77 | + return "complete", nil |
| 78 | + }) |
| 79 | + |
| 80 | + output, err := flow.Run(ctx, 3) |
| 81 | + if err != nil { |
| 82 | + t.Fatalf("Run error: %v", err) |
| 83 | + } |
| 84 | + if output != "complete" { |
| 85 | + t.Errorf("output = %q, want %q", output, "complete") |
| 86 | + } |
| 87 | + }) |
| 88 | + |
| 89 | + t.Run("handles errors", func(t *testing.T) { |
| 90 | + ctx := context.Background() |
| 91 | + g := genkit.Init(ctx) |
| 92 | + |
| 93 | + expectedErr := errors.New("flow failed") |
| 94 | + flow := DefineStreamingFlow(g, "test/failing", func(ctx context.Context, _ int, stream chan<- int) (string, error) { |
| 95 | + return "", expectedErr |
| 96 | + }) |
| 97 | + |
| 98 | + var gotErr error |
| 99 | + for _, err := range flow.Stream(ctx, 1) { |
| 100 | + if err != nil { |
| 101 | + gotErr = err |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + if gotErr == nil { |
| 106 | + t.Error("expected error, got nil") |
| 107 | + } |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("handles context cancellation", func(t *testing.T) { |
| 111 | + ctx := context.Background() |
| 112 | + g := genkit.Init(ctx) |
| 113 | + |
| 114 | + flow := DefineStreamingFlow(g, "test/cancel", func(ctx context.Context, n int, stream chan<- int) (int, error) { |
| 115 | + for i := 0; i < n; i++ { |
| 116 | + select { |
| 117 | + case stream <- i: |
| 118 | + case <-ctx.Done(): |
| 119 | + return 0, ctx.Err() |
| 120 | + } |
| 121 | + } |
| 122 | + return n, nil |
| 123 | + }) |
| 124 | + |
| 125 | + cancelCtx, cancel := context.WithCancel(ctx) |
| 126 | + cancel() |
| 127 | + |
| 128 | + var gotErr error |
| 129 | + for _, err := range flow.Stream(cancelCtx, 100) { |
| 130 | + if err != nil { |
| 131 | + gotErr = err |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + if gotErr == nil { |
| 136 | + t.Error("expected context cancellation error, got nil") |
| 137 | + } |
| 138 | + }) |
| 139 | + |
| 140 | + t.Run("handles empty stream", func(t *testing.T) { |
| 141 | + ctx := context.Background() |
| 142 | + g := genkit.Init(ctx) |
| 143 | + |
| 144 | + flow := DefineStreamingFlow(g, "test/empty", func(ctx context.Context, _ struct{}, stream chan<- int) (string, error) { |
| 145 | + return "empty", nil |
| 146 | + }) |
| 147 | + |
| 148 | + var streamedValues []int |
| 149 | + var finalOutput string |
| 150 | + |
| 151 | + for v, err := range flow.Stream(ctx, struct{}{}) { |
| 152 | + if err != nil { |
| 153 | + t.Fatalf("Stream error: %v", err) |
| 154 | + } |
| 155 | + if v.Done { |
| 156 | + finalOutput = v.Output |
| 157 | + } else { |
| 158 | + streamedValues = append(streamedValues, v.Stream) |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + if len(streamedValues) != 0 { |
| 163 | + t.Errorf("streamed values = %v, want empty", streamedValues) |
| 164 | + } |
| 165 | + if finalOutput != "empty" { |
| 166 | + t.Errorf("final output = %q, want %q", finalOutput, "empty") |
| 167 | + } |
| 168 | + }) |
| 169 | + |
| 170 | + t.Run("handles consumer breaking early", func(t *testing.T) { |
| 171 | + ctx := context.Background() |
| 172 | + g := genkit.Init(ctx) |
| 173 | + |
| 174 | + var produced atomic.Int32 |
| 175 | + flow := DefineStreamingFlow(g, "test/earlybreak", func(ctx context.Context, n int, stream chan<- int) (string, error) { |
| 176 | + for i := 0; i < n; i++ { |
| 177 | + select { |
| 178 | + case stream <- i: |
| 179 | + produced.Add(1) |
| 180 | + case <-ctx.Done(): |
| 181 | + return "cancelled", ctx.Err() |
| 182 | + } |
| 183 | + } |
| 184 | + return "done", nil |
| 185 | + }) |
| 186 | + |
| 187 | + var received []int |
| 188 | + done := make(chan struct{}) |
| 189 | + go func() { |
| 190 | + defer close(done) |
| 191 | + for v, err := range flow.Stream(ctx, 1000) { |
| 192 | + if err != nil { |
| 193 | + return |
| 194 | + } |
| 195 | + if !v.Done { |
| 196 | + received = append(received, v.Stream) |
| 197 | + if len(received) >= 3 { |
| 198 | + break // Stop early |
| 199 | + } |
| 200 | + } |
| 201 | + } |
| 202 | + }() |
| 203 | + |
| 204 | + // Should complete without deadlock |
| 205 | + select { |
| 206 | + case <-done: |
| 207 | + // Success - no deadlock |
| 208 | + case <-time.After(2 * time.Second): |
| 209 | + t.Fatal("timeout - likely deadlock when consumer breaks early") |
| 210 | + } |
| 211 | + |
| 212 | + if len(received) != 3 { |
| 213 | + t.Errorf("received %d values, want 3", len(received)) |
| 214 | + } |
| 215 | + |
| 216 | + // Producer should have been signaled to stop (though may have produced a few more) |
| 217 | + // The important thing is no deadlock occurred |
| 218 | + t.Logf("producer created %d chunks before stopping", produced.Load()) |
| 219 | + }) |
| 220 | +} |
0 commit comments