Repository navigation
Expand file tree
/
Copy pathclient_runtime_test.go
More file actions
173 lines (157 loc) · 5.41 KB
/
Copy pathclient_runtime_test.go
File metadata and controls
173 lines (157 loc) · 5.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) 2026 Rajeh Taher
//
// Licensed under the MIT License. See LICENSE-MIT for details.
package whatsmeow
import (
"context"
"fmt"
"net/http"
"runtime"
"sync"
"testing"
"time"
waBinary "github.com/polymorfa/hypermeow/binary"
"github.com/polymorfa/hypermeow/store"
waLog "github.com/polymorfa/hypermeow/util/log"
)
func TestPutBoundedCache(t *testing.T) {
cache := make(map[string]int, 2)
putBoundedCache(cache, "one", 1, 2)
putBoundedCache(cache, "two", 2, 2)
putBoundedCache(cache, "three", 3, 2)
if len(cache) != 2 {
t.Fatalf("cache grew to %d entries", len(cache))
}
if cache["three"] != 3 {
t.Fatal("new value was not cached")
}
putBoundedCache(cache, "three", 4, 2)
if len(cache) != 2 || cache["three"] != 4 {
t.Fatal("updating a value changed the cache size")
}
}
func TestPruneExpiredCache(t *testing.T) {
now := time.Now()
cache := map[string]time.Time{
"old": now.Add(-2 * time.Hour),
"new": now,
}
pruneExpiredCache(cache, now.Add(-time.Hour))
if _, ok := cache["old"]; ok {
t.Fatal("expired entry was retained")
}
if _, ok := cache["new"]; !ok {
t.Fatal("fresh entry was removed")
}
}
func TestClientCachesStayBounded(t *testing.T) {
messageRetries := make(map[string]int, maxMessageRetryEntries)
for i := 0; i <= maxMessageRetryEntries; i++ {
key := fmt.Sprintf("message-%d", i)
putBoundedCache(messageRetries, key, i, maxMessageRetryEntries)
}
if len(messageRetries) != maxMessageRetryEntries {
t.Fatalf("message retry cache grew to %d entries", len(messageRetries))
}
}
func TestIncrementBoundedCounterFailsClosedAndResets(t *testing.T) {
now := time.Now()
resetAt := now
cache := map[string]int{"one": 1}
if _, accepted := incrementBoundedCounter(cache, "two", 1, &resetAt, now); accepted {
t.Fatal("accepted a new counter after reaching capacity")
}
count, accepted := incrementBoundedCounter(cache, "two", 1, &resetAt, now.Add(retryCounterWindow))
if !accepted || count != 1 {
t.Fatalf("counter did not reset: accepted=%t count=%d", accepted, count)
}
if _, exists := cache["one"]; exists {
t.Fatal("old counter survived reset")
}
}
func TestNewClientDefersSparseState(t *testing.T) {
client := NewClient(&store.Device{}, waLog.Noop)
if client.messageRetries != nil || client.incomingRetryRequestCounter != nil || client.appStateKeyRequests != nil {
t.Fatal("retry or app-state maps allocated before use")
}
if client.groupCache != nil || client.userDevicesCache != nil || client.sessionRecreateHistory != nil {
t.Fatal("routing caches allocated before use")
}
if client.pendingPhoneRerequests != nil || client.responseWaiters != nil || client.tcTokenSenderTS != nil {
t.Fatal("request maps allocated before use")
}
if client.handlerQueueWait != nil {
t.Fatal("handler queue allocated before connection")
}
if client.mediaHTTP != client.websocketHTTP || client.websocketHTTP != client.preLoginHTTP {
t.Fatal("default HTTP clients do not share immutable configuration")
}
if client.mediaHTTP.Transport != sharedHTTPTransport || client.websocketHTTP.Transport != sharedHTTPTransport || client.preLoginHTTP.Transport != sharedHTTPTransport {
t.Fatal("default HTTP clients do not share the transport")
}
}
func TestHandlerQueueOverflowForcesReconnect(t *testing.T) {
client := NewClient(&store.Device{}, waLog.Noop)
queue := make(chan *waBinary.Node, handlerQueueSize)
for range cap(queue) {
queue <- &waBinary.Node{Tag: "message"}
}
client.enqueueNode(context.Background(), queue, &waBinary.Node{Tag: "message"})
if !client.forceAutoReconnect.Load() {
t.Fatal("handler queue overflow did not force a reconnect")
}
}
func TestSetMediaHTTPClientDoesNotChangeOtherClients(t *testing.T) {
client := NewClient(&store.Device{}, waLog.Noop)
websocketClient := client.websocketHTTP
mediaClient := &http.Client{Timeout: 42}
client.SetMediaHTTPClient(mediaClient)
if client.mediaHTTP != mediaClient {
t.Fatal("media HTTP client was not replaced")
}
if client.websocketHTTP != websocketClient || client.preLoginHTTP != websocketClient {
t.Fatal("replacing media HTTP client changed websocket clients")
}
}
func TestSetProxyClonesSharedHTTPClients(t *testing.T) {
client := NewClient(&store.Device{}, waLog.Noop)
defaultClient := client.mediaHTTP
client.SetProxy(nil, SetProxyOptions{NoMedia: true})
if client.mediaHTTP != defaultClient {
t.Fatal("NoMedia proxy option replaced the media client")
}
if client.websocketHTTP == defaultClient || client.preLoginHTTP == defaultClient {
t.Fatal("proxy transport mutated the shared default client")
}
if defaultClient.Transport != sharedHTTPTransport {
t.Fatal("proxy transport mutated shared default transport")
}
}
func BenchmarkNewClient(b *testing.B) {
device := &store.Device{}
clients := make([]*Client, 0, b.N)
b.ReportAllocs()
b.ResetTimer()
for range b.N {
clients = append(clients, NewClient(device, waLog.Noop))
}
runtime.KeepAlive(clients)
}
func TestRefreshMediaConnConcurrentCacheReads(t *testing.T) {
cached := &MediaConn{TTL: 60, FetchedAt: time.Now(), Hosts: []MediaConnHost{{Hostname: "media.example.test"}}}
client := &Client{mediaConnCache: cached}
var wait sync.WaitGroup
for range 32 {
wait.Add(1)
go func() {
defer wait.Done()
got, err := client.refreshMediaConn(context.Background(), false)
if err != nil {
t.Errorf("refreshMediaConn failed: %v", err)
} else if got != cached {
t.Errorf("refreshMediaConn returned %p, want %p", got, cached)
}
}()
}
wait.Wait()
}