-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors_test.go
More file actions
88 lines (75 loc) 路 2.55 KB
/
Copy patherrors_test.go
File metadata and controls
88 lines (75 loc) 路 2.55 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
package bskyoauth
import (
"context"
"errors"
"fmt"
"net"
"os"
"testing"
"time"
)
// mockTimeoutError is a mock implementation of net.Error with Timeout() returning true
type mockTimeoutError struct {
error
}
func (e *mockTimeoutError) Timeout() bool { return true }
func (e *mockTimeoutError) Temporary() bool { return false }
func TestIsTimeoutError_WrappedContextDeadlineExceeded(t *testing.T) {
err := fmt.Errorf("wrapped error: %w", context.DeadlineExceeded)
if !IsTimeoutError(err) {
t.Error("IsTimeoutError should return true for wrapped context.DeadlineExceeded")
}
}
func TestIsTimeoutError_NetError(t *testing.T) {
err := &mockTimeoutError{error: errors.New("network timeout")}
if !IsTimeoutError(err) {
t.Error("IsTimeoutError should return true for net.Error with Timeout() = true")
}
}
func TestIsTimeoutError_WrappedNetError(t *testing.T) {
netErr := &mockTimeoutError{error: errors.New("network timeout")}
err := fmt.Errorf("wrapped: %w", netErr)
if !IsTimeoutError(err) {
t.Error("IsTimeoutError should return true for wrapped net.Error timeout")
}
}
func TestIsTimeoutError_OSDeadlineExceeded(t *testing.T) {
err := os.ErrDeadlineExceeded
if !IsTimeoutError(err) {
t.Error("IsTimeoutError should return true for os.ErrDeadlineExceeded")
}
}
func TestIsTimeoutError_WrappedOSDeadlineExceeded(t *testing.T) {
err := fmt.Errorf("wrapped: %w", os.ErrDeadlineExceeded)
if !IsTimeoutError(err) {
t.Error("IsTimeoutError should return true for wrapped os.ErrDeadlineExceeded")
}
}
func TestIsTimeoutError_WrappedNonTimeoutError(t *testing.T) {
err := fmt.Errorf("wrapped: %w", errors.New("some other error"))
if IsTimeoutError(err) {
t.Error("IsTimeoutError should return false for wrapped non-timeout error")
}
}
func TestIsTimeoutError_ContextCanceled(t *testing.T) {
// context.Canceled is different from context.DeadlineExceeded
err := context.Canceled
if IsTimeoutError(err) {
t.Error("IsTimeoutError should return false for context.Canceled (not a timeout)")
}
}
func TestIsTimeoutError_RealDialTimeout(t *testing.T) {
// Create a real timeout error by attempting to connect to a non-routable IP
// with a very short timeout
dialer := net.Dialer{
Timeout: 1 * time.Nanosecond, // Extremely short timeout
}
_, err := dialer.Dial("tcp", "192.0.2.1:80") // 192.0.2.0/24 is reserved for documentation
if err == nil {
t.Skip("Expected dial to fail with timeout")
}
// This should be a real timeout error
if !IsTimeoutError(err) {
t.Errorf("IsTimeoutError should return true for real dial timeout, got error: %v", err)
}
}