compress_test.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package fasthttp
  2. import (
  3. "bytes"
  4. "fmt"
  5. "io/ioutil"
  6. "testing"
  7. "time"
  8. )
  9. var compressTestcases = func() []string {
  10. a := []string{
  11. "",
  12. "foobar",
  13. "выфаодлодл одлфываыв sd2 k34",
  14. }
  15. bigS := createFixedBody(1e4)
  16. a = append(a, string(bigS))
  17. return a
  18. }()
  19. func TestGzipBytesSerial(t *testing.T) {
  20. t.Parallel()
  21. if err := testGzipBytes(); err != nil {
  22. t.Fatal(err)
  23. }
  24. }
  25. func TestGzipBytesConcurrent(t *testing.T) {
  26. t.Parallel()
  27. if err := testConcurrent(10, testGzipBytes); err != nil {
  28. t.Fatal(err)
  29. }
  30. }
  31. func TestDeflateBytesSerial(t *testing.T) {
  32. t.Parallel()
  33. if err := testDeflateBytes(); err != nil {
  34. t.Fatal(err)
  35. }
  36. }
  37. func TestDeflateBytesConcurrent(t *testing.T) {
  38. t.Parallel()
  39. if err := testConcurrent(10, testDeflateBytes); err != nil {
  40. t.Fatal(err)
  41. }
  42. }
  43. func testGzipBytes() error {
  44. for _, s := range compressTestcases {
  45. if err := testGzipBytesSingleCase(s); err != nil {
  46. return err
  47. }
  48. }
  49. return nil
  50. }
  51. func testDeflateBytes() error {
  52. for _, s := range compressTestcases {
  53. if err := testDeflateBytesSingleCase(s); err != nil {
  54. return err
  55. }
  56. }
  57. return nil
  58. }
  59. func testGzipBytesSingleCase(s string) error {
  60. prefix := []byte("foobar")
  61. gzippedS := AppendGzipBytes(prefix, []byte(s))
  62. if !bytes.Equal(gzippedS[:len(prefix)], prefix) {
  63. return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, gzippedS[:len(prefix)], prefix)
  64. }
  65. gunzippedS, err := AppendGunzipBytes(prefix, gzippedS[len(prefix):])
  66. if err != nil {
  67. return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
  68. }
  69. if !bytes.Equal(gunzippedS[:len(prefix)], prefix) {
  70. return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, gunzippedS[:len(prefix)], prefix)
  71. }
  72. gunzippedS = gunzippedS[len(prefix):]
  73. if string(gunzippedS) != s {
  74. return fmt.Errorf("unexpected uncompressed string %q. Expecting %q", gunzippedS, s)
  75. }
  76. return nil
  77. }
  78. func testDeflateBytesSingleCase(s string) error {
  79. prefix := []byte("foobar")
  80. deflatedS := AppendDeflateBytes(prefix, []byte(s))
  81. if !bytes.Equal(deflatedS[:len(prefix)], prefix) {
  82. return fmt.Errorf("unexpected prefix when compressing %q: %q. Expecting %q", s, deflatedS[:len(prefix)], prefix)
  83. }
  84. inflatedS, err := AppendInflateBytes(prefix, deflatedS[len(prefix):])
  85. if err != nil {
  86. return fmt.Errorf("unexpected error when uncompressing %q: %w", s, err)
  87. }
  88. if !bytes.Equal(inflatedS[:len(prefix)], prefix) {
  89. return fmt.Errorf("unexpected prefix when uncompressing %q: %q. Expecting %q", s, inflatedS[:len(prefix)], prefix)
  90. }
  91. inflatedS = inflatedS[len(prefix):]
  92. if string(inflatedS) != s {
  93. return fmt.Errorf("unexpected uncompressed string %q. Expecting %q", inflatedS, s)
  94. }
  95. return nil
  96. }
  97. func TestGzipCompressSerial(t *testing.T) {
  98. t.Parallel()
  99. if err := testGzipCompress(); err != nil {
  100. t.Fatal(err)
  101. }
  102. }
  103. func TestGzipCompressConcurrent(t *testing.T) {
  104. t.Parallel()
  105. if err := testConcurrent(10, testGzipCompress); err != nil {
  106. t.Fatal(err)
  107. }
  108. }
  109. func TestFlateCompressSerial(t *testing.T) {
  110. t.Parallel()
  111. if err := testFlateCompress(); err != nil {
  112. t.Fatal(err)
  113. }
  114. }
  115. func TestFlateCompressConcurrent(t *testing.T) {
  116. t.Parallel()
  117. if err := testConcurrent(10, testFlateCompress); err != nil {
  118. t.Fatal(err)
  119. }
  120. }
  121. func testGzipCompress() error {
  122. for _, s := range compressTestcases {
  123. if err := testGzipCompressSingleCase(s); err != nil {
  124. return err
  125. }
  126. }
  127. return nil
  128. }
  129. func testFlateCompress() error {
  130. for _, s := range compressTestcases {
  131. if err := testFlateCompressSingleCase(s); err != nil {
  132. return err
  133. }
  134. }
  135. return nil
  136. }
  137. func testGzipCompressSingleCase(s string) error {
  138. var buf bytes.Buffer
  139. zw := acquireStacklessGzipWriter(&buf, CompressDefaultCompression)
  140. if _, err := zw.Write([]byte(s)); err != nil {
  141. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  142. }
  143. releaseStacklessGzipWriter(zw, CompressDefaultCompression)
  144. zr, err := acquireGzipReader(&buf)
  145. if err != nil {
  146. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  147. }
  148. body, err := ioutil.ReadAll(zr)
  149. if err != nil {
  150. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  151. }
  152. if string(body) != s {
  153. return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
  154. }
  155. releaseGzipReader(zr)
  156. return nil
  157. }
  158. func testFlateCompressSingleCase(s string) error {
  159. var buf bytes.Buffer
  160. zw := acquireStacklessDeflateWriter(&buf, CompressDefaultCompression)
  161. if _, err := zw.Write([]byte(s)); err != nil {
  162. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  163. }
  164. releaseStacklessDeflateWriter(zw, CompressDefaultCompression)
  165. zr, err := acquireFlateReader(&buf)
  166. if err != nil {
  167. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  168. }
  169. body, err := ioutil.ReadAll(zr)
  170. if err != nil {
  171. return fmt.Errorf("unexpected error: %w. s=%q", err, s)
  172. }
  173. if string(body) != s {
  174. return fmt.Errorf("unexpected string after decompression: %q. Expecting %q", body, s)
  175. }
  176. releaseFlateReader(zr)
  177. return nil
  178. }
  179. func testConcurrent(concurrency int, f func() error) error {
  180. ch := make(chan error, concurrency)
  181. for i := 0; i < concurrency; i++ {
  182. go func(idx int) {
  183. err := f()
  184. if err != nil {
  185. ch <- fmt.Errorf("error in goroutine %d: %w", idx, err)
  186. }
  187. ch <- nil
  188. }(i)
  189. }
  190. for i := 0; i < concurrency; i++ {
  191. select {
  192. case err := <-ch:
  193. if err != nil {
  194. return err
  195. }
  196. case <-time.After(time.Second):
  197. return fmt.Errorf("timeout")
  198. }
  199. }
  200. return nil
  201. }