diff --git a/client.go b/client.go index 0ef5a2df..2b402102 100644 --- a/client.go +++ b/client.go @@ -23,6 +23,11 @@ type ClientConfig struct { Timeout time.Duration } +type waitParams struct { + Context context.Context + Interval time.Duration +} + // ClientInterface is interface for all Meilisearch client type ClientInterface interface { Index(uid string) *Index @@ -42,8 +47,7 @@ type ClientInterface interface { IsHealthy() bool GetTask(taskID int64) (resp *Task, err error) GetTasks() (resp *ResultTask, err error) - WaitForTask(ctx context.Context, interval time.Duration, task *Task) (*Task, error) - DefaultWaitForTask(task *Task) (*Task, error) + WaitForTask(task *Task, options ...waitParams) (*Task, error) } var _ ClientInterface = &Client{} @@ -209,24 +213,24 @@ func (c *Client) GetTasks() (resp *ResultTask, err error) { return resp, nil } -// // DefaultWaitForTask checks each 50ms the status of a task. -// // This is a default implementation of WaitForTask. -func (c *Client) DefaultWaitForTask(task *Task) (*Task, error) { - ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5) - defer cancelFunc() - return c.WaitForTask(ctx, time.Millisecond*50, task) -} - // WaitForTask waits for a task to be processed. // The function will check by regular interval provided in parameter interval -// the TaskStatus. If it is not TaskStatusEnqueued or the ctx cancelled -// we return the TaskStatus. -func (c *Client) WaitForTask( - ctx context.Context, - interval time.Duration, - task *Task) (*Task, error) { +// the TaskStatus. +// If no ctx and interval are provided WaitForTask will check each 50ms the +// status of a task. +func (c *Client) WaitForTask(task *Task, options ...waitParams) (*Task, error) { + if options == nil { + ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5) + defer cancelFunc() + options = []waitParams{ + { + Context: ctx, + Interval: time.Millisecond * 50, + }, + } + } for { - if err := ctx.Err(); err != nil { + if err := options[0].Context.Err(); err != nil { return nil, err } getTask, err := c.GetTask(task.UID) @@ -236,6 +240,6 @@ func (c *Client) WaitForTask( if getTask.Status != TaskStatusEnqueued && getTask.Status != TaskStatusProcessing { return getTask, nil } - time.Sleep(interval) + time.Sleep(options[0].Interval) } } diff --git a/client_index_test.go b/client_index_test.go index 13e8e8e4..6d5c6941 100644 --- a/client_index_test.go +++ b/client_index_test.go @@ -107,7 +107,7 @@ func TestClient_CreateIndex(t *testing.T) { // Make sure that timestamps are also retrieved require.NotZero(t, gotResp.EnqueuedAt) - _, err := c.DefaultWaitForTask(gotResp) + _, err := c.WaitForTask(gotResp) require.NoError(t, err) index, err := c.GetIndex(tt.args.config.Uid) diff --git a/client_test.go b/client_test.go index f34fd99c..1bbe57e5 100644 --- a/client_test.go +++ b/client_test.go @@ -313,7 +313,7 @@ func TestClient_GetTask(t *testing.T) { task, err := i.AddDocuments(tt.args.document) require.NoError(t, err) - _, err = c.DefaultWaitForTask(task) + _, err = c.WaitForTask(task) require.NoError(t, err) gotResp, err := c.GetTask(task.UID) @@ -371,7 +371,7 @@ func TestClient_GetTasks(t *testing.T) { task, err := i.AddDocuments(tt.args.document) require.NoError(t, err) - _, err = c.DefaultWaitForTask(task) + _, err = c.WaitForTask(task) require.NoError(t, err) gotResp, err := i.GetTasks() diff --git a/index.go b/index.go index cddab7cf..20b2e75a 100644 --- a/index.go +++ b/index.go @@ -1,10 +1,8 @@ package meilisearch import ( - "context" "net/http" "strconv" - "time" ) // IndexConfig configure the Index @@ -68,8 +66,7 @@ type IndexInterface interface { UpdateFilterableAttributes(request *[]string) (resp *Task, err error) ResetFilterableAttributes() (resp *Task, err error) - WaitForTask(ctx context.Context, interval time.Duration, task *Task) (*Task, error) - DefaultWaitForTask(task *Task) (*Task, error) + WaitForTask(task *Task, options ...waitParams) (*Task, error) } var _ IndexInterface = &Index{} @@ -193,21 +190,11 @@ func (i Index) GetTasks() (resp *ResultTask, err error) { return resp, nil } -// DefaultWaitForTask checks each 50ms the status of a WaitForTask. -// This is a default implementation of WaitForTask. -func (i Index) DefaultWaitForTask(taskID *Task) (*Task, error) { - ctx, cancelFunc := context.WithTimeout(context.Background(), time.Second*5) - defer cancelFunc() - return i.WaitForTask(ctx, time.Millisecond*50, taskID) -} - // WaitForTask waits for a task to be processed. // The function will check by regular interval provided in parameter interval -// the TaskStatus. If it is not TaskStatusEnqueued or the ctx cancelled -// we return the TaskStatus. -func (i Index) WaitForTask( - ctx context.Context, - interval time.Duration, - task *Task) (*Task, error) { - return i.client.WaitForTask(ctx, interval, task) +// the TaskStatus. +// If no ctx and interval are provided WaitForTask will check each 50ms the +// status of a task. +func (i Index) WaitForTask(task *Task, options ...waitParams) (*Task, error) { + return i.client.WaitForTask(task, options...) } diff --git a/index_test.go b/index_test.go index d7b39c6b..e13e496e 100644 --- a/index_test.go +++ b/index_test.go @@ -249,7 +249,7 @@ func TestIndex_GetTask(t *testing.T) { task, err := i.AddDocuments(tt.args.document) require.NoError(t, err) - _, err = c.DefaultWaitForTask(task) + _, err = c.WaitForTask(task) require.NoError(t, err) gotResp, err := i.GetTask(task.UID) @@ -307,7 +307,7 @@ func TestIndex_GetTasks(t *testing.T) { task, err := i.AddDocuments(tt.args.document) require.NoError(t, err) - _, err = c.DefaultWaitForTask(task) + _, err = c.WaitForTask(task) require.NoError(t, err) gotResp, err := i.GetTasks() @@ -319,63 +319,6 @@ func TestIndex_GetTasks(t *testing.T) { } } -func TestIndex_DefaultWaitForTask(t *testing.T) { - type args struct { - UID string - client *Client - taskID *Task - document []docTest - } - tests := []struct { - name string - args args - want TaskStatus - }{ - { - name: "TestDefaultWaitForTask", - args: args{ - UID: "TestDefaultWaitForTask", - client: defaultClient, - taskID: &Task{ - UID: 0, - }, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - want: "succeeded", - }, - { - name: "TestDefaultWaitForTaskWithCustomClient", - args: args{ - UID: "TestDefaultWaitForTaskWithCustomClient", - client: customClient, - taskID: &Task{ - UID: 0, - }, - document: []docTest{ - {ID: "123", Name: "Pride and Prejudice"}, - }, - }, - want: "succeeded", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - c := tt.args.client - i := c.Index(tt.args.UID) - t.Cleanup(cleanup(c)) - - task, err := i.AddDocuments(tt.args.document) - require.NoError(t, err) - - gotTask, err := i.DefaultWaitForTask(task) - require.NoError(t, err) - require.Equal(t, tt.want, gotTask.Status) - }) - } -} - func TestIndex_WaitForTask(t *testing.T) { type args struct { UID string @@ -391,9 +334,9 @@ func TestIndex_WaitForTask(t *testing.T) { want TaskStatus }{ { - name: "TestDefaultWaitForTask50", + name: "TestWaitForTask50", args: args{ - UID: "TestDefaultWaitForTask50", + UID: "TestWaitForTask50", client: defaultClient, interval: time.Millisecond * 50, timeout: time.Second * 5, @@ -409,9 +352,9 @@ func TestIndex_WaitForTask(t *testing.T) { want: "succeeded", }, { - name: "TestDefaultWaitForTask50WithCustomClient", + name: "TestWaitForTask50WithCustomClient", args: args{ - UID: "TestDefaultWaitForTask50WithCustomClient", + UID: "TestWaitForTask50WithCustomClient", client: customClient, interval: time.Millisecond * 50, timeout: time.Second * 5, @@ -427,9 +370,9 @@ func TestIndex_WaitForTask(t *testing.T) { want: "succeeded", }, { - name: "TestDefaultWaitForTask10", + name: "TestWaitForTask10", args: args{ - UID: "TestDefaultWaitForTask10", + UID: "TestWaitForTask10", client: defaultClient, interval: time.Millisecond * 10, timeout: time.Second * 5, @@ -445,9 +388,9 @@ func TestIndex_WaitForTask(t *testing.T) { want: "succeeded", }, { - name: "TestDefaultWaitForTaskWithTimeout", + name: "TestWaitForTaskWithTimeout", args: args{ - UID: "TestDefaultWaitForTaskWithTimeout", + UID: "TestWaitForTaskWithTimeout", client: defaultClient, interval: time.Millisecond * 50, timeout: time.Millisecond * 10, @@ -475,7 +418,7 @@ func TestIndex_WaitForTask(t *testing.T) { ctx, cancelFunc := context.WithTimeout(context.Background(), tt.args.timeout) defer cancelFunc() - gotTask, err := i.WaitForTask(ctx, tt.args.interval, task) + gotTask, err := i.WaitForTask(task, waitParams{Context: ctx, Interval: tt.args.interval}) if tt.args.timeout < tt.args.interval { require.Error(t, err) } else { @@ -633,7 +576,7 @@ func TestIndex_UpdateIndex(t *testing.T) { gotResp, err := i.UpdateIndex(tt.args.primaryKey) require.NoError(t, err) - _, err = c.DefaultWaitForTask(gotResp) + _, err = c.WaitForTask(gotResp) require.NoError(t, err) require.NoError(t, err) diff --git a/main_test.go b/main_test.go index 7eb596ff..018cc261 100644 --- a/main_test.go +++ b/main_test.go @@ -30,7 +30,7 @@ func deleteAllIndexes(client ClientInterface) (ok bool, err error) { for _, index := range list { task, _ := client.DeleteIndex(index.UID) - _, err := client.DefaultWaitForTask(task) + _, err := client.WaitForTask(task) if err != nil { return false, err } @@ -46,13 +46,13 @@ func cleanup(c ClientInterface) func() { } func testWaitForTask(t *testing.T, i *Index, u *Task) { - _, err := i.DefaultWaitForTask(u) + _, err := i.WaitForTask(u) require.NoError(t, err) } func testWaitForBatchTask(t *testing.T, i *Index, u []Task) { for _, id := range u { - _, err := i.DefaultWaitForTask(&id) + _, err := i.WaitForTask(&id) require.NoError(t, err) } } @@ -67,7 +67,7 @@ func SetUpEmptyIndex(index *IndexConfig) (resp *Index, err error) { fmt.Println(err) return nil, err } - finalTask, _ := client.DefaultWaitForTask(task) + finalTask, _ := client.WaitForTask(task) if finalTask.Status != "succeeded" { os.Exit(1) } @@ -94,7 +94,7 @@ func SetUpBasicIndex(indexUID string) { fmt.Println(err) os.Exit(1) } - finalTask, _ := index.DefaultWaitForTask(task) + finalTask, _ := index.WaitForTask(task) if finalTask.Status != "succeeded" { os.Exit(1) } @@ -134,7 +134,7 @@ func SetUpIndexForFaceting() { fmt.Println(err) os.Exit(1) } - finalTask, _ := index.DefaultWaitForTask(task) + finalTask, _ := index.WaitForTask(task) if finalTask.Status != "succeeded" { os.Exit(1) }