Skip to content

Commit 04a110a

Browse files
committed
Add tests
1 parent 57cdd32 commit 04a110a

File tree

3 files changed

+1295
-159
lines changed

3 files changed

+1295
-159
lines changed

internal/routing/aggregator.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,9 @@ func (a *AggMinAggregator) Finish() (interface{}, error) {
218218
if a.firstErr != nil {
219219
return nil, a.firstErr
220220
}
221+
if !a.hasResult {
222+
return nil, fmt.Errorf("redis: no valid results to aggregate for min operation")
223+
}
221224
return a.min, nil
222225
}
223226

@@ -264,6 +267,9 @@ func (a *AggMaxAggregator) Finish() (interface{}, error) {
264267
if a.firstErr != nil {
265268
return nil, a.firstErr
266269
}
270+
if !a.hasResult {
271+
return nil, fmt.Errorf("redis: no valid results to aggregate for max operation")
272+
}
267273
return a.max, nil
268274
}
269275

@@ -312,6 +318,9 @@ func (a *AggLogicalAndAggregator) Finish() (interface{}, error) {
312318
if a.firstErr != nil {
313319
return nil, a.firstErr
314320
}
321+
if !a.hasResult {
322+
return nil, fmt.Errorf("redis: no valid results to aggregate for logical AND operation")
323+
}
315324
return a.result, nil
316325
}
317326

@@ -360,6 +369,9 @@ func (a *AggLogicalOrAggregator) Finish() (interface{}, error) {
360369
if a.firstErr != nil {
361370
return nil, a.firstErr
362371
}
372+
if !a.hasResult {
373+
return nil, fmt.Errorf("redis: no valid results to aggregate for logical OR operation")
374+
}
363375
return a.result, nil
364376
}
365377

osscluster_router.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -312,12 +312,23 @@ func (c *ClusterClient) executeParallel(ctx context.Context, cmd Cmder, nodes []
312312
close(results)
313313
}()
314314

315-
// Collect results
315+
// Collect results and check for errors
316316
cmds := make([]Cmder, 0, len(nodes))
317+
var firstErr error
318+
317319
for result := range results {
320+
if result.err != nil && firstErr == nil {
321+
firstErr = result.err
322+
}
318323
cmds = append(cmds, result.cmd)
319324
}
320325

326+
// If there was an error and no policy specified, fail fast
327+
if firstErr != nil && (policy == nil || policy.Response == routing.RespDefaultKeyless) {
328+
cmd.SetErr(firstErr)
329+
return firstErr
330+
}
331+
321332
return c.aggregateResponses(cmd, cmds, policy)
322333
}
323334

@@ -342,11 +353,11 @@ func (c *ClusterClient) aggregateMultiSlotResults(ctx context.Context, cmd Cmder
342353
return firstErr
343354
}
344355

345-
return c.aggregateKeyedResponses(ctx, cmd, keyedResults, keyOrder, policy)
356+
return c.aggregateKeyedResponses(cmd, keyedResults, keyOrder, policy)
346357
}
347358

348359
// aggregateKeyedResponses aggregates responses while preserving key order
349-
func (c *ClusterClient) aggregateKeyedResponses(ctx context.Context, cmd Cmder, keyedResults map[string]Cmder, keyOrder []string, policy *routing.CommandPolicy) error {
360+
func (c *ClusterClient) aggregateKeyedResponses(cmd Cmder, keyedResults map[string]Cmder, keyOrder []string, policy *routing.CommandPolicy) error {
350361
if len(keyedResults) == 0 {
351362
return fmt.Errorf("redis: no results to aggregate")
352363
}

0 commit comments

Comments
 (0)