Skip to content

Commit 6f95e9b

Browse files
committed
testing context and timeouts and fallbacks
1 parent a6a523e commit 6f95e9b

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

render_with_data_fetch_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ func (v *ExpensiveView) Renderable(ctx context.Context) (Renderable, error) {
5757
}
5858
}
5959

60+
type ViewWithTimeout struct {
61+
Delegate AsRenderable
62+
Timeout time.Duration
63+
}
64+
65+
func (v ViewWithTimeout) Renderable(ctx context.Context) (Renderable, error) {
66+
ctx, _ = context.WithTimeout(ctx, v.Timeout)
67+
return v.Delegate.Renderable(ctx)
68+
}
69+
6070
func TestViewWithChannels(t *testing.T) {
6171
t.Run("successful", func(t *testing.T) {
6272
html, err := Render(context.Background(), NewExpensiveView(false, 1*time.Millisecond))
@@ -80,4 +90,16 @@ func TestViewWithChannels(t *testing.T) {
8090
_, err := Render(ctx, NewExpensiveView(false, 2*time.Millisecond))
8191
assert.NoError(t, err)
8292
})
93+
94+
t.Run("with timeout and fallible", func(t *testing.T) {
95+
html, err := Render(context.Background(), FallibleView{
96+
Child: ViewWithTimeout{
97+
Delegate: NewExpensiveView(false, 10*time.Millisecond),
98+
Timeout: 2 * time.Millisecond,
99+
},
100+
CapturesErr: context.DeadlineExceeded,
101+
})
102+
assert.NoError(t, err)
103+
assert.Equal(t, template.HTML(`HEADING`), html)
104+
})
83105
}

0 commit comments

Comments
 (0)