]> git.giacomolli.uk - onechat/blobdiff - telegram_api_test.go
Sync agent queue replacements before success
[onechat] / telegram_api_test.go
index 98aa232a9539894007921f177c3622c81a88462d..4395d6b3d0e00f11bff8c9c13718782460f7270e 100644 (file)
@@ -6,6 +6,7 @@ import (
        "net/http"
        "net/http/httptest"
        "testing"
        "net/http"
        "net/http/httptest"
        "testing"
+       "testing/synctest"
        "time"
 
        "github.com/stretchr/testify/require"
        "time"
 
        "github.com/stretchr/testify/require"
@@ -28,7 +29,7 @@ func TestGetUpdatesAsksForMessagesFromTheOffset(t *testing.T) {
        require.Equal(t, "getUpdates", call.method)
        require.Equal(t, map[string]any{
                "offset":          float64(7),
        require.Equal(t, "getUpdates", call.method)
        require.Equal(t, map[string]any{
                "offset":          float64(7),
-               "timeout":         float64(telegramPollSeconds),
+               "timeout":         float64(telegramUpdateWaitTimeout / time.Second),
                "allowed_updates": []any{"message"},
        }, call.params)
 }
                "allowed_updates": []any{"message"},
        }, call.params)
 }
@@ -103,31 +104,34 @@ func TestSendCallsTimeOutOnAStalledConnection(t *testing.T) {
 
        for _, tc := range testCases {
                t.Run(tc.method, func(t *testing.T) {
 
        for _, tc := range testCases {
                t.Run(tc.method, func(t *testing.T) {
-                       server := newTelegramFake(t)
-                       server.stall = func(call TelegramFakeCall) bool {
-                               return call.method == tc.method
-                       }
-
-                       api := newTelegramAPI(server.URL, telegramTestToken)
-                       api.sendTimeout = 50 * time.Millisecond
-                       start := time.Now()
-                       err := tc.send(t.Context(), api)
-                       require.EqualError(t, err, "no answer in 50ms")
-                       require.Less(t, time.Since(start), 5*time.Second)
-                       require.NotContains(t, err.Error(), telegramTestToken)
-                       require.NotContains(t, err.Error(), server.URL)
+                       synctest.Test(t, func(t *testing.T) {
+                               server := newTelegramLocalFake(t)
+                               server.stall = func(call TelegramFakeCall) bool {
+                                       return call.method == tc.method
+                               }
+
+                               api := newTelegramAPI(server.URL, telegramTestToken)
+                               api.http.Transport = server
+                               start := time.Now()
+                               err := tc.send(t.Context(), api)
+                               require.EqualError(t, err, "no answer in 30s")
+                               require.Equal(t, telegramSendTimeout, time.Since(start))
+                               require.NotContains(t, err.Error(), telegramTestToken)
+                               require.NotContains(t, err.Error(), server.URL)
 
 
-                       retry, _ := telegramRetryable(err)
-                       require.True(t, retry)
+                               retry, _ := telegramRetryable(err)
+                               require.True(t, retry)
 
 
-                       api.sendTimeout = time.Hour
-                       ctx, cancel := context.WithCancel(t.Context())
-                       time.AfterFunc(50*time.Millisecond, cancel)
+                               ctx, cancel := context.WithCancel(t.Context())
+                               defer cancel()
 
 
-                       start = time.Now()
-                       err = tc.send(ctx, api)
-                       require.Error(t, err)
-                       require.Less(t, time.Since(start), 5*time.Second)
+                               time.AfterFunc(time.Second, cancel)
+
+                               start = time.Now()
+                               err = tc.send(ctx, api)
+                               require.Error(t, err)
+                               require.Equal(t, time.Second, time.Since(start))
+                       })
                })
        }
 }
                })
        }
 }
@@ -145,9 +149,13 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        name: "refused connection is retried",
                        reply: func(w http.ResponseWriter) {
                                conn, _, err := http.NewResponseController(w).Hijack()
                        name: "refused connection is retried",
                        reply: func(w http.ResponseWriter) {
                                conn, _, err := http.NewResponseController(w).Hijack()
-                               if err == nil {
-                                       _ = conn.Close()
+                               if err != nil {
+                                       panic(err)
                                }
                                }
+
+                               // This forced disconnect has no response or
+                               // retry after close.
+                               conn.Close() // nolint: errcheck
                        },
                        retry:     true,
                        errorText: "no answer",
                        },
                        retry:     true,
                        errorText: "no answer",
@@ -157,7 +165,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusTooManyRequests)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusTooManyRequests)
 
-                               _, _ = io.WriteString(w, `{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 3","parameters":{"retry_after":3}}`)
+                               _, err := io.WriteString(w, `{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 3","parameters":{"retry_after":3}}`)
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     true,
                        wait:      3 * time.Second,
                        },
                        retry:     true,
                        wait:      3 * time.Second,
@@ -168,7 +179,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusBadGateway)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusBadGateway)
 
-                               _, _ = io.WriteString(w, "<html>bad gateway</html>")
+                               _, err := io.WriteString(w, "<html>bad gateway</html>")
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     true,
                        errorText: "502 Bad Gateway",
                        },
                        retry:     true,
                        errorText: "502 Bad Gateway",
@@ -178,7 +192,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusUnauthorized)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusUnauthorized)
 
-                               _, _ = io.WriteString(w, `{"ok":false,"error_code":401,"description":"Unauthorized: bot`+telegramTestToken+`"}`)
+                               _, err := io.WriteString(w, `{"ok":false,"error_code":401,"description":"Unauthorized: bot`+telegramTestToken+`"}`)
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     false,
                        errorText: "Unauthorized: botTOKEN",
                        },
                        retry:     false,
                        errorText: "Unauthorized: botTOKEN",
@@ -188,7 +205,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusBadRequest)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusBadRequest)
 
-                               _, _ = io.WriteString(w, `{"ok":false,"error_code":400,"description":"Bad Request: can't parse"}`)
+                               _, err := io.WriteString(w, `{"ok":false,"error_code":400,"description":"Bad Request: can't parse"}`)
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     false,
                        rejected:  true,
                        },
                        retry:     false,
                        rejected:  true,
@@ -199,7 +219,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusNotFound)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusNotFound)
 
-                               _, _ = io.WriteString(w, `{"ok":false,"error_code":404,"description":"Not Found"}`)
+                               _, err := io.WriteString(w, `{"ok":false,"error_code":404,"description":"Not Found"}`)
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     false,
                        rejected:  true,
                        },
                        retry:     false,
                        rejected:  true,
@@ -210,7 +233,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusNotFound)
 
                        reply: func(w http.ResponseWriter) {
                                w.WriteHeader(http.StatusNotFound)
 
-                               _, _ = io.WriteString(w, "<html>not found</html>")
+                               _, err := io.WriteString(w, "<html>not found</html>")
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     false,
                        errorText: "404 Not Found",
                        },
                        retry:     false,
                        errorText: "404 Not Found",
@@ -218,7 +244,10 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                {
                        name: "broken answer is retried",
                        reply: func(w http.ResponseWriter) {
                {
                        name: "broken answer is retried",
                        reply: func(w http.ResponseWriter) {
-                               _, _ = io.WriteString(w, `{"ok":`)
+                               _, err := io.WriteString(w, `{"ok":`)
+                               if err != nil {
+                                       panic(err)
+                               }
                        },
                        retry:     true,
                        errorText: "invalid JSON",
                        },
                        retry:     true,
                        errorText: "invalid JSON",
@@ -230,6 +259,7 @@ func TestBotErrorsDoNotHoldTheToken(t *testing.T) {
                        server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
                                tc.reply(w)
                        }))
                        server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
                                tc.reply(w)
                        }))
+
                        t.Cleanup(server.Close)
 
                        err := newTelegramAPI(server.URL, telegramTestToken).SendMessage(t.Context(), telegramTestOwner, "hi", nil)
                        t.Cleanup(server.Close)
 
                        err := newTelegramAPI(server.URL, telegramTestToken).SendMessage(t.Context(), telegramTestOwner, "hi", nil)