1
0

readers_equal.go 875 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. package testutil
  2. import (
  3. "io"
  4. "testing"
  5. "github.com/imgproxy/imgproxy/v3/ioutil"
  6. "github.com/stretchr/testify/require"
  7. )
  8. const bufSize = 4096
  9. // RequireReadersEqual compares two io.Reader contents in a streaming manner.
  10. // It fails the test if contents differ or if reading fails.
  11. func ReadersEqual(t *testing.T, expected, actual io.Reader) bool {
  12. // Marks this function as a test helper so in case failure happens here, location would
  13. // point to the correct line in the calling test.
  14. t.Helper()
  15. buf1 := make([]byte, bufSize)
  16. buf2 := make([]byte, bufSize)
  17. for {
  18. n1, err1 := ioutil.TryReadFull(expected, buf1)
  19. n2, err2 := ioutil.TryReadFull(actual, buf2)
  20. if n1 != n2 {
  21. return false
  22. }
  23. require.Equal(t, buf1[:n1], buf2[:n1])
  24. if err1 == io.EOF && err2 == io.EOF {
  25. return true
  26. }
  27. require.NoError(t, err1)
  28. require.NoError(t, err2)
  29. }
  30. }