|
| 1 | +package core |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "sync/atomic" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | +) |
| 11 | + |
| 12 | +// stubTelegramSDKFetch swaps the upstream GET for the duration of a test and |
| 13 | +// reports how many times it was called. |
| 14 | +func stubTelegramSDKFetch(t *testing.T, fn func(ctx context.Context) ([]byte, error)) *atomic.Int32 { |
| 15 | + t.Helper() |
| 16 | + var calls atomic.Int32 |
| 17 | + prev := telegramSDKFetch |
| 18 | + telegramSDKFetch = func(ctx context.Context) ([]byte, error) { |
| 19 | + calls.Add(1) |
| 20 | + return fn(ctx) |
| 21 | + } |
| 22 | + t.Cleanup(func() { telegramSDKFetch = prev }) |
| 23 | + return &calls |
| 24 | +} |
| 25 | + |
| 26 | +// waitTelegramSDKIdle blocks until no fetch is in flight. Background refreshes |
| 27 | +// outlive the call that spawned them, so a test must settle them before its cleanup |
| 28 | +// restores telegramSDKFetch — otherwise the in-flight goroutine reads the var as it |
| 29 | +// is reassigned (a test-only race; production never reassigns it). |
| 30 | +func waitTelegramSDKIdle(t *testing.T, m *Manager) { |
| 31 | + t.Helper() |
| 32 | + deadline := time.Now().Add(5 * time.Second) |
| 33 | + for { |
| 34 | + m.tgSDKMu.Lock() |
| 35 | + idle := m.tgSDKWait == nil |
| 36 | + m.tgSDKMu.Unlock() |
| 37 | + if idle { |
| 38 | + return |
| 39 | + } |
| 40 | + if time.Now().After(deadline) { |
| 41 | + t.Fatal("a telegram SDK fetch is still in flight after 5s") |
| 42 | + } |
| 43 | + time.Sleep(5 * time.Millisecond) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +// A cold cache must fetch INLINE and hand the real body to the very first caller — |
| 48 | +// that's the whole point of the cold path (an empty file would silently disable the |
| 49 | +// Mini App bridge for whoever loads the page first). |
| 50 | +func TestTelegramSDKColdFetchesInline(t *testing.T) { |
| 51 | + calls := stubTelegramSDKFetch(t, func(context.Context) ([]byte, error) { |
| 52 | + return []byte("// WebView"), nil |
| 53 | + }) |
| 54 | + m := &Manager{} |
| 55 | + |
| 56 | + body, ok := m.TelegramWebAppSDK() |
| 57 | + if !ok || string(body) != "// WebView" { |
| 58 | + t.Fatalf("cold read: got (%q, %v), want the fetched body", body, ok) |
| 59 | + } |
| 60 | + if got := calls.Load(); got != 1 { |
| 61 | + t.Fatalf("upstream called %d times, want 1", got) |
| 62 | + } |
| 63 | + |
| 64 | + // Second read is served from cache — no new fetch. |
| 65 | + if body, ok = m.TelegramWebAppSDK(); !ok || string(body) != "// WebView" { |
| 66 | + t.Fatalf("warm read: got (%q, %v)", body, ok) |
| 67 | + } |
| 68 | + if got := calls.Load(); got != 1 { |
| 69 | + t.Fatalf("warm read refetched (%d calls), want still 1", got) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +// An unreachable telegram.org must degrade to "empty body" AND arm the cooldown, so |
| 74 | +// only the first caller pays the timeout. Without this, every page load would block |
| 75 | +// for the full budget whenever upstream is down. |
| 76 | +func TestTelegramSDKUnreachableArmsCooldown(t *testing.T) { |
| 77 | + calls := stubTelegramSDKFetch(t, func(context.Context) ([]byte, error) { |
| 78 | + return nil, errors.New("dial tcp: connection refused") |
| 79 | + }) |
| 80 | + m := &Manager{} |
| 81 | + |
| 82 | + if body, ok := m.TelegramWebAppSDK(); ok || body != nil { |
| 83 | + t.Fatalf("failed fetch: got (%q, %v), want (nil, false)", body, ok) |
| 84 | + } |
| 85 | + if got := calls.Load(); got != 1 { |
| 86 | + t.Fatalf("upstream called %d times, want 1", got) |
| 87 | + } |
| 88 | + |
| 89 | + // Within the cooldown: return immediately, do NOT retry upstream. |
| 90 | + for i := 0; i < 5; i++ { |
| 91 | + if _, ok := m.TelegramWebAppSDK(); ok { |
| 92 | + t.Fatal("expected ok=false during cooldown") |
| 93 | + } |
| 94 | + } |
| 95 | + if got := calls.Load(); got != 1 { |
| 96 | + t.Fatalf("cooldown was not honoured: %d upstream calls, want 1", got) |
| 97 | + } |
| 98 | + |
| 99 | + // Once the cooldown lapses, it tries again. |
| 100 | + m.tgSDKMu.Lock() |
| 101 | + m.tgSDKFailAt = time.Now().Add(-telegramSDKRetryGap - time.Second) |
| 102 | + m.tgSDKMu.Unlock() |
| 103 | + if _, ok := m.TelegramWebAppSDK(); ok { |
| 104 | + t.Fatal("still failing upstream, expected ok=false") |
| 105 | + } |
| 106 | + if got := calls.Load(); got != 2 { |
| 107 | + t.Fatalf("after cooldown: %d upstream calls, want 2", got) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +// Concurrent cold readers must collapse onto ONE upstream fetch and all receive the |
| 112 | +// body — a thundering herd on a cold cache would otherwise hammer telegram.org once |
| 113 | +// per page load. |
| 114 | +func TestTelegramSDKConcurrentColdSingleflight(t *testing.T) { |
| 115 | + release := make(chan struct{}) |
| 116 | + calls := stubTelegramSDKFetch(t, func(context.Context) ([]byte, error) { |
| 117 | + <-release // hold the fetch open so every goroutine piles up behind it |
| 118 | + return []byte("// WebView"), nil |
| 119 | + }) |
| 120 | + m := &Manager{} |
| 121 | + |
| 122 | + const readers = 12 |
| 123 | + var wg sync.WaitGroup |
| 124 | + got := make([]bool, readers) |
| 125 | + for i := range readers { |
| 126 | + wg.Add(1) |
| 127 | + go func() { |
| 128 | + defer wg.Done() |
| 129 | + body, ok := m.TelegramWebAppSDK() |
| 130 | + got[i] = ok && string(body) == "// WebView" |
| 131 | + }() |
| 132 | + } |
| 133 | + time.Sleep(50 * time.Millisecond) // let them all reach the cold path |
| 134 | + close(release) |
| 135 | + wg.Wait() |
| 136 | + |
| 137 | + if n := calls.Load(); n != 1 { |
| 138 | + t.Fatalf("upstream called %d times, want exactly 1 (singleflight)", n) |
| 139 | + } |
| 140 | + for i, okd := range got { |
| 141 | + if !okd { |
| 142 | + t.Errorf("reader %d did not get the body", i) |
| 143 | + } |
| 144 | + } |
| 145 | +} |
| 146 | + |
| 147 | +// A stale copy is served immediately (never blocking the page) while the refresh |
| 148 | +// happens behind it. |
| 149 | +func TestTelegramSDKStaleServedImmediately(t *testing.T) { |
| 150 | + block, started := make(chan struct{}), make(chan struct{}) |
| 151 | + var once sync.Once |
| 152 | + stubTelegramSDKFetch(t, func(context.Context) ([]byte, error) { |
| 153 | + once.Do(func() { close(started) }) |
| 154 | + <-block |
| 155 | + return []byte("fresh"), nil |
| 156 | + }) |
| 157 | + m := &Manager{} |
| 158 | + m.tgSDKBody = []byte("old") |
| 159 | + m.tgSDKAt = time.Now().Add(-telegramSDKTTL - time.Minute) // stale |
| 160 | + |
| 161 | + start := time.Now() |
| 162 | + body, ok := m.TelegramWebAppSDK() |
| 163 | + elapsed := time.Since(start) |
| 164 | + if !ok || string(body) != "old" { |
| 165 | + t.Fatalf("stale read: got (%q, %v), want the old body", body, ok) |
| 166 | + } |
| 167 | + if elapsed > time.Second { |
| 168 | + t.Fatalf("stale read blocked for %v — it must not wait on the refresh", elapsed) |
| 169 | + } |
| 170 | + |
| 171 | + // Wait for the refresh to actually BEGIN before releasing it: the goroutine may |
| 172 | + // not have been scheduled yet, and "no fetch in flight" can't distinguish |
| 173 | + // "not started" from "finished". |
| 174 | + select { |
| 175 | + case <-started: |
| 176 | + case <-time.After(5 * time.Second): |
| 177 | + t.Fatal("background refresh never started") |
| 178 | + } |
| 179 | + close(block) |
| 180 | + waitTelegramSDKIdle(t, m) // let it land before cleanup restores the stub |
| 181 | + |
| 182 | + // ...and the refresh it kicked off replaced the stale copy. |
| 183 | + if body, ok := m.TelegramWebAppSDK(); !ok || string(body) != "fresh" { |
| 184 | + t.Fatalf("after refresh: got (%q, %v), want the fresh body", body, ok) |
| 185 | + } |
| 186 | +} |
0 commit comments