|
| 1 | +package common |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/databricks/databricks-sdk-go/experimental/mocks" |
| 9 | + "github.com/databricks/databricks-sdk-go/service/workspace" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRetryOnTimeout_NoError(t *testing.T) { |
| 15 | + w := mocks.NewMockWorkspaceClient(t) |
| 16 | + expected := &workspace.ObjectInfo{} |
| 17 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 18 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(expected, nil) |
| 19 | + res, err := RetryOnTimeout(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 20 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 21 | + }) |
| 22 | + assert.NoError(t, err) |
| 23 | + assert.Equal(t, expected, res) |
| 24 | +} |
| 25 | + |
| 26 | +func TestRetryOnTimeout_OneError(t *testing.T) { |
| 27 | + w := mocks.NewMockWorkspaceClient(t) |
| 28 | + expected := &workspace.ObjectInfo{} |
| 29 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 30 | + call1 := api.GetStatusByPath(mock.Anything, mock.Anything).Return(nil, errors.New("request failed: request timed out after 1m0s of inactivity")) |
| 31 | + call1.Repeatability = 1 |
| 32 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(expected, nil) |
| 33 | + res, err := RetryOnTimeout(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 34 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 35 | + }) |
| 36 | + assert.NoError(t, err) |
| 37 | + assert.Equal(t, expected, res) |
| 38 | +} |
| 39 | + |
| 40 | +func TestRetryOnTimeout_NonRetriableError(t *testing.T) { |
| 41 | + w := mocks.NewMockWorkspaceClient(t) |
| 42 | + expected := errors.New("request failed: non-retriable error") |
| 43 | + api := w.GetMockWorkspaceAPI().EXPECT() |
| 44 | + api.GetStatusByPath(mock.Anything, mock.Anything).Return(nil, expected) |
| 45 | + _, err := RetryOnTimeout(context.Background(), func(ctx context.Context) (*workspace.ObjectInfo, error) { |
| 46 | + return w.WorkspaceClient.Workspace.GetStatusByPath(ctx, "path") |
| 47 | + }) |
| 48 | + assert.ErrorIs(t, err, expected) |
| 49 | +} |
0 commit comments