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 870833a
Show file tree
Hide file tree
Showing 6 changed files with 221 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
176 changes: 174 additions & 2 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package meilisearch

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -313,7 +315,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 +373,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 All @@ -382,3 +384,173 @@ func TestClient_GetTasks(t *testing.T) {
})
}
}

func TestClient_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"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
{
name: "TestDefaultWaitForTaskWithCustomClient",
args: args{
UID: "TestDefaultWaitForTaskWithCustomClient",
client: customClient,
taskID: &Task{
UID: 0,
},
document: []docTest{
{ID: "123", Name: "Pride and Prejudice"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.args.client
t.Cleanup(cleanup(c))

task, err := c.Index(tt.args.UID).AddDocuments(tt.args.document)
require.NoError(t, err)

gotTask, err := c.WaitForTask(task)
require.NoError(t, err)
require.Equal(t, tt.want, gotTask.Status)
})
}
}


func TestClient_WaitForTaskWithContext(t *testing.T) {
type args struct {
UID string
client *Client
interval time.Duration
timeout time.Duration
taskID *Task
document []docTest
}
tests := []struct {
name string
args args
want TaskStatus
}{
{
name: "TestWaitForTask50",
args: args{
UID: "TestWaitForTask50",
client: defaultClient,
interval: time.Millisecond * 50,
timeout: time.Second * 5,
taskID: &Task{
UID: 0,
},
document: []docTest{
{ID: "123", Name: "Pride and Prejudice"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
{
name: "TestWaitForTask50WithCustomClient",
args: args{
UID: "TestWaitForTask50WithCustomClient",
client: customClient,
interval: time.Millisecond * 50,
timeout: time.Second * 5,
taskID: &Task{
UID: 0,
},
document: []docTest{
{ID: "123", Name: "Pride and Prejudice"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
{
name: "TestWaitForTask10",
args: args{
UID: "TestWaitForTask10",
client: defaultClient,
interval: time.Millisecond * 10,
timeout: time.Second * 5,
taskID: &Task{
UID: 1,
},
document: []docTest{
{ID: "123", Name: "Pride and Prejudice"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
{
name: "TestWaitForTaskWithTimeout",
args: args{
UID: "TestWaitForTaskWithTimeout",
client: defaultClient,
interval: time.Millisecond * 50,
timeout: time.Millisecond * 10,
taskID: &Task{
UID: 1,
},
document: []docTest{
{ID: "123", Name: "Pride and Prejudice"},
{ID: "456", Name: "Le Petit Prince"},
{ID: "1", Name: "Alice In Wonderland"},
},
},
want: "succeeded",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := tt.args.client
t.Cleanup(cleanup(c))

task, err := c.Index(tt.args.UID).AddDocuments(tt.args.document)
require.NoError(t, err)

ctx, cancelFunc := context.WithTimeout(context.Background(), tt.args.timeout)
defer cancelFunc()

gotTask, err := c.WaitForTask(task, waitParams{Context: ctx, Interval: tt.args.interval})
if tt.args.timeout < tt.args.interval {
require.Error(t, err)
} else {
require.NoError(t, err)
require.Equal(t, tt.want, gotTask.Status)
}
})
}
}
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...)
}
Loading

0 comments on commit 870833a

Please sign in to comment.