bytebuffer_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. package bytebufferpool
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io"
  6. "testing"
  7. "time"
  8. )
  9. func TestByteBufferReadFrom(t *testing.T) {
  10. prefix := "foobar"
  11. expectedS := "asadfsdafsadfasdfisdsdfa"
  12. prefixLen := int64(len(prefix))
  13. expectedN := int64(len(expectedS))
  14. var bb ByteBuffer
  15. bb.WriteString(prefix)
  16. rf := (io.ReaderFrom)(&bb)
  17. for i := 0; i < 20; i++ {
  18. r := bytes.NewBufferString(expectedS)
  19. n, err := rf.ReadFrom(r)
  20. if n != expectedN {
  21. t.Fatalf("unexpected n=%d. Expecting %d. iteration %d", n, expectedN, i)
  22. }
  23. if err != nil {
  24. t.Fatalf("unexpected error: %s", err)
  25. }
  26. bbLen := int64(bb.Len())
  27. expectedLen := prefixLen + int64(i+1)*expectedN
  28. if bbLen != expectedLen {
  29. t.Fatalf("unexpected byteBuffer length: %d. Expecting %d", bbLen, expectedLen)
  30. }
  31. for j := 0; j < i; j++ {
  32. start := prefixLen + int64(j)*expectedN
  33. b := bb.B[start : start+expectedN]
  34. if string(b) != expectedS {
  35. t.Fatalf("unexpected byteBuffer contents: %q. Expecting %q", b, expectedS)
  36. }
  37. }
  38. }
  39. }
  40. func TestByteBufferWriteTo(t *testing.T) {
  41. expectedS := "foobarbaz"
  42. var bb ByteBuffer
  43. bb.WriteString(expectedS[:3])
  44. bb.WriteString(expectedS[3:])
  45. wt := (io.WriterTo)(&bb)
  46. var w bytes.Buffer
  47. for i := 0; i < 10; i++ {
  48. n, err := wt.WriteTo(&w)
  49. if n != int64(len(expectedS)) {
  50. t.Fatalf("unexpected n returned from WriteTo: %d. Expecting %d", n, len(expectedS))
  51. }
  52. if err != nil {
  53. t.Fatalf("unexpected error: %s", err)
  54. }
  55. s := string(w.Bytes())
  56. if s != expectedS {
  57. t.Fatalf("unexpected string written %q. Expecting %q", s, expectedS)
  58. }
  59. w.Reset()
  60. }
  61. }
  62. func TestByteBufferGetPutSerial(t *testing.T) {
  63. testByteBufferGetPut(t)
  64. }
  65. func TestByteBufferGetPutConcurrent(t *testing.T) {
  66. concurrency := 10
  67. ch := make(chan struct{}, concurrency)
  68. for i := 0; i < concurrency; i++ {
  69. go func() {
  70. testByteBufferGetPut(t)
  71. ch <- struct{}{}
  72. }()
  73. }
  74. for i := 0; i < concurrency; i++ {
  75. select {
  76. case <-ch:
  77. case <-time.After(time.Second):
  78. t.Fatalf("timeout!")
  79. }
  80. }
  81. }
  82. func testByteBufferGetPut(t *testing.T) {
  83. for i := 0; i < 10; i++ {
  84. expectedS := fmt.Sprintf("num %d", i)
  85. b := Get()
  86. b.B = append(b.B, "num "...)
  87. b.B = append(b.B, fmt.Sprintf("%d", i)...)
  88. if string(b.B) != expectedS {
  89. t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
  90. }
  91. Put(b)
  92. }
  93. }
  94. func testByteBufferGetString(t *testing.T) {
  95. for i := 0; i < 10; i++ {
  96. expectedS := fmt.Sprintf("num %d", i)
  97. b := Get()
  98. b.SetString(expectedS)
  99. if b.String() != expectedS {
  100. t.Fatalf("unexpected result: %q. Expecting %q", b.B, expectedS)
  101. }
  102. Put(b)
  103. }
  104. }
  105. func TestByteBufferGetStringSerial(t *testing.T) {
  106. testByteBufferGetString(t)
  107. }
  108. func TestByteBufferGetStringConcurrent(t *testing.T) {
  109. concurrency := 10
  110. ch := make(chan struct{}, concurrency)
  111. for i := 0; i < concurrency; i++ {
  112. go func() {
  113. testByteBufferGetString(t)
  114. ch <- struct{}{}
  115. }()
  116. }
  117. for i := 0; i < concurrency; i++ {
  118. select {
  119. case <-ch:
  120. case <-time.After(time.Second):
  121. t.Fatalf("timeout!")
  122. }
  123. }
  124. }