Debian Codegraph @

pkg/router/timeout_test.go

raw
1 package router
2
3 import (
4 "context"
5 "errors"
6 "testing"
7 "testing/synctest"
8 "time"
9
10 "github.com/stretchr/testify/mock"
11
12 "github.com/nicholas-fedor/shoutrrr/pkg/types"
13 "github.com/nicholas-fedor/shoutrrr/pkg/types/mocks"
14 )
15
16 // budgetMock is a service assembled from generated mocks.
17 type budgetMock struct {
18 *mocks.MockService
19 *mocks.MockContextSender
20 *mocks.MockServiceTimeout
21 }
22
23 func newBudgetMock(t *testing.T, id string, block func(context.Context) error) *budgetMock {
24 t.Helper()
25
26 service := mocks.NewMockService(t)
27 sender := mocks.NewMockContextSender(t)
28 timeout := mocks.NewMockServiceTimeout(t)
29
30 service.EXPECT().Initialize(mock.Anything, mock.Anything).Return(nil)
31 service.EXPECT().GetID().Return(id)
32 sender.EXPECT().SendContext(mock.Anything, mock.Anything, mock.Anything).RunAndReturn(
33 func(ctx context.Context, _ string, _ *types.Params) error {
34 return block(ctx)
35 },
36 )
37
38 return &budgetMock{
39 MockService: service,
40 MockContextSender: sender,
41 MockServiceTimeout: timeout,
42 }
43 }
44
45 func (m *budgetMock) expectBudget(budget time.Duration, optional bool) {
46 call := m.MockServiceTimeout.EXPECT().ServiceTimeout(mock.Anything).Return(budget)
47 if optional {
48 call.Maybe()
49 }
50 }
51
52 func registerService(t *testing.T, scheme string, service types.Service) {
53 t.Helper()
54
55 serviceMap[scheme] = func() types.Service { return service }
56
57 t.Cleanup(func() { delete(serviceMap, scheme) })
58 }
59
60 func requireTimeout(t *testing.T, err error) {
61 t.Helper()
62
63 if !errors.Is(err, ErrServiceTimeout) {
64 t.Fatalf("error = %v, want ErrServiceTimeout", err)
65 }
66 }
67
68 //nolint:paralleltest // Modifies shared serviceMap and cannot run in parallel.
69 func TestSendBudget(t *testing.T) {
70 tests := []struct {
71 name string
72 scheme string
73 timeout time.Duration
74 assign time.Duration
75 reported time.Duration
76 want time.Duration
77 }{
78 {
79 name: "service budget extends the default",
80 scheme: "mock-budget",
81 reported: 30 * time.Second,
82 want: 30 * time.Second,
83 },
84 {
85 name: "sender timeout caps the service",
86 scheme: "mock-cap",
87 timeout: 2 * time.Second,
88 reported: 30 * time.Second,
89 want: 2 * time.Second,
90 },
91 {
92 name: "assigned timeout caps the service",
93 scheme: "mock-assign",
94 assign: 2 * time.Second,
95 reported: 30 * time.Second,
96 want: 2 * time.Second,
97 },
98 {
99 name: "shorter service budget keeps the default",
100 scheme: "mock-short",
101 reported: time.Second,
102 want: DefaultTimeout,
103 },
104 }
105
106 for _, tt := range tests {
107 t.Run(tt.name, func(t *testing.T) {
108 service := newBudgetMock(t, tt.scheme, func(ctx context.Context) error {
109 <-ctx.Done()
110
111 return ctx.Err()
112 })
113 service.expectBudget(tt.reported, tt.timeout > 0 || tt.assign > 0)
114 registerService(t, tt.scheme, service)
115
116 synctest.Test(t, func(t *testing.T) {
117 serviceRouter, err := NewWithOptions(nil, types.SenderOptions{Timeout: tt.timeout}, tt.scheme+"://")
118 if err != nil {
119 t.Fatalf("NewWithOptions: %v", err)
120 }
121
122 if tt.assign > 0 {
123 serviceRouter.Timeout = tt.assign
124 }
125
126 start := time.Now()
127 errs := serviceRouter.Send("hello", nil)
128
129 if time.Since(start) != tt.want {
130 t.Fatalf("elapsed = %s, want %s", time.Since(start), tt.want)
131 }
132
133 if len(errs) != 1 {
134 t.Fatalf("Send returned %d errors, want 1", len(errs))
135 }
136
137 requireTimeout(t, errs[0])
138 })
139 })
140 }
141 }
142
143 //nolint:paralleltest // Modifies shared serviceMap and cannot run in parallel.
144 func TestSendWithoutServiceTimeoutUsesDefault(t *testing.T) {
145 const scheme = "mock-hang"
146
147 service := mocks.NewMockService(t)
148 service.EXPECT().Initialize(mock.Anything, mock.Anything).Return(nil)
149 service.EXPECT().GetID().Return(scheme)
150 registerService(t, scheme, service)
151
152 synctest.Test(t, func(t *testing.T) {
153 // The release channel has to be created in the bubble. A channel from
154 // outside is not a durable block, so the fake clock never advances.
155 release := make(chan struct{})
156
157 service.EXPECT().Send(mock.Anything, mock.Anything).RunAndReturn(func(string, *types.Params) error {
158 <-release
159
160 return nil
161 })
162
163 serviceRouter, err := NewWithOptions(nil, types.SenderOptions{}, scheme+"://")
164 if err != nil {
165 t.Fatalf("NewWithOptions: %v", err)
166 }
167
168 start := time.Now()
169 errs := serviceRouter.Send("hello", nil)
170
171 close(release)
172 synctest.Wait()
173
174 if time.Since(start) != DefaultTimeout {
175 t.Fatalf("elapsed = %s, want %s", time.Since(start), DefaultTimeout)
176 }
177
178 if len(errs) != 1 {
179 t.Fatalf("Send returned %d errors, want 1", len(errs))
180 }
181
182 requireTimeout(t, errs[0])
183 })
184 }