@@ -14,6 +14,7 @@ import (
1414 "strconv"
1515 "strings"
1616 "testing"
17+ "time"
1718
1819 "github.com/stretchr/testify/assert"
1920 "github.com/stretchr/testify/require"
@@ -28,6 +29,9 @@ func TestTasksClient(t *testing.T) {
2829 t .Parallel ()
2930 client , err := testutil .NewClient (t )
3031 require .NoError (t , err )
32+ // .tasks index mapping lacked cancellation_time_millis and resource_stats fields;
33+ // cancelled tasks could never persist completion (opensearch-project/OpenSearch#16201).
34+ testutil .SkipIfVersion (t , client , "<" , "2.18" , "TestTasksClient" )
3135 failingClient , err := osapitest .CreateFailingClient (t )
3236 require .NoError (t , err )
3337
@@ -120,16 +124,6 @@ func TestTasksClient(t *testing.T) {
120124
121125 // Create unique indices for this test
122126 destIndex := testutil .MustUniqueString (t , "test-tasks-dest" )
123- testIndices := []string {sourceIndex , destIndex }
124- t .Cleanup (func () {
125- client .Indices .Delete (
126- context .Background (),
127- opensearchapi.IndicesDeleteReq {
128- Indices : testIndices ,
129- Params : opensearchapi.IndicesDeleteParams {IgnoreUnavailable : opensearchapi .ToPointer (true )},
130- },
131- )
132- })
133127
134128 // Create destination index
135129 client .Indices .Create (
@@ -157,6 +151,21 @@ func TestTasksClient(t *testing.T) {
157151 require .NotEmpty (t , resp )
158152 taskID := resp .Task
159153
154+ t .Cleanup (func () {
155+ client .Tasks .Cancel (context .Background (), opensearchapi.TasksCancelReq {TaskID : taskID })
156+ require .Eventually (t , func () bool {
157+ resp , err := client .Tasks .Get (context .Background (), opensearchapi.TasksGetReq {TaskID : taskID })
158+ return err == nil && resp .Completed
159+ }, 30 * time .Second , 100 * time .Millisecond , "reindex task did not complete after cancel" )
160+ client .Indices .Delete (
161+ context .Background (),
162+ opensearchapi.IndicesDeleteReq {
163+ Indices : []string {destIndex },
164+ Params : opensearchapi.IndicesDeleteParams {IgnoreUnavailable : opensearchapi .ToPointer (true )},
165+ },
166+ )
167+ })
168+
160169 testCases := []tasksTests {
161170 {
162171 Name : "with request" ,
@@ -204,16 +213,6 @@ func TestTasksClient(t *testing.T) {
204213
205214 // Create unique indices for this test
206215 destIndex := testutil .MustUniqueString (t , "test-tasks-dest" )
207- testIndices := []string {sourceIndex , destIndex }
208- t .Cleanup (func () {
209- client .Indices .Delete (
210- context .Background (),
211- opensearchapi.IndicesDeleteReq {
212- Indices : testIndices ,
213- Params : opensearchapi.IndicesDeleteParams {IgnoreUnavailable : opensearchapi .ToPointer (true )},
214- },
215- )
216- })
217216
218217 // Create destination index
219218 client .Indices .Create (
@@ -241,6 +240,21 @@ func TestTasksClient(t *testing.T) {
241240 require .NotEmpty (t , resp )
242241 taskID := resp .Task
243242
243+ t .Cleanup (func () {
244+ client .Tasks .Cancel (context .Background (), opensearchapi.TasksCancelReq {TaskID : taskID })
245+ require .Eventually (t , func () bool {
246+ resp , err := client .Tasks .Get (context .Background (), opensearchapi.TasksGetReq {TaskID : taskID })
247+ return err == nil && resp .Completed
248+ }, 30 * time .Second , 100 * time .Millisecond , "reindex task did not complete after cancel" )
249+ client .Indices .Delete (
250+ context .Background (),
251+ opensearchapi.IndicesDeleteReq {
252+ Indices : []string {destIndex },
253+ Params : opensearchapi.IndicesDeleteParams {IgnoreUnavailable : opensearchapi .ToPointer (true )},
254+ },
255+ )
256+ })
257+
244258 testCases := []tasksTests {
245259 {
246260 Name : "with request" ,
0 commit comments