github.com/auxten/ginkgo@v0.0.0-20220130172820-7d98ad59d232/seed/seed_test.go (about) 1 package seed 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path" 7 "testing" 8 9 log "github.com/sirupsen/logrus" 10 . "github.com/smartystreets/goconvey/convey" 11 ) 12 13 func TestHost_Hash(t *testing.T) { 14 log.SetLevel(log.DebugLevel) 15 const ( 16 size = 256 17 vCount = 5 18 threshold = 20 19 ) 20 21 var testData = []struct { 22 host Host 23 }{ 24 {Host{[4]byte{0, 0, 0, 0}, 8080}}, 25 {Host{[4]byte{127, 0, 0, 1}, 8080}}, 26 {Host{[4]byte{192, 168, 0, 1}, 8080}}, 27 {Host{[4]byte{192, 168, 0, 2}, 8080}}, 28 {Host{[4]byte{192, 168, 0, 3}, 8080}}, 29 {Host{[4]byte{10, 10, 0, 3}, 8080}}, 30 {Host{[4]byte{10, 20, 0, 3}, 8080}}, 31 {Host{[4]byte{10, 20, 0, 4}, 8080}}, 32 {Host{[4]byte{10, 20, 0, 4}, 8081}}, 33 {Host{[4]byte{10, 20, 0, 4}, 8082}}, 34 {Host{[4]byte{10, 20, 1, 4}, 8082}}, 35 } 36 t.Run("fnv hash collision test", func(t *testing.T) { 37 var pool = make(map[uint64]byte) 38 var counter int 39 for _, d := range testData { 40 for j := byte(0); j < vCount; j++ { 41 h := uint64(d.host.HashFnv(j) % size) 42 //log.Debugf("%d", h) 43 if _, collision := pool[h]; collision { 44 log.Debugf("%s %d found %d", d.host, j, h) 45 counter++ 46 } else { 47 pool[h] = 1 48 } 49 } 50 } 51 if counter > threshold { 52 t.Errorf("collision count %d", counter) 53 } 54 log.Debugf("fnv collision count %d", counter) 55 }) 56 t.Run("crc32 hash collision test", func(t *testing.T) { 57 var pool = make(map[uint64]byte) 58 var counter int 59 for _, d := range testData { 60 for j := byte(0); j < vCount; j++ { 61 h := uint64(d.host.HashCrc(j) % size) 62 //log.Debugf("%d", h) 63 if _, collision := pool[h]; collision { 64 log.Debugf("%s %d found %d", d.host, j, h) 65 counter++ 66 } else { 67 pool[h] = 1 68 } 69 } 70 } 71 if counter > threshold { 72 t.Errorf("collision count %d", counter) 73 } 74 log.Debugf("crc32 collision count %d", counter) 75 }) 76 t.Run("sha256 hash collision test", func(t *testing.T) { 77 var pool = make(map[uint64]byte) 78 var counter int 79 for _, d := range testData { 80 for j := byte(0); j < vCount; j++ { 81 h := uint64(d.host.HashSha(j) % size) 82 //log.Debugf("%d", h) 83 if _, collision := pool[h]; collision { 84 log.Debugf("%s %d found %d", d.host, j, h) 85 counter++ 86 } else { 87 pool[h] = 1 88 } 89 } 90 } 91 if counter > threshold { 92 t.Errorf("collision count %d", counter) 93 } 94 log.Debugf("sha256 collision count %d", counter) 95 }) 96 } 97 98 func TestHost_String(t *testing.T) { 99 t.Run("hash string", func(t *testing.T) { 100 h := Host{ 101 IP: [4]byte{10, 20, 30, 110}, 102 Port: 8081, 103 } 104 if h.String() != "10.20.30.110:8081" { 105 t.Fatal() 106 } 107 }) 108 } 109 110 func TestSeed_TouchAll(t *testing.T) { 111 Convey("ensure touch all", t, func() { 112 dir, err := ioutil.TempDir("", "touchAll") 113 So(err, ShouldBeNil) 114 defer os.RemoveAll(dir) // clean up 115 err = os.Chdir(dir) 116 So(err, ShouldBeNil) 117 sd := Seed{ 118 Files: []*File{ 119 {LocalPath: "./dir", Size: -1}, 120 {LocalPath: "./dir/dir1", Size: -1}, 121 {LocalPath: "./dir/dir1/emptyFile", Size: 0}, 122 {LocalPath: "./dir/dir1/file1", Size: 4}, 123 {LocalPath: "./dir/dir1/link", Size: -1, SymPath: "file1"}, 124 }, 125 } 126 err = sd.TouchAll() 127 So(err, ShouldBeNil) 128 fd, err := os.OpenFile("./dir/dir1/file1", os.O_WRONLY, 0644) 129 So(err, ShouldBeNil) 130 n, err := fd.Write([]byte("1234")) 131 So(err, ShouldBeNil) 132 So(n, ShouldEqual, 4) 133 err = fd.Close() 134 So(err, ShouldBeNil) 135 136 sd2, err := MakeSeed("./dir", 10) 137 So(err, ShouldBeNil) 138 for i := range sd2.Files { 139 So(sd2.Files[i].Path, ShouldResemble, path.Clean(sd.Files[i].LocalPath)) 140 So(sd2.Files[i].Size, ShouldResemble, sd.Files[i].Size) 141 } 142 }) 143 }