| 1 |
// Copyright 2024 The Hugo Authors. All rights reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package hugio |
| 15 |
|
| 16 |
import ( |
| 17 |
"bytes" |
| 18 |
"fmt" |
| 19 |
"io" |
| 20 |
"math/rand" |
| 21 |
"strings" |
| 22 |
"testing" |
| 23 |
"time" |
| 24 |
|
| 25 |
qt "github.com/frankban/quicktest" |
| 26 |
) |
| 27 |
|
| 28 |
func TestHasBytesWriter(t *testing.T) { |
| 29 |
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 30 |
|
| 31 |
c := qt.New((t)) |
| 32 |
|
| 33 |
neww := func() (*HasBytesWriter, io.Writer) { |
| 34 |
var b bytes.Buffer |
| 35 |
|
| 36 |
h := &HasBytesWriter{ |
| 37 |
Patterns: []*HasBytesPattern{ |
| 38 |
{Pattern: []byte("__foo")}, |
| 39 |
}, |
| 40 |
} |
| 41 |
|
| 42 |
return h, io.MultiWriter(&b, h) |
| 43 |
} |
| 44 |
|
| 45 |
rndStr := func() string { |
| 46 |
return strings.Repeat("ab cfo", r.Intn(33)) |
| 47 |
} |
| 48 |
|
| 49 |
for range 22 { |
| 50 |
h, w := neww() |
| 51 |
fmt.Fprint(w, rndStr()+"abc __foobar"+rndStr()) |
| 52 |
c.Assert(h.Patterns[0].Match, qt.Equals, true) |
| 53 |
|
| 54 |
h, w = neww() |
| 55 |
fmt.Fprint(w, rndStr()+"abc __f") |
| 56 |
fmt.Fprint(w, "oo bar"+rndStr()) |
| 57 |
c.Assert(h.Patterns[0].Match, qt.Equals, true) |
| 58 |
|
| 59 |
h, w = neww() |
| 60 |
fmt.Fprint(w, rndStr()+"abc __moo bar") |
| 61 |
c.Assert(h.Patterns[0].Match, qt.Equals, false) |
| 62 |
} |
| 63 |
|
| 64 |
h, w := neww() |
| 65 |
fmt.Fprintf(w, "__foo") |
| 66 |
c.Assert(h.Patterns[0].Match, qt.Equals, true) |
| 67 |
} |
| 68 |
|
| 69 |
func TestHasBytesWriterMultiplePatterns(t *testing.T) { |
| 70 |
c := qt.New(t) |
| 71 |
|
| 72 |
neww := func() (*HasBytesWriter, io.Writer) { |
| 73 |
var b bytes.Buffer |
| 74 |
h := &HasBytesWriter{ |
| 75 |
Patterns: []*HasBytesPattern{ |
| 76 |
{Pattern: []byte("__hdeferred/")}, |
| 77 |
{Pattern: []byte("__h_pp_l1")}, |
| 78 |
}, |
| 79 |
} |
| 80 |
return h, io.MultiWriter(&b, h) |
| 81 |
} |
| 82 |
|
| 83 |
// Neither pattern present. |
| 84 |
h, w := neww() |
| 85 |
fmt.Fprint(w, "the quick brown fox jumps over the lazy dog") |
| 86 |
c.Assert(h.Patterns[0].Match, qt.Equals, false) |
| 87 |
c.Assert(h.Patterns[1].Match, qt.Equals, false) |
| 88 |
c.Assert(h.done, qt.Equals, false) |
| 89 |
|
| 90 |
// Only the second pattern present; the writer must not report a match |
| 91 |
// for the first, and must not prematurely mark itself done. |
| 92 |
h, w = neww() |
| 93 |
fmt.Fprint(w, "prefix __h_pp_l1 suffix") |
| 94 |
c.Assert(h.Patterns[0].Match, qt.Equals, false) |
| 95 |
c.Assert(h.Patterns[1].Match, qt.Equals, true) |
| 96 |
c.Assert(h.done, qt.Equals, false) |
| 97 |
|
| 98 |
// Both patterns present across multiple writes; done once all match. |
| 99 |
h, w = neww() |
| 100 |
fmt.Fprint(w, "aaa __hdef") |
| 101 |
fmt.Fprint(w, "erred/xyz bbb __h_p") |
| 102 |
fmt.Fprint(w, "p_l1 ccc") |
| 103 |
c.Assert(h.Patterns[0].Match, qt.Equals, true) |
| 104 |
c.Assert(h.Patterns[1].Match, qt.Equals, true) |
| 105 |
c.Assert(h.done, qt.Equals, true) |
| 106 |
} |
| 107 |
|
| 108 |
func BenchmarkHasBytesWriter(b *testing.B) { |
| 109 |
// A large chunk of output containing neither pattern is the common case |
| 110 |
// (a normal rendered page): the writer must scan all of it. |
| 111 |
content := []byte(strings.Repeat("<div class=\"nav\"><a href=\"/foo/bar\">baz</a></div>\n", 4000)) |
| 112 |
|
| 113 |
b.ResetTimer() |
| 114 |
for range b.N { |
| 115 |
h := &HasBytesWriter{ |
| 116 |
Patterns: []*HasBytesPattern{ |
| 117 |
{Pattern: []byte("__hdeferred/")}, |
| 118 |
{Pattern: []byte("__h_pp_l1")}, |
| 119 |
}, |
| 120 |
} |
| 121 |
if _, err := h.Write(content); err != nil { |
| 122 |
b.Fatal(err) |
| 123 |
} |
| 124 |
} |
| 125 |
} |