Simplify test case

This commit is contained in:
David Robertson 2023-10-26 17:01:31 +01:00
parent 18a781ab70
commit c37e906bf5
No known key found for this signature in database
GPG Key ID: 903ECE108A39DEDD

View File

@ -2,15 +2,12 @@ package syncv3_test
import ( import (
"context" "context"
"errors"
"github.com/matrix-org/complement/client" "github.com/matrix-org/complement/client"
"github.com/matrix-org/sliding-sync/sync3" "github.com/matrix-org/sliding-sync/sync3"
"github.com/tidwall/gjson"
"io"
"net/http" "net/http"
"net/url" "net/url"
"strconv"
"testing" "testing"
"time"
) )
func TestRequestCancelledWhenItsConnIsDestroyed(t *testing.T) { func TestRequestCancelledWhenItsConnIsDestroyed(t *testing.T) {
@ -26,60 +23,34 @@ func TestRequestCancelledWhenItsConnIsDestroyed(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
timeout := time.Second
cancelWithin := 100 * time.Millisecond
client.WithQueries(url.Values{ client.WithQueries(url.Values{
"timeout": []string{strconv.FormatInt(timeout.Milliseconds(), 10)}, "timeout": []string{"1000"},
"pos": []string{aliceRes.Pos}, "pos": []string{aliceRes.Pos},
})(req) })(req)
client.WithRawBody([]byte("{}")) client.WithRawBody([]byte("{}"))
client.WithContentType("application/json")(req) client.WithContentType("application/json")(req)
req.Header.Set("Authorization", "Bearer "+alice.AccessToken) req.Header.Set("Authorization", "Bearer "+alice.AccessToken)
type secondSyncResponse struct { done := make(chan struct{})
res *http.Response
body gjson.Result
duration time.Duration
}
done := make(chan secondSyncResponse)
go func() { go func() {
t.Log("Alice makes her second sync.") t.Log("Alice makes her second sync.")
t.Log(req) t.Log(req)
start := time.Now() _, err := alice.Client.Do(req)
res, err := alice.Client.Do(req)
end := time.Now()
if err != nil { if err != nil {
t.Error(err) t.Error(err)
} }
body, err := io.ReadAll(res.Body) done <- struct{}{}
if err != nil {
t.Error(err)
}
done <- secondSyncResponse{
res: res,
body: gjson.ParseBytes(body),
duration: end.Sub(start),
}
}() }()
t.Log("Alice logs out.") t.Log("Alice logs out.")
alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "logout"}) alice.MustDo(t, "POST", []string{"_matrix", "client", "v3", "logout"})
t.Log("Alice waits for her second sync to return.") t.Log("Alice waits for her second sync response.")
response := <-done <-done
// TODO: At first I expected that this the cancelled request should return 400 M_UNKNOWN_POS. if !errors.Is(ctx.Err(), context.Canceled) {
// But I think that is best handled on the next incoming request. t.Logf("ctx.Err(): got %v, expected %v", ctx.Err(), context.Canceled)
// assertEqual(t, "status code", response.res.StatusCode, http.StatusBadRequest)
// assertEqual(t, "response errcode", response.body.Get("errcode").Str, "M_UNKNOWN_POS")
if response.duration > cancelWithin {
t.Errorf("Waited for %s, but expected second sync to cancel after at most %s", response.duration, cancelWithin)
} }
} }