Skip to content

Commit

Permalink
Modification due to review
Browse files Browse the repository at this point in the history
  • Loading branch information
alallema committed Feb 1, 2022
1 parent d59a75b commit 1e5c2ff
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 115 deletions.
40 changes: 22 additions & 18 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{}
Expand Down Expand Up @@ -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)
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion client_index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
25 changes: 6 additions & 19 deletions index.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package meilisearch

import (
"context"
"net/http"
"strconv"
"time"
)

// IndexConfig configure the Index
Expand Down Expand Up @@ -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{}
Expand Down Expand Up @@ -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...)
}
81 changes: 12 additions & 69 deletions index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down
12 changes: 6 additions & 6 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down

0 comments on commit 1e5c2ff

Please sign in to comment.