readers_equal.go 803 B

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