pagewriter_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2016 The etcd Authors
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ioutil
  15. import (
  16. "math/rand"
  17. "testing"
  18. )
  19. func TestPageWriterRandom(t *testing.T) {
  20. // smaller buffer for stress testing
  21. defaultBufferBytes = 8 * 1024
  22. pageBytes := 128
  23. buf := make([]byte, 4*defaultBufferBytes)
  24. cw := &checkPageWriter{pageBytes: pageBytes, t: t}
  25. w := NewPageWriter(cw, pageBytes, 0)
  26. n := 0
  27. for i := 0; i < 4096; i++ {
  28. c, err := w.Write(buf[:rand.Intn(len(buf))])
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. n += c
  33. }
  34. if cw.writeBytes > n {
  35. t.Fatalf("wrote %d bytes to io.Writer, but only wrote %d bytes", cw.writeBytes, n)
  36. }
  37. if n-cw.writeBytes > pageBytes {
  38. t.Fatalf("got %d bytes pending, expected less than %d bytes", n-cw.writeBytes, pageBytes)
  39. }
  40. t.Logf("total writes: %d", cw.writes)
  41. t.Logf("total write bytes: %d (of %d)", cw.writeBytes, n)
  42. }
  43. // TestPageWriterPariallack tests the case where a write overflows the buffer
  44. // but there is not enough data to complete the slack write.
  45. func TestPageWriterPartialSlack(t *testing.T) {
  46. defaultBufferBytes = 1024
  47. pageBytes := 128
  48. buf := make([]byte, defaultBufferBytes)
  49. cw := &checkPageWriter{pageBytes: 64, t: t}
  50. w := NewPageWriter(cw, pageBytes, 0)
  51. // put writer in non-zero page offset
  52. if _, err := w.Write(buf[:64]); err != nil {
  53. t.Fatal(err)
  54. }
  55. if err := w.Flush(); err != nil {
  56. t.Fatal(err)
  57. }
  58. if cw.writes != 1 {
  59. t.Fatalf("got %d writes, expected 1", cw.writes)
  60. }
  61. // nearly fill buffer
  62. if _, err := w.Write(buf[:1022]); err != nil {
  63. t.Fatal(err)
  64. }
  65. // overflow buffer, but without enough to write as aligned
  66. if _, err := w.Write(buf[:8]); err != nil {
  67. t.Fatal(err)
  68. }
  69. if cw.writes != 1 {
  70. t.Fatalf("got %d writes, expected 1", cw.writes)
  71. }
  72. // finish writing slack space
  73. if _, err := w.Write(buf[:128]); err != nil {
  74. t.Fatal(err)
  75. }
  76. if cw.writes != 2 {
  77. t.Fatalf("got %d writes, expected 2", cw.writes)
  78. }
  79. }
  80. // TestPageWriterOffset tests if page writer correctly repositions when offset is given.
  81. func TestPageWriterOffset(t *testing.T) {
  82. defaultBufferBytes = 1024
  83. pageBytes := 128
  84. buf := make([]byte, defaultBufferBytes)
  85. cw := &checkPageWriter{pageBytes: 64, t: t}
  86. w := NewPageWriter(cw, pageBytes, 0)
  87. if _, err := w.Write(buf[:64]); err != nil {
  88. t.Fatal(err)
  89. }
  90. if err := w.Flush(); err != nil {
  91. t.Fatal(err)
  92. }
  93. if w.pageOffset != 64 {
  94. t.Fatalf("w.pageOffset expected 64, got %d", w.pageOffset)
  95. }
  96. w = NewPageWriter(cw, w.pageOffset, pageBytes)
  97. if _, err := w.Write(buf[:64]); err != nil {
  98. t.Fatal(err)
  99. }
  100. if err := w.Flush(); err != nil {
  101. t.Fatal(err)
  102. }
  103. if w.pageOffset != 0 {
  104. t.Fatalf("w.pageOffset expected 0, got %d", w.pageOffset)
  105. }
  106. }
  107. // checkPageWriter implements an io.Writer that fails a test on unaligned writes.
  108. type checkPageWriter struct {
  109. pageBytes int
  110. writes int
  111. writeBytes int
  112. t *testing.T
  113. }
  114. func (cw *checkPageWriter) Write(p []byte) (int, error) {
  115. if len(p)%cw.pageBytes != 0 {
  116. cw.t.Fatalf("got write len(p) = %d, expected len(p) == k*cw.pageBytes", len(p))
  117. }
  118. cw.writes++
  119. cw.writeBytes += len(p)
  120. return len(p), nil
  121. }