You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.1 KiB
43 lines
1.1 KiB
package telemost
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"net/http"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGetConnectionInfoHonorsContextCancellation(t *testing.T) {
|
|
previousBaseURL := apiBaseURL
|
|
previousClient := apiHTTPClient
|
|
apiBaseURL = "https://telemost.test"
|
|
apiHTTPClient = &http.Client{
|
|
Transport: roundTripperFunc(func(req *http.Request) (*http.Response, error) {
|
|
<-req.Context().Done()
|
|
return nil, req.Context().Err()
|
|
}),
|
|
}
|
|
t.Cleanup(func() {
|
|
apiBaseURL = previousBaseURL
|
|
apiHTTPClient = previousClient
|
|
})
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 50*time.Millisecond)
|
|
defer cancel()
|
|
|
|
start := time.Now()
|
|
_, _, err := GetConnectionInfo(ctx, "https://telemost.yandex.ru/j/test-room", "tester")
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
t.Fatalf("GetConnectionInfo() error = %v, want context deadline exceeded", err)
|
|
}
|
|
if elapsed := time.Since(start); elapsed > time.Second {
|
|
t.Fatalf("GetConnectionInfo() took too long to abort: %v", elapsed)
|
|
}
|
|
}
|
|
|
|
type roundTripperFunc func(*http.Request) (*http.Response, error)
|
|
|
|
func (f roundTripperFunc) RoundTrip(req *http.Request) (*http.Response, error) {
|
|
return f(req)
|
|
}
|
|
|