test(zalo): cover empty getUpdates response + extract poll timeout headroom

Follow-up to #924:
- Add TestGetUpdates_EmptyResultReturnsNilSlice to cover the no-events
  polling branch (API returns {} when no pending updates).
- Extract magic 7s into pollTimeoutHeadroom const for readability.
This commit is contained in:
viettranx committed 2026-04-16 14:20:02 +07:00
1 parent f1e71ee8fe
commit 0a4b8df001
2 files changed
+27 -6

No files matched your search

+7 -6
View File
@@ -24,11 +24,12 @@ import (
)
const (
defaultPollTimeout = 30
maxTextLength = 2000
defaultMediaMaxMB = 5
pollErrorBackoff = 5 * time.Second
pairingDebounce = 60 * time.Second
defaultPollTimeout = 30
maxTextLength = 2000
defaultMediaMaxMB = 5
pollErrorBackoff = 5 * time.Second
pairingDebounce = 60 * time.Second
pollTimeoutHeadroom = 7 * time.Second
)
// apiBase is the Zalo Bot API root. Declared as a variable so tests can
@@ -494,7 +495,7 @@ func (c *Channel) getUpdates(timeout int) ([]zaloUpdate, error) {
"timeout": timeout,
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout+7)*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeout)*time.Second+pollTimeoutHeadroom)
defer cancel()
result, err := c.callAPIWith(ctx, c.pollClient, "getUpdates", params)
+20
View File
@@ -165,6 +165,26 @@ func TestGetUpdates_ParsesSingleUpdate(t *testing.T) {
}
}
// TestGetUpdates_EmptyResultReturnsNilSlice verifies that when the Zalo API
// returns an empty update object (no pending events), getUpdates returns nil
// instead of a slice with a zero-valued element — the poll loop treats it as
// "nothing to dispatch" without invoking processUpdate.
func TestGetUpdates_EmptyResultReturnsNilSlice(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`{"ok":true,"result":{}}`))
}))
defer srv.Close()
ch := newTestChannel(t, srv.URL)
updates, err := ch.getUpdates(1)
if err != nil {
t.Fatalf("getUpdates: %v", err)
}
if updates != nil {
t.Errorf("updates = %+v, want nil (empty result object)", updates)
}
}
// TestGetUpdates_UnmarshalError verifies a malformed result surfaces an error.
func TestGetUpdates_UnmarshalError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {