-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontext_test.go
More file actions
66 lines (53 loc) · 1.69 KB
/
context_test.go
File metadata and controls
66 lines (53 loc) · 1.69 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
package disk
import (
"testing"
"time"
)
func TestContextManagement(t *testing.T) {
t.Run("NewWithConfig creates client with custom timeout", func(t *testing.T) {
config := &ClientConfig{
DefaultTimeout: 45 * time.Second,
}
client, err := NewWithConfig(config, "test-token")
if err != nil {
t.Fatal("Expected no error, got:", err)
}
if client.GetTimeout() != 45*time.Second {
t.Errorf("Expected timeout to be 45s, got %v", client.GetTimeout())
}
})
t.Run("SetTimeout updates client timeout", func(t *testing.T) {
client, _ := New("test-token")
client.SetTimeout(60 * time.Second)
if client.GetTimeout() != 60*time.Second {
t.Errorf("Expected timeout to be 60s, got %v", client.GetTimeout())
}
})
t.Run("Context helper functions work correctly", func(t *testing.T) {
// Test WithTimeout
ctx, cancel := WithTimeout(5 * time.Second)
defer cancel()
deadline, ok := ctx.Deadline()
if !ok {
t.Error("Expected context to have a deadline")
}
// Should be approximately 5 seconds from now
expectedDeadline := time.Now().Add(5 * time.Second)
if deadline.Before(expectedDeadline.Add(-100*time.Millisecond)) ||
deadline.After(expectedDeadline.Add(100*time.Millisecond)) {
t.Error("Context deadline is not approximately 5 seconds from now")
}
})
t.Run("Context cancellation is detected", func(t *testing.T) {
ctx, cancel := WithCancel()
cancel() // Cancel immediately
client, _ := New("test-token")
_, err := client.doRequest(ctx, GET, "", nil)
if err == nil {
t.Error("Expected error due to cancelled context")
}
if err.Error() != "request cancelled: context canceled" {
t.Errorf("Expected cancellation error, got: %v", err)
}
})
}