|
| 1 | +package azkustoingest |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + "testing/synctest" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/Azure/azure-kusto-go/azkustoingest/internal/status" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | +) |
| 12 | + |
| 13 | +type TableClientReaderFunc func(ctx context.Context, ingestionSourceID string) (map[string]interface{}, error) |
| 14 | + |
| 15 | +func (t TableClientReaderFunc) Read(ctx context.Context, ingestionSourceID string) (map[string]interface{}, error) { |
| 16 | + return t(ctx, ingestionSourceID) |
| 17 | +} |
| 18 | + |
| 19 | +var _ status.TableClientReader = (*TableClientReaderFunc)(nil) |
| 20 | + |
| 21 | +func TestWait(t *testing.T) { |
| 22 | + synctest.Test(t, func(t *testing.T) { |
| 23 | + res := &Result{ |
| 24 | + reportToTable: true, |
| 25 | + tableClient: TableClientReaderFunc(func(ctx context.Context, ingestionSourceID string) (map[string]interface{}, error) { |
| 26 | + assert.FailNow(t, "Expected table client to not be called") |
| 27 | + return nil, nil |
| 28 | + }), |
| 29 | + record: statusRecord{ |
| 30 | + Status: Pending, |
| 31 | + }, |
| 32 | + } |
| 33 | + |
| 34 | + ch := res.Wait(t.Context()) |
| 35 | + synctest.Wait() |
| 36 | + |
| 37 | + select { |
| 38 | + case <-ch: |
| 39 | + assert.FailNow(t, "Expected nothing to be sent on channel") |
| 40 | + default: |
| 41 | + } |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +func TestWait_ImmediateFirst(t *testing.T) { |
| 46 | + synctest.Test(t, func(t *testing.T) { |
| 47 | + res := &Result{ |
| 48 | + reportToTable: true, |
| 49 | + tableClient: TableClientReaderFunc(func(ctx context.Context, ingestionSourceID string) (map[string]any, error) { |
| 50 | + return map[string]any{"Status": string(Succeeded)}, nil |
| 51 | + }), |
| 52 | + record: statusRecord{ |
| 53 | + Status: Pending, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + ch := res.Wait(t.Context(), WithImmediateFirst()) |
| 58 | + synctest.Wait() |
| 59 | + |
| 60 | + select { |
| 61 | + case <-ch: |
| 62 | + default: |
| 63 | + assert.FailNow(t, "Expected something to be sent on channel") |
| 64 | + } |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func TestWait_WithInterval(t *testing.T) { |
| 69 | + synctest.Test(t, func(t *testing.T) { |
| 70 | + startTime := time.Now() |
| 71 | + var calledTimes []time.Duration |
| 72 | + |
| 73 | + res := &Result{ |
| 74 | + reportToTable: true, |
| 75 | + tableClient: TableClientReaderFunc(func(ctx context.Context, ingestionSourceID string) (map[string]any, error) { |
| 76 | + calledTimes = append(calledTimes, time.Now().Sub(startTime)) |
| 77 | + ret := map[string]any{"Status": string(Pending)} |
| 78 | + if len(calledTimes) >= 3 { |
| 79 | + ret["Status"] = string(Failed) |
| 80 | + } |
| 81 | + return ret, nil |
| 82 | + }), |
| 83 | + record: statusRecord{ |
| 84 | + Status: Pending, |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + ch := res.Wait(t.Context(), WithInterval(5*time.Second)) |
| 89 | + synctest.Wait() |
| 90 | + assert.Empty(t, calledTimes, "Expected no calls to table client") |
| 91 | + |
| 92 | + time.Sleep(5 * time.Second) |
| 93 | + synctest.Wait() |
| 94 | + assert.Len(t, calledTimes, 1) |
| 95 | + assert.Equal(t, 5*time.Second, calledTimes[0]) |
| 96 | + |
| 97 | + time.Sleep(5 * time.Second) |
| 98 | + synctest.Wait() |
| 99 | + assert.Len(t, calledTimes, 2) |
| 100 | + assert.Equal(t, 10*time.Second, calledTimes[1]) |
| 101 | + |
| 102 | + time.Sleep(5 * time.Second) |
| 103 | + synctest.Wait() |
| 104 | + assert.Len(t, calledTimes, 3) |
| 105 | + assert.Equal(t, 15*time.Second, calledTimes[2]) |
| 106 | + |
| 107 | + select { |
| 108 | + case <-ch: |
| 109 | + default: |
| 110 | + assert.FailNow(t, "Expected something to be sent on channel") |
| 111 | + } |
| 112 | + }) |
| 113 | +} |
| 114 | + |
| 115 | +func TestWait_WithRetryBackoffDelay(t *testing.T) { |
| 116 | + synctest.Test(t, func(t *testing.T) { |
| 117 | + startTime := time.Now() |
| 118 | + var calledTimes []time.Duration |
| 119 | + |
| 120 | + res := &Result{ |
| 121 | + reportToTable: true, |
| 122 | + tableClient: TableClientReaderFunc(func(ctx context.Context, ingestionSourceID string) (map[string]any, error) { |
| 123 | + calledTimes = append(calledTimes, time.Now().Sub(startTime)) |
| 124 | + return nil, assert.AnError |
| 125 | + }), |
| 126 | + record: statusRecord{ |
| 127 | + Status: Pending, |
| 128 | + }, |
| 129 | + } |
| 130 | + |
| 131 | + ch := res.Wait(t.Context(), WithRetryBackoffDelay(1*time.Second, 3*time.Second), WithRetryBackoffJitter(0)) |
| 132 | + synctest.Wait() |
| 133 | + assert.Empty(t, calledTimes, "Expected no calls to table client") |
| 134 | + |
| 135 | + // First call after DefaultWaitPollInterval (10s) |
| 136 | + time.Sleep(10 * time.Second) |
| 137 | + synctest.Wait() |
| 138 | + assert.Len(t, calledTimes, 1) |
| 139 | + assert.Equal(t, 10*time.Second, calledTimes[0]) |
| 140 | + |
| 141 | + // Second call after first backoff delay (1s) + poll interval (10s) |
| 142 | + time.Sleep(11 * time.Second) |
| 143 | + synctest.Wait() |
| 144 | + assert.Len(t, calledTimes, 2) |
| 145 | + assert.Equal(t, 21*time.Second, calledTimes[1]) |
| 146 | + |
| 147 | + // Third call after second backoff delay (3s) + poll interval (10s) |
| 148 | + time.Sleep(13 * time.Second) |
| 149 | + synctest.Wait() |
| 150 | + assert.Len(t, calledTimes, 3) |
| 151 | + assert.Equal(t, 34*time.Second, calledTimes[2]) |
| 152 | + |
| 153 | + select { |
| 154 | + case err := <-ch: |
| 155 | + assert.NotNil(t, err) |
| 156 | + default: |
| 157 | + assert.FailNow(t, "Expected something to be sent on channel") |
| 158 | + } |
| 159 | + }) |
| 160 | +} |
0 commit comments