[MM-49989] Pass a context.Context to Client4 methods (#22922)

* Migrate all method in model/client4.go to accept a context.Context

* Fix th.*Client

* Fix remaining issues

* Empty commit to triger CI

* Fix test

* Add cancellation test

* Test that returned error is context.Canceled

* Fix bad merge

* Update mmctl code

---------

Co-authored-by: Mattermost Build <build@mattermost.com>
Этот коммит содержится в:
Ben Schumacher
2023-06-06 23:29:29 +02:00
коммит произвёл GitHub
родитель 7116e9267a
Коммит 6c82605df0
140 изменённых файлов: 7516 добавлений и 7333 удалений

Разница между файлами не показана из-за своего большого размера Загрузить разницу

Просмотреть файл

@@ -4,11 +4,13 @@
package model
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@@ -73,7 +75,7 @@ func TestClient4CreatePost(t *testing.T) {
}))
client := NewAPIv4Client(server.URL)
_, resp, err := client.CreatePost(post)
_, resp, err := client.CreatePost(context.Background(), post)
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
@@ -100,7 +102,52 @@ func TestClient4SetToken(t *testing.T) {
client := NewAPIv4Client(server.URL)
client.SetToken(expected)
_, resp, err := client.GetMe("")
_, resp, err := client.GetMe(context.Background(), "")
assert.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode)
}
func TestClient4RequestCancellation(t *testing.T) {
t.Run("cancel before making the reqeust", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
t.Fatal("request should not hit the server")
}))
client := NewAPIv4Client(server.URL)
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, resp, err := client.GetMe(ctx, "")
assert.Error(t, err)
assert.ErrorIs(t, err, context.Canceled)
assert.Nil(t, resp)
})
t.Run("cancel after making the reqeust", func(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(100 * time.Millisecond)
t.Fatal("request should not hit the server")
}))
client := NewAPIv4Client(server.URL)
ctx, cancel := context.WithCancel(context.Background())
done := make(chan struct{})
go func() {
_, resp, err := client.GetMe(ctx, "")
assert.Error(t, err)
assert.ErrorIs(t, err, context.Canceled)
assert.Nil(t, resp)
done <- struct{}{}
}()
cancel()
<-done
})
}