forked from olivere/elastic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtasks_get_task_test.go
147 lines (135 loc) · 3.36 KB
/
tasks_get_task_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 2012-present Oliver Eilhard. All rights reserved.
// Use of this source code is governed by a MIT-license.
// See http://olivere.mit-license.org/license.txt for details.
package elastic
import (
"context"
"errors"
"testing"
"time"
)
func TestTasksGetTaskBuildURL(t *testing.T) {
client := setupTestClient(t)
// Get specific task
got, _, err := client.TasksGetTask().TaskId("node1:123").buildURL()
if err != nil {
t.Fatal(err)
}
want := "/_tasks/node1%3A123"
if got != want {
t.Errorf("want %q; got %q", want, got)
}
// Get specific task
got, _, err = client.TasksGetTask().TaskIdFromNodeAndId("node2", 678).buildURL()
if err != nil {
t.Fatal(err)
}
want = "/_tasks/node2%3A678"
if got != want {
t.Errorf("want %q; got %q", want, got)
}
}
func TestTasksGetTask(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
// Create a reindexing task
var taskID string
{
res, err := client.Reindex().
SourceIndex(testIndexName).
DestinationIndex(testIndexName4).
DoAsync(context.Background())
if err != nil {
t.Fatalf("unable to start reindexing task: %v", err)
}
taskID = res.TaskId
}
// Get the task by ID
res, err := client.TasksGetTask().
TaskId(taskID).
Header("X-Opaque-Id", "987654").
Do(context.Background())
if err != nil {
t.Fatal(err)
}
if res == nil {
t.Fatal("response is nil")
}
if want, have := "987654", res.Header.Get("X-Opaque-Id"); want != have {
t.Fatalf("expected HTTP header %#v; got: %#v", want, have)
}
if res.Task == nil {
t.Fatal("task is nil")
}
// Elasticsearch <= 6.4.1 doesn't return the X-Opaque-Id in the body,
// only in response header.
/*
have, found := res.Task.Headers["X-Opaque-Id"]
if !found {
t.Fatalf("expected to find headers[%q]", "X-Opaque-Id")
}
if want := "987654"; want != have {
t.Fatalf("expected headers[%q]=%q; got: %q", "X-Opaque-Id", want, have)
}
*/
}
func TestTasksGetTaskWithError(t *testing.T) {
client := setupTestClientAndCreateIndexAndAddDocs(t) //, SetTraceLog(log.New(os.Stdout, "", 0)))
// Create a reindexing task
var taskID string
{
res, err := client.UpdateByQuery(testIndexName).
WaitForCompletion(false).
Conflicts("proceed").
Script(NewScript("kaboom")).
DoAsync(context.Background())
if err != nil {
t.Fatalf("unable to start update_by_query task: %v", err)
}
taskID = res.TaskId
}
var (
response *TasksGetTaskResponse
lastErr error
)
done := make(chan struct{}, 1)
go func() {
defer close(done)
for {
// Get the task by ID
res, err := client.TasksGetTask().
TaskId(taskID).
Do(context.Background())
if err != nil {
lastErr = err
return
}
if res == nil {
lastErr = errors.New("response is nil")
return
}
if res.Completed {
lastErr = nil
response = res
return
}
time.Sleep(1 * time.Second) // retry
}
}()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("expected to finish the task after 5 seconds")
}
if lastErr != nil {
t.Fatalf("expected no error, got %v", lastErr)
}
if response == nil {
t.Fatal("expected a response, got nil")
}
if response.Error == nil {
t.Fatal("expected a response with an error, got nil")
}
if want, have := "script_exception", response.Error.Type; want != have {
t.Fatalf("expected an error type of %q, got %q", want, have)
}
}