-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtomoe_test.go
88 lines (75 loc) · 2.09 KB
/
tomoe_test.go
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
package tomoe
import (
"context"
"io"
"testing"
"time"
)
type Todo struct {
UserID int `json:"userId"`
ID int `json:"id"`
Title string `json:"title"`
Completed bool `json:"completed"`
}
func TestSingle(t *testing.T) {
client := NewClient("https://jsonplaceholder.typicode.com", 30*time.Second, 3, 5*time.Second, nil)
ctx := context.Background()
// Single request with retries
opts := RequestOptions{
Method: "GET",
Path: "/todos/1",
}
response, err := client.Do(ctx, opts)
if err != nil {
t.Errorf("Single Request Error: %v", err.Error())
return
}
defer response.Body.Close()
// Read raw body for additional processing or error messages
body, err := io.ReadAll(response.Body)
if err != nil {
t.Errorf("Parse Body Error: %v", err.Error())
return
}
t.Logf("Success: %v", string(body))
}
func TestParrarel(t *testing.T) {
client := NewClient("https://jsonplaceholder.typicode.com", 30*time.Second, 3, 5*time.Second, nil)
ctx := context.Background()
requests := []RequestOptions{
{Method: "GET", Path: "/todos/1"},
{Method: "GET", Path: "/todos/2"},
{Method: "GET", Path: "/todos/3"},
{Method: "GET", Path: "/todos/1"},
{Method: "GET", Path: "/todos/2"},
{Method: "GET", Path: "/todos/3"},
{Method: "GET", Path: "/todos/1"},
{Method: "GET", Path: "/todos/2"},
{Method: "GET", Path: "/todos/3"},
{Method: "GET", Path: "/todos/1"},
{Method: "GET", Path: "/todos/2"},
{Method: "GET", Path: "/todos/3"},
{Method: "GET", Path: "/todos/1"},
{Method: "GET", Path: "/todos/2"},
{Method: "GET", Path: "/todos/3"},
}
responses, err := client.ParallelRequests(ctx, requests)
if err != nil {
t.Errorf("Parallel Request Error: %v", err.Error())
return
}
for i, response := range responses {
if err != nil {
t.Errorf("Single Request Error: %v", err.Error())
return
}
defer response.Body.Close()
// Read raw body for additional processing or error messages
body, err := io.ReadAll(response.Body)
if err != nil {
t.Errorf("Parse Body Error: %v", err.Error())
return
}
t.Logf("Response %d: %+v\n", i+1, string(body))
}
}