Debian Codegraph @

hugofs/hashing_fs_test.go

raw
1 // Copyright 2018 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 hugofs
15
16 import (
17 "fmt"
18 "strings"
19 "testing"
20
21 qt "github.com/frankban/quicktest"
22 "github.com/spf13/afero"
23 )
24
25 type testHashReceiver struct {
26 name string
27 sum uint64
28 }
29
30 func (t *testHashReceiver) OnFileClose(name string, checksum uint64) {
31 t.name = name
32 t.sum = checksum
33 }
34
35 func TestHashingFs(t *testing.T) {
36 c := qt.New(t)
37
38 fs := afero.NewMemMapFs()
39 observer := &testHashReceiver{}
40 ofs := NewHashingFs(fs, observer)
41
42 f, err := ofs.Create("hashme")
43 c.Assert(err, qt.IsNil)
44 _, err = f.Write([]byte("content"))
45 c.Assert(err, qt.IsNil)
46 c.Assert(f.Close(), qt.IsNil)
47 c.Assert(observer.sum, qt.Equals, uint64(7807861979271768572))
48 c.Assert(observer.name, qt.Equals, "hashme")
49
50 f, err = ofs.Create("nowrites")
51 c.Assert(err, qt.IsNil)
52 c.Assert(f.Close(), qt.IsNil)
53 c.Assert(observer.sum, qt.Equals, uint64(17241709254077376921))
54 }
55
56 func BenchmarkHashingFs(b *testing.B) {
57 fs := afero.NewMemMapFs()
58 observer := &testHashReceiver{}
59 ofs := NewHashingFs(fs, observer)
60 content := []byte(strings.Repeat("lorem ipsum ", 1000))
61
62 for i := 0; b.Loop(); i++ {
63 f, err := ofs.Create(fmt.Sprintf("file%d", i))
64 if err != nil {
65 b.Fatal(err)
66 }
67 _, err = f.Write(content)
68 if err != nil {
69 b.Fatal(err)
70 }
71 if err := f.Close(); err != nil {
72 b.Fatal(err)
73 }
74 }
75 }