File: map_test.go

package info (click to toggle)
golang-github-getsentry-sentry-go 0.29.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,404 kB
  • sloc: makefile: 75; sh: 21
file content (269 lines) | stat: -rw-r--r-- 6,130 bytes parent folder | download | duplicates (2)
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
package ratelimit

import (
	"net/http"
	"testing"
	"time"

	"github.com/google/go-cmp/cmp"
)

func TestFromResponse(t *testing.T) {
	tests := []struct {
		name     string
		response *http.Response
		want     Map
	}{
		{
			"200 no rate limit",
			&http.Response{
				StatusCode: http.StatusOK,
			},
			Map{},
		},
		{
			"200 ignored Retry-After",
			&http.Response{
				StatusCode: http.StatusOK,
				Header: http.Header{
					"Retry-After": []string{"100"}, // ignored
				},
			},
			Map{},
		},
		{
			"200 Retry-After + X-Sentry-Rate-Limits",
			&http.Response{
				StatusCode: http.StatusOK,
				Header: http.Header{
					"Retry-After":          []string{"100"}, // ignored
					"X-Sentry-Rate-Limits": []string{"50:transaction"},
				},
			},
			Map{CategoryTransaction: Deadline(now.Add(50 * time.Second))},
		},
		{
			"200 X-Sentry-Rate-Limits",
			&http.Response{
				StatusCode: http.StatusOK,
				Header: http.Header{
					"X-Sentry-Rate-Limits": []string{"50:transaction"},
				},
			},
			Map{CategoryTransaction: Deadline(now.Add(50 * time.Second))},
		},
		{
			"429 no rate limit, use default",
			&http.Response{
				StatusCode: http.StatusTooManyRequests,
			},
			Map{CategoryAll: Deadline(now.Add(defaultRetryAfter))},
		},
		{
			"429 Retry-After",
			&http.Response{
				StatusCode: http.StatusTooManyRequests,
				Header: http.Header{
					"Retry-After": []string{"100"},
				},
			},
			Map{CategoryAll: Deadline(now.Add(100 * time.Second))},
		},
		{
			"429 X-Sentry-Rate-Limits",
			&http.Response{
				StatusCode: http.StatusTooManyRequests,
				Header: http.Header{
					"X-Sentry-Rate-Limits": []string{"50:error"},
				},
			},
			Map{CategoryError: Deadline(now.Add(50 * time.Second))},
		},
		{
			"429 Retry-After + X-Sentry-Rate-Limits",
			&http.Response{
				StatusCode: http.StatusTooManyRequests,
				Header: http.Header{
					"Retry-After":          []string{"100"}, // ignored
					"X-Sentry-Rate-Limits": []string{"50:error"},
				},
			},
			Map{CategoryError: Deadline(now.Add(50 * time.Second))},
		},
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			got := fromResponse(tt.response, now)
			if diff := cmp.Diff(tt.want, got); diff != "" {
				t.Errorf("(-want +got):\n%s", diff)
			}
		})
	}
}

func TestMapDeadlineIsRateLimited(t *testing.T) {
	noDeadline := Deadline{}
	plus5s := Deadline(now.Add(5 * time.Second))
	plus10s := Deadline(now.Add(10 * time.Second))
	future := now.Add(time.Hour)

	tests := []struct {
		name string
		m    Map
		want map[Category]Deadline
	}{
		{
			"Empty map = no deadlines",
			Map{},
			map[Category]Deadline{
				CategoryAll:         noDeadline,
				CategoryError:       noDeadline,
				CategoryTransaction: noDeadline,
				Category("unknown"): noDeadline,
			},
		},
		{
			"Only one category",
			Map{
				CategoryError: plus5s,
			},
			map[Category]Deadline{
				CategoryAll:         noDeadline,
				CategoryError:       plus5s,
				CategoryTransaction: noDeadline,
				Category("unknown"): noDeadline,
			},
		},
		{
			"Only CategoryAll",
			Map{
				CategoryAll: plus5s,
			},
			map[Category]Deadline{
				CategoryAll:         plus5s,
				CategoryError:       plus5s,
				CategoryTransaction: plus5s,
				Category("unknown"): plus5s,
			},
		},
		{
			"Two categories",
			Map{
				CategoryError:       plus5s,
				CategoryTransaction: plus10s,
			},
			map[Category]Deadline{
				CategoryAll:         noDeadline,
				CategoryError:       plus5s,
				CategoryTransaction: plus10s,
				Category("unknown"): noDeadline,
			},
		},
		{
			"CategoryAll earlier",
			Map{
				CategoryAll:         plus5s,
				CategoryTransaction: plus10s,
			},
			map[Category]Deadline{
				CategoryAll:         plus5s,
				CategoryError:       plus5s,
				CategoryTransaction: plus10s,
				Category("unknown"): plus5s,
			},
		},
		{
			"CategoryAll later",
			Map{
				CategoryAll:         plus10s,
				CategoryTransaction: plus5s,
			},
			map[Category]Deadline{
				CategoryAll:         plus10s,
				CategoryError:       plus10s,
				CategoryTransaction: plus10s,
				Category("unknown"): plus10s,
			},
		},
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			for c, want := range tt.want {
				got := tt.m.Deadline(c)
				if got != want {
					t.Fatalf("Deadline(%v): got %v, want %v", c, got, want)
				}
				limited := tt.m.isRateLimited(c, now)
				wantLimited := want != noDeadline
				if limited != wantLimited {
					t.Errorf("isRateLimited(%v, now): got %v, want %v", c, limited, wantLimited)
				}
				// Nothing should be rate-limited in the future
				limited = tt.m.isRateLimited(c, future)
				wantLimited = false
				if limited != wantLimited {
					t.Errorf("isRateLimited(%v, future): got %v, want %v", c, limited, wantLimited)
				}
			}
		})
	}
}

func TestMapMerge(t *testing.T) {
	tests := []struct {
		name     string
		old, new Map
		want     Map
	}{
		{
			name: "both empty",
			old:  Map{},
			new:  Map{},
			want: Map{},
		},
		{
			name: "old empty",
			old:  Map{},
			new:  Map{CategoryError: Deadline(now)},
			want: Map{CategoryError: Deadline(now)},
		},
		{
			name: "new empty",
			old:  Map{CategoryError: Deadline(now)},
			new:  Map{},
			want: Map{CategoryError: Deadline(now)},
		},
		{
			name: "no overlap = union",
			old:  Map{CategoryTransaction: Deadline(now)},
			new:  Map{CategoryError: Deadline(now)},
			want: Map{
				CategoryTransaction: Deadline(now),
				CategoryError:       Deadline(now),
			},
		},
		{
			name: "overlap keep old",
			old:  Map{CategoryError: Deadline(now.Add(time.Minute))},
			new:  Map{CategoryError: Deadline(now)},
			want: Map{CategoryError: Deadline(now.Add(time.Minute))},
		},
		{
			name: "overlap replace with new",
			old:  Map{CategoryError: Deadline(now)},
			new:  Map{CategoryError: Deadline(now.Add(time.Minute))},
			want: Map{CategoryError: Deadline(now.Add(time.Minute))},
		},
	}
	for _, tt := range tests {
		tt := tt
		t.Run(tt.name, func(t *testing.T) {
			tt.old.Merge(tt.new)
			if diff := cmp.Diff(tt.want, tt.old); diff != "" {
				t.Errorf("(-want +got):\n%s", diff)
			}
		})
	}
}