X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/4b063ca25798c78cc03d1d07f67b02c94c95248d..fc51ee2f545233df3cc1fa2fd4f28a4484c6bc00:/botapi_test.go?ds=inline diff --git a/botapi_test.go b/botapi_test.go index b571c6a..e182747 100644 --- a/botapi_test.go +++ b/botapi_test.go @@ -1,6 +1,7 @@ package main import ( + "context" "encoding/json" "fmt" "io" @@ -56,6 +57,33 @@ func TestSendMessageSendsPlainText(t *testing.T) { }, server.Calls()[0].params) } +func TestSendMessageTimesOutOnAStalledConnection(t *testing.T) { + server := newFakeBot(t) + server.stall = func(call fakeBotCall) bool { + return call.method == "sendMessage" + } + + api := newBotAPI(server.URL, testBotToken) + api.sendTimeout = 50 * time.Millisecond + start := time.Now() + err := api.SendMessage(t.Context(), testOwner, "hi") + require.ErrorIs(t, err, errBotNetwork) + require.Less(t, time.Since(start), 5*time.Second) + require.NotContains(t, err.Error(), testBotToken) + require.NotContains(t, err.Error(), server.URL) + + retry, _ := retryable(err) + require.True(t, retry) + + api.sendTimeout = time.Hour + ctx, cancel := context.WithCancel(t.Context()) + time.AfterFunc(50*time.Millisecond, cancel) + start = time.Now() + err = api.SendMessage(ctx, testOwner, "hi") + require.Error(t, err) + require.Less(t, time.Since(start), 5*time.Second) +} + func TestBotErrorsDoNotHoldTheToken(t *testing.T) { testCases := []struct { name string @@ -142,8 +170,9 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) { // fakeBot is a fake Bot API server for testBotToken. getUpdates returns the // added updates from the offset on, or waits a short time when there are none. // The other methods succeed, unless fail gives an error answer for a call. An -// answer with HTTP 429 asks for a wait of one second. Fail runs with the mutex -// held. +// answer with HTTP 429 asks for a wait of one second. When stall is true for a +// call, the call gets no answer until the client gives it up or the test ends. +// Fail and stall run with the mutex held. type fakeBot struct { *httptest.Server @@ -151,7 +180,9 @@ type fakeBot struct { updates []botUpdate calls []fakeBotCall fail func(call fakeBotCall) (int, string) + stall func(call fakeBotCall) bool added chan struct{} + stopped chan struct{} } // fakeBotCall is one call of the fake Bot API. OK tells if the call got a @@ -167,11 +198,15 @@ func newFakeBot(t *testing.T) *fakeBot { t.Helper() f := &fakeBot{ - added: make(chan struct{}, 1), + added: make(chan struct{}, 1), + stopped: make(chan struct{}), } f.Server = httptest.NewServer(http.HandlerFunc(f.serve)) t.Cleanup(f.Close) + t.Cleanup(func() { + close(f.stopped) + }) return f } @@ -233,8 +268,19 @@ func (f *fakeBot) serve(w http.ResponseWriter, r *http.Request) { status, description = f.fail(call) } + stall := f.stall != nil && f.stall(call) f.mutex.Unlock() + if stall { + f.record(call) + select { + case <-r.Context().Done(): + case <-f.stopped: + } + + return + } + if status != 0 { f.record(call) w.WriteHeader(status)