-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_test.go
More file actions
147 lines (128 loc) · 3.32 KB
/
batch_test.go
File metadata and controls
147 lines (128 loc) · 3.32 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
package limpet
import (
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"sync/atomic"
"testing"
)
func TestGetMany(t *testing.T) {
cl := setupClient(t)
ctx := t.Context()
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/plain")
_, _ = w.Write([]byte("ok:" + r.URL.Path))
}))
t.Cleanup(svr.Close)
const n = 10
urls := make([]string, n)
for i := range urls {
urls[i] = fmt.Sprintf("%s/page/%d", svr.URL, i)
}
var mu sync.Mutex
got := make(map[string]bool)
err := cl.GetMany(ctx, urls, 3, DoConfig{}, func(url string, page *Page, fetchErr error) error {
if fetchErr != nil {
return fetchErr
}
mu.Lock()
got[url] = true
mu.Unlock()
return nil
})
if err != nil {
t.Fatalf("GetMany: %v", err)
}
if len(got) != n {
t.Errorf("fetched %d URLs, want %d", len(got), n)
}
for _, u := range urls {
if !got[u] {
t.Errorf("missing URL %s", u)
}
}
}
func TestGetManyRespectsConcurrency(t *testing.T) {
cl := setupClient(t)
ctx := t.Context()
const maxConcurrency = 2
var inflight atomic.Int32
var maxSeen atomic.Int32
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cur := inflight.Add(1)
// Track the maximum concurrency observed.
for {
old := maxSeen.Load()
if cur <= old || maxSeen.CompareAndSwap(old, cur) {
break
}
}
// Yield to let other goroutines run, making overlap more likely.
// No sleep -- just a non-trivial response body.
_, _ = w.Write([]byte("data"))
inflight.Add(-1)
}))
t.Cleanup(svr.Close)
urls := make([]string, 20)
for i := range urls {
urls[i] = fmt.Sprintf("%s/%d", svr.URL, i)
}
err := cl.GetMany(ctx, urls, maxConcurrency, DoConfig{Replace: true}, func(_ string, _ *Page, fetchErr error) error {
return fetchErr
})
if err != nil {
t.Fatalf("GetMany: %v", err)
}
if m := maxSeen.Load(); m > int32(maxConcurrency) {
t.Errorf("max concurrent requests = %d, want <= %d", m, maxConcurrency)
}
}
func TestGetManyCallbackError(t *testing.T) {
cl := setupClient(t)
ctx := t.Context()
var served atomic.Int32
svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
served.Add(1)
_, _ = w.Write([]byte("ok"))
}))
t.Cleanup(svr.Close)
urls := make([]string, 50)
for i := range urls {
urls[i] = fmt.Sprintf("%s/%d", svr.URL, i)
}
sentinel := fmt.Errorf("stop here")
var callbackCount atomic.Int32
err := cl.GetMany(ctx, urls, 1, DoConfig{Replace: true}, func(_ string, _ *Page, fetchErr error) error {
if fetchErr != nil {
return fetchErr
}
n := callbackCount.Add(1)
if n >= 3 {
return sentinel
}
return nil
})
if err == nil {
t.Fatal("expected error from GetMany, got nil")
}
if !errors.Is(err, sentinel) {
t.Errorf("got error %v, want sentinel", err)
}
// With concurrency=1 and early cancel, we should not have fetched all 50.
if s := served.Load(); s >= 50 {
t.Errorf("served %d requests, expected early stop well before 50", s)
}
}
func TestGetManyEmptyURLs(t *testing.T) {
cl := setupClient(t)
ctx := t.Context()
err := cl.GetMany(ctx, nil, 3, DoConfig{}, func(_ string, _ *Page, _ error) error {
t.Fatal("callback should not be called for empty URLs")
return nil
})
if err != nil {
t.Fatalf("expected nil error for empty URLs, got %v", err)
}
}