Skip to content

Commit

Permalink
hugolib: Add some content timeout to image tests
Browse files Browse the repository at this point in the history
Also allow timeout to be set as a duration string, e.g. `30s`.
See gohugoio#6501
  • Loading branch information
bep committed Nov 25, 2019
1 parent 10fb2e5 commit e4b953e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 11 deletions.
2 changes: 1 addition & 1 deletion hugolib/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ func loadDefaultSettingsFor(v *viper.Viper) error {
v.SetDefault("disableAliases", false)
v.SetDefault("debug", false)
v.SetDefault("disableFastRender", false)
v.SetDefault("timeout", 30000) // 30 seconds
v.SetDefault("timeout", "30s")
v.SetDefault("enableInlineShortcodes", false)

return nil
Expand Down
40 changes: 31 additions & 9 deletions hugolib/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ func TestImageOps(t *testing.T) {
c.Assert(err, qt.IsNil)
defer clean()

newBuilder := func() *sitesBuilder {
newBuilder := func(timeout string) *sitesBuilder {

v := viper.New()
v.Set("workingDir", workDir)
v.Set("baseURL", "https://example.org")
v.Set("timeout", timeout)

b := newTestSitesBuilder(t).WithWorkingDir(workDir)
b.Fs = hugofs.NewDefault(v)
Expand All @@ -49,9 +50,17 @@ func TestImageOps(t *testing.T) {
title: "My bundle"
---
{{< imgproc >}}
`)

b.WithTemplatesAdded("index.html", `
b.WithTemplatesAdded(
"shortcodes/imgproc.html", `
{{ $img := resources.Get "images/sunset.jpg" }}
{{ $r := $img.Resize "129x239" }}
IMG SHORTCODE: {{ $r.RelPermalink }}/{{ $r.Width }}
`,
"index.html", `
{{ $p := .Site.GetPage "mybundle" }}
{{ $img1 := resources.Get "images/sunset.jpg" }}
{{ $img2 := $p.Resources.GetMatch "sunset.jpg" }}
Expand Down Expand Up @@ -83,7 +92,7 @@ BG3: {{ $blurryGrayscale3.RelPermalink }}/{{ $blurryGrayscale3.Width }}
{{ $blurryGrayscale4 := $r.Filter $filters }}
BG4: {{ $blurryGrayscale4.RelPermalink }}/{{ $blurryGrayscale4.Width }}
{{ $p.Content }}
`)

Expand Down Expand Up @@ -112,8 +121,8 @@ BG4: {{ $blurryGrayscale4.RelPermalink }}/{{ $blurryGrayscale4.Width }}
out.Close()
src.Close()

b := newBuilder()
b.Build(BuildCfg{})
// First build it with a very short timeout to trigger errors.
b := newBuilder("1ns")

imgExpect := `
Resized1: images/sunset.jpg|123|234|image/jpg|/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_123x234_resize_q75_box.jpg|
Expand All @@ -126,16 +135,29 @@ BG1: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_2ae8bb993431ec1aec4
BG2: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_2ae8bb993431ec1aec40fe59927b46b4.jpg/123
BG3: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_ed7740a90b82802261c2fbdb98bc8082.jpg/123
BG4: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_ed7740a90b82802261c2fbdb98bc8082.jpg/123
IMG SHORTCODE: /images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_129x239_resize_q75_box.jpg/129
`

b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
b.AssertImage(350, 219, "public/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_350x0_resize_q75_box.a86fe88d894e5db613f6aa8a80538fefc25b20fa24ba0d782c057adcef616f56.jpg")
assertImages := func() {
b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
b.AssertImage(350, 219, "public/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_350x0_resize_q75_box.a86fe88d894e5db613f6aa8a80538fefc25b20fa24ba0d782c057adcef616f56.jpg")
b.AssertImage(129, 239, "public/images/sunset_hu59e56ffff1bc1d8d122b1403d34e039f_90587_129x239_resize_q75_box.jpg")
}

err = b.BuildE(BuildCfg{})
c.Assert(err, qt.Not(qt.IsNil))
assertImages()

b = newBuilder("30s")
b.Build(BuildCfg{})

assertImages()

// Build it again to make sure we read images from file cache.
b = newBuilder()
b = newBuilder("30s")
b.Build(BuildCfg{})

b.AssertFileContent(filepath.Join(workDir, "public/index.html"), imgExpect)
assertImages()

}

Expand Down
15 changes: 14 additions & 1 deletion hugolib/site.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,10 +410,23 @@ func newSite(cfg deps.DepsCfg) (*Site, error) {
return nil, err
}

timeout := 30 * time.Second
if cfg.Language.IsSet("timeout") {
switch v := cfg.Language.Get("timeout").(type) {
case int:
timeout = time.Duration(v) * time.Millisecond
case string:
d, err := time.ParseDuration(v)
if err == nil {
timeout = d
}
}
}

siteConfig := siteConfigHolder{
sitemap: config.DecodeSitemap(config.Sitemap{Priority: -1, Filename: "sitemap.xml"}, cfg.Language.GetStringMap("sitemap")),
taxonomiesConfig: taxonomies,
timeout: time.Duration(cfg.Language.GetInt("timeout")) * time.Millisecond,
timeout: timeout,
hasCJKLanguage: cfg.Language.GetBool("hasCJKLanguage"),
enableEmoji: cfg.Language.Cfg.GetBool("enableEmoji"),
}
Expand Down

0 comments on commit e4b953e

Please sign in to comment.