diff options
author | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-18 00:36:48 +0100 |
---|---|---|
committer | bep <bjorn.erik.pedersen@gmail.com> | 2015-03-18 17:05:54 +0100 |
commit | 98ee69bce207a4c2a7c2ffca2b3e7c0ccdd6e8c9 (patch) | |
tree | ebfb45ab42cad8a14dc44f8bacb92b47cfd12b94 /transform/chain_test.go | |
parent | 9688ed2585fcea732095baeb4006c5c96875447c (diff) |
Write to rotating ContentReWriter in transformer chain
This commit adds the interface ContentReWriter in the tranformer chain.
This is backed by two pooled byte buffers, alternating between being the reader or the writer.
This keeps the performance characteristic of the old implementation, but in a thread safe way.
Fixes #911
Benchmark old vs new:
benchmark old ns/op new ns/op delta
BenchmarkAbsURL 17614 17384 -1.31%
BenchmarkXMLAbsURL 9431 9248 -1.94%
benchmark old allocs new allocs delta
BenchmarkAbsURL 24 28 +16.67%
BenchmarkXMLAbsURL 12 14 +16.67%
benchmark old bytes new bytes delta
BenchmarkAbsURL 3295 3424 +3.92%
BenchmarkXMLAbsURL 1954 1987 +1.69%
Diffstat (limited to 'transform/chain_test.go')
-rw-r--r-- | transform/chain_test.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/transform/chain_test.go b/transform/chain_test.go index 8fa45f5a9..2477c3abf 100644 --- a/transform/chain_test.go +++ b/transform/chain_test.go @@ -2,6 +2,7 @@ package transform import ( "bytes" + "github.com/spf13/hugo/helpers" "strings" "testing" ) @@ -54,6 +55,35 @@ func TestChainZeroTransformers(t *testing.T) { } } +func TestChaingMultipleTransformers(t *testing.T) { + f1 := func(rw ContentReWriter) { + rw.Write(bytes.Replace(rw.Content(), []byte("f1"), []byte("f1r"), -1)) + } + f2 := func(rw ContentReWriter) { + rw.Write(bytes.Replace(rw.Content(), []byte("f2"), []byte("f2r"), -1)) + } + f3 := func(rw ContentReWriter) { + rw.Write(bytes.Replace(rw.Content(), []byte("f3"), []byte("f3r"), -1)) + } + + f4 := func(rw ContentReWriter) { + rw.Write(bytes.Replace(rw.Content(), []byte("f4"), []byte("f4r"), -1)) + } + + tr := NewChain(f1, f2, f3, f4) + + out := new(bytes.Buffer) + if err := tr.Apply(out, helpers.StringToReader("Test: f4 f3 f1 f2 f1 The End.")); err != nil { + t.Errorf("Multi transformer chain returned an error: %s", err) + } + + expected := "Test: f4r f3r f1r f2r f1r The End." + + if string(out.Bytes()) != expected { + t.Errorf("Expected %s got %s", expected, string(out.Bytes())) + } +} + func BenchmarkAbsURL(b *testing.B) { absURL, _ := AbsURL("http://base") tr := NewChain(absURL...) |