|
| 1 | +// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 2 | +// or more contributor license agreements. Licensed under the Elastic License; |
| 3 | +// you may not use this file except in compliance with the Elastic License. |
| 4 | + |
| 5 | +package osqdcli |
| 6 | + |
| 7 | +import ( |
| 8 | + "context" |
| 9 | + "testing" |
| 10 | + "time" |
| 11 | + |
| 12 | + "github.com/elastic/beats/v7/libbeat/logp" |
| 13 | + "github.com/google/go-cmp/cmp" |
| 14 | + "github.com/google/go-cmp/cmp/cmpopts" |
| 15 | +) |
| 16 | + |
| 17 | +func TestRetryRun(t *testing.T) { |
| 18 | + logp.Configure(logp.Config{ |
| 19 | + Level: logp.DebugLevel, |
| 20 | + ToStderr: true, |
| 21 | + Selectors: []string{"*"}, |
| 22 | + }) |
| 23 | + |
| 24 | + log := logp.NewLogger("retry_test").With("context", "osquery client connect") |
| 25 | + ctx := context.Background() |
| 26 | + |
| 27 | + type fields struct { |
| 28 | + maxRetry int |
| 29 | + retryWait time.Duration |
| 30 | + log *logp.Logger |
| 31 | + } |
| 32 | + |
| 33 | + type args struct { |
| 34 | + ctx context.Context |
| 35 | + fn tryFunc |
| 36 | + } |
| 37 | + |
| 38 | + argsWithFunc := func(fn tryFunc) args { |
| 39 | + return args{ |
| 40 | + ctx: ctx, |
| 41 | + fn: fn, |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + funcSucceedsOnNAttempt := func(attempt int) func(context.Context) error { |
| 46 | + curAttempt := 1 |
| 47 | + return func(ctx context.Context) error { |
| 48 | + if curAttempt == attempt { |
| 49 | + return nil |
| 50 | + } |
| 51 | + curAttempt++ |
| 52 | + return ErrAlreadyConnected |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + tests := []struct { |
| 57 | + name string |
| 58 | + fields fields |
| 59 | + args args |
| 60 | + wantErr error |
| 61 | + }{ |
| 62 | + { |
| 63 | + name: "no retries, no wait, success", |
| 64 | + fields: fields{ |
| 65 | + log: log, |
| 66 | + }, |
| 67 | + args: argsWithFunc(func(ctx context.Context) error { |
| 68 | + return nil |
| 69 | + }), |
| 70 | + }, |
| 71 | + { |
| 72 | + name: "no retries, no wait, error", |
| 73 | + fields: fields{ |
| 74 | + log: log, |
| 75 | + }, |
| 76 | + args: argsWithFunc(func(ctx context.Context) error { |
| 77 | + return ErrAlreadyConnected |
| 78 | + }), |
| 79 | + wantErr: ErrAlreadyConnected, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: "retries, no wait, no more retries fails", |
| 83 | + fields: fields{ |
| 84 | + maxRetry: 3, |
| 85 | + log: log, |
| 86 | + }, |
| 87 | + args: argsWithFunc(funcSucceedsOnNAttempt(8)), |
| 88 | + wantErr: ErrAlreadyConnected, |
| 89 | + }, |
| 90 | + { |
| 91 | + name: "retries, no wait, success", |
| 92 | + fields: fields{ |
| 93 | + maxRetry: 3, |
| 94 | + log: log, |
| 95 | + }, |
| 96 | + args: argsWithFunc(funcSucceedsOnNAttempt(4)), |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "retries, with wait, success", |
| 100 | + fields: fields{ |
| 101 | + maxRetry: 3, |
| 102 | + retryWait: 1 * time.Millisecond, |
| 103 | + log: log, |
| 104 | + }, |
| 105 | + args: argsWithFunc(funcSucceedsOnNAttempt(4)), |
| 106 | + }, |
| 107 | + { |
| 108 | + name: "retries, with wait, success sooner", |
| 109 | + fields: fields{ |
| 110 | + maxRetry: 3, |
| 111 | + retryWait: 1 * time.Millisecond, |
| 112 | + log: log, |
| 113 | + }, |
| 114 | + args: argsWithFunc(funcSucceedsOnNAttempt(2)), |
| 115 | + }, |
| 116 | + } |
| 117 | + for _, tt := range tests { |
| 118 | + t.Run(tt.name, func(t *testing.T) { |
| 119 | + r := &retry{ |
| 120 | + maxRetry: tt.fields.maxRetry, |
| 121 | + retryWait: tt.fields.retryWait, |
| 122 | + log: tt.fields.log, |
| 123 | + } |
| 124 | + err := r.Run(tt.args.ctx, tt.args.fn) |
| 125 | + if err != nil { |
| 126 | + if tt.wantErr != nil { |
| 127 | + diff := cmp.Diff(tt.wantErr, err, cmpopts.EquateErrors()) |
| 128 | + if diff != "" { |
| 129 | + t.Error(diff) |
| 130 | + } |
| 131 | + } else { |
| 132 | + t.Errorf("got err: %v, wantErr: nil", err) |
| 133 | + } |
| 134 | + } else if tt.wantErr != nil { |
| 135 | + t.Errorf("got err: nil, wantErr: %v", tt.wantErr) |
| 136 | + } |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
0 commit comments