-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathruntime_test.go
More file actions
107 lines (95 loc) · 2.5 KB
/
runtime_test.go
File metadata and controls
107 lines (95 loc) · 2.5 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
package main
import (
"context"
"net/http"
"strings"
"testing"
"time"
)
func TestShouldRetryMessageFetchStatusErrors(t *testing.T) {
testCases := []struct {
name string
err error
want bool
}{
{
name: "rate limited",
err: &messageFetchStatusError{
StatusCode: http.StatusTooManyRequests,
Status: "429 Too Many Requests",
},
want: true,
},
{
name: "server error",
err: &messageFetchStatusError{
StatusCode: http.StatusBadGateway,
Status: "502 Bad Gateway",
},
want: true,
},
{
name: "client error",
err: &messageFetchStatusError{
StatusCode: http.StatusBadRequest,
Status: "400 Bad Request",
},
want: false,
},
{
name: "cancelled",
err: context.Canceled,
want: false,
},
{
name: "deadline exceeded",
err: context.DeadlineExceeded,
want: true,
},
}
for _, tc := range testCases {
if got := shouldRetryMessageFetch(tc.err); got != tc.want {
t.Fatalf("%s: expected retry=%v, got %v", tc.name, tc.want, got)
}
}
}
func TestMessageFetchBackoffCapsAtMaximum(t *testing.T) {
if got := messageFetchBackoff(1); got != messageFetchRetryBaseDelay {
t.Fatalf("expected first backoff %v, got %v", messageFetchRetryBaseDelay, got)
}
got := messageFetchBackoff(10)
if got != messageFetchRetryMaxDelay {
t.Fatalf("expected backoff to cap at %v, got %v", messageFetchRetryMaxDelay, got)
}
}
func TestConversationLoadErrorTextForUnauthorized(t *testing.T) {
mainText, secondaryText := conversationLoadErrorText(&messageFetchStatusError{
StatusCode: http.StatusUnauthorized,
Status: "401 Unauthorized",
})
if !strings.Contains(mainText, "Teams rejected") {
t.Fatalf("expected auth failure title, got %q", mainText)
}
if !strings.Contains(secondaryText, "tokens may have expired") {
t.Fatalf("expected auth guidance, got %q", secondaryText)
}
}
func TestConversationLoadErrorTextForTimeout(t *testing.T) {
mainText, secondaryText := conversationLoadErrorText(context.DeadlineExceeded)
if !strings.Contains(mainText, "Timed out") {
t.Fatalf("expected timeout title, got %q", mainText)
}
if !strings.Contains(secondaryText, "request timeout") {
t.Fatalf("expected timeout guidance, got %q", secondaryText)
}
}
func TestRequestStopCancelsRuntimeContext(t *testing.T) {
state := AppState{}
state.initRuntime(context.Background())
state.requestStop()
select {
case <-state.appContext().Done():
case <-time.After(100 * time.Millisecond):
t.Fatal("expected runtime context to be cancelled")
}
}