github.com/akashshinde/docker@v1.9.1/pkg/ioutils/bytespipe_test.go (about) 1 package ioutils 2 3 import ( 4 "crypto/sha1" 5 "encoding/hex" 6 "testing" 7 ) 8 9 func TestBytesPipeRead(t *testing.T) { 10 buf := NewBytesPipe(nil) 11 buf.Write([]byte("12")) 12 buf.Write([]byte("34")) 13 buf.Write([]byte("56")) 14 buf.Write([]byte("78")) 15 buf.Write([]byte("90")) 16 rd := make([]byte, 4) 17 n, err := buf.Read(rd) 18 if err != nil { 19 t.Fatal(err) 20 } 21 if n != 4 { 22 t.Fatalf("Wrong number of bytes read: %d, should be %d", n, 4) 23 } 24 if string(rd) != "1234" { 25 t.Fatalf("Read %s, but must be %s", rd, "1234") 26 } 27 n, err = buf.Read(rd) 28 if err != nil { 29 t.Fatal(err) 30 } 31 if n != 4 { 32 t.Fatalf("Wrong number of bytes read: %d, should be %d", n, 4) 33 } 34 if string(rd) != "5678" { 35 t.Fatalf("Read %s, but must be %s", rd, "5679") 36 } 37 n, err = buf.Read(rd) 38 if err != nil { 39 t.Fatal(err) 40 } 41 if n != 2 { 42 t.Fatalf("Wrong number of bytes read: %d, should be %d", n, 2) 43 } 44 if string(rd[:n]) != "90" { 45 t.Fatalf("Read %s, but must be %s", rd, "90") 46 } 47 } 48 49 func TestBytesPipeWrite(t *testing.T) { 50 buf := NewBytesPipe(nil) 51 buf.Write([]byte("12")) 52 buf.Write([]byte("34")) 53 buf.Write([]byte("56")) 54 buf.Write([]byte("78")) 55 buf.Write([]byte("90")) 56 if string(buf.buf[0]) != "1234567890" { 57 t.Fatalf("Buffer %s, must be %s", buf.buf, "1234567890") 58 } 59 } 60 61 // Write and read in different speeds/chunk sizes and check valid data is read. 62 func TestBytesPipeWriteRandomChunks(t *testing.T) { 63 cases := []struct{ iterations, writesPerLoop, readsPerLoop int }{ 64 {100, 10, 1}, 65 {1000, 10, 5}, 66 {1000, 100, 0}, 67 {1000, 5, 6}, 68 {10000, 50, 25}, 69 } 70 71 testMessage := []byte("this is a random string for testing") 72 // random slice sizes to read and write 73 writeChunks := []int{25, 35, 15, 20} 74 readChunks := []int{5, 45, 20, 25} 75 76 for _, c := range cases { 77 // first pass: write directly to hash 78 hash := sha1.New() 79 for i := 0; i < c.iterations*c.writesPerLoop; i++ { 80 if _, err := hash.Write(testMessage[:writeChunks[i%len(writeChunks)]]); err != nil { 81 t.Fatal(err) 82 } 83 } 84 expected := hex.EncodeToString(hash.Sum(nil)) 85 86 // write/read through buffer 87 buf := NewBytesPipe(nil) 88 hash.Reset() 89 for i := 0; i < c.iterations; i++ { 90 for w := 0; w < c.writesPerLoop; w++ { 91 buf.Write(testMessage[:writeChunks[(i*c.writesPerLoop+w)%len(writeChunks)]]) 92 } 93 for r := 0; r < c.readsPerLoop; r++ { 94 p := make([]byte, readChunks[(i*c.readsPerLoop+r)%len(readChunks)]) 95 n, _ := buf.Read(p) 96 hash.Write(p[:n]) 97 } 98 } 99 // read rest of the data from buffer 100 for i := 0; ; i++ { 101 p := make([]byte, readChunks[(c.iterations*c.readsPerLoop+i)%len(readChunks)]) 102 n, _ := buf.Read(p) 103 if n == 0 { 104 break 105 } 106 hash.Write(p[:n]) 107 } 108 actual := hex.EncodeToString(hash.Sum(nil)) 109 110 if expected != actual { 111 t.Fatalf("BytesPipe returned invalid data. Expected checksum %v, got %v", expected, actual) 112 } 113 114 } 115 } 116 117 func BenchmarkBytesPipeWrite(b *testing.B) { 118 for i := 0; i < b.N; i++ { 119 buf := NewBytesPipe(nil) 120 for j := 0; j < 1000; j++ { 121 buf.Write([]byte("pretty short line, because why not?")) 122 } 123 } 124 } 125 126 func BenchmarkBytesPipeRead(b *testing.B) { 127 rd := make([]byte, 1024) 128 for i := 0; i < b.N; i++ { 129 b.StopTimer() 130 buf := NewBytesPipe(nil) 131 for j := 0; j < 1000; j++ { 132 buf.Write(make([]byte, 1024)) 133 } 134 b.StartTimer() 135 for j := 0; j < 1000; j++ { 136 if n, _ := buf.Read(rd); n != 1024 { 137 b.Fatalf("Wrong number of bytes: %d", n) 138 } 139 } 140 } 141 }