Skip to content

Commit

Permalink
testing context and timeouts and fallbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
stanistan committed Nov 30, 2023
1 parent a6a523e commit 6f95e9b
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions render_with_data_fetch_test.go
Expand Up @@ -57,6 +57,16 @@ func (v *ExpensiveView) Renderable(ctx context.Context) (Renderable, error) {
}
}

type ViewWithTimeout struct {
Delegate AsRenderable
Timeout time.Duration
}

func (v ViewWithTimeout) Renderable(ctx context.Context) (Renderable, error) {
ctx, _ = context.WithTimeout(ctx, v.Timeout)
return v.Delegate.Renderable(ctx)
}

func TestViewWithChannels(t *testing.T) {
t.Run("successful", func(t *testing.T) {
html, err := Render(context.Background(), NewExpensiveView(false, 1*time.Millisecond))
Expand All @@ -80,4 +90,16 @@ func TestViewWithChannels(t *testing.T) {
_, err := Render(ctx, NewExpensiveView(false, 2*time.Millisecond))
assert.NoError(t, err)
})

t.Run("with timeout and fallible", func(t *testing.T) {
html, err := Render(context.Background(), FallibleView{
Child: ViewWithTimeout{
Delegate: NewExpensiveView(false, 10*time.Millisecond),
Timeout: 2 * time.Millisecond,
},
CapturesErr: context.DeadlineExceeded,
})
assert.NoError(t, err)
assert.Equal(t, template.HTML(`HEADING`), html)
})
}

0 comments on commit 6f95e9b

Please sign in to comment.