Vibe-guided bskyoauth and custom repo example code in Golang 馃 probably not safe to use in prod
1package bskyoauth
2
3import (
4 "context"
5 "errors"
6 "fmt"
7 "net"
8 "os"
9 "testing"
10 "time"
11)
12
13// mockTimeoutError is a mock implementation of net.Error with Timeout() returning true
14type mockTimeoutError struct {
15 error
16}
17
18func (e *mockTimeoutError) Timeout() bool { return true }
19func (e *mockTimeoutError) Temporary() bool { return false }
20
21func TestIsTimeoutError_WrappedContextDeadlineExceeded(t *testing.T) {
22 err := fmt.Errorf("wrapped error: %w", context.DeadlineExceeded)
23 if !IsTimeoutError(err) {
24 t.Error("IsTimeoutError should return true for wrapped context.DeadlineExceeded")
25 }
26}
27
28func TestIsTimeoutError_NetError(t *testing.T) {
29 err := &mockTimeoutError{error: errors.New("network timeout")}
30 if !IsTimeoutError(err) {
31 t.Error("IsTimeoutError should return true for net.Error with Timeout() = true")
32 }
33}
34
35func TestIsTimeoutError_WrappedNetError(t *testing.T) {
36 netErr := &mockTimeoutError{error: errors.New("network timeout")}
37 err := fmt.Errorf("wrapped: %w", netErr)
38 if !IsTimeoutError(err) {
39 t.Error("IsTimeoutError should return true for wrapped net.Error timeout")
40 }
41}
42
43func TestIsTimeoutError_OSDeadlineExceeded(t *testing.T) {
44 err := os.ErrDeadlineExceeded
45 if !IsTimeoutError(err) {
46 t.Error("IsTimeoutError should return true for os.ErrDeadlineExceeded")
47 }
48}
49
50func TestIsTimeoutError_WrappedOSDeadlineExceeded(t *testing.T) {
51 err := fmt.Errorf("wrapped: %w", os.ErrDeadlineExceeded)
52 if !IsTimeoutError(err) {
53 t.Error("IsTimeoutError should return true for wrapped os.ErrDeadlineExceeded")
54 }
55}
56
57func TestIsTimeoutError_WrappedNonTimeoutError(t *testing.T) {
58 err := fmt.Errorf("wrapped: %w", errors.New("some other error"))
59 if IsTimeoutError(err) {
60 t.Error("IsTimeoutError should return false for wrapped non-timeout error")
61 }
62}
63
64func TestIsTimeoutError_ContextCanceled(t *testing.T) {
65 // context.Canceled is different from context.DeadlineExceeded
66 err := context.Canceled
67 if IsTimeoutError(err) {
68 t.Error("IsTimeoutError should return false for context.Canceled (not a timeout)")
69 }
70}
71
72func TestIsTimeoutError_RealDialTimeout(t *testing.T) {
73 // Create a real timeout error by attempting to connect to a non-routable IP
74 // with a very short timeout
75 dialer := net.Dialer{
76 Timeout: 1 * time.Nanosecond, // Extremely short timeout
77 }
78 _, err := dialer.Dial("tcp", "192.0.2.1:80") // 192.0.2.0/24 is reserved for documentation
79
80 if err == nil {
81 t.Skip("Expected dial to fail with timeout")
82 }
83
84 // This should be a real timeout error
85 if !IsTimeoutError(err) {
86 t.Errorf("IsTimeoutError should return true for real dial timeout, got error: %v", err)
87 }
88}