buffered_writer_test.go 642 B

123456789101112131415161718192021222324252627282930
  1. package io_test
  2. import (
  3. "testing"
  4. "github.com/v2ray/v2ray-core/common/alloc"
  5. . "github.com/v2ray/v2ray-core/common/io"
  6. v2testing "github.com/v2ray/v2ray-core/testing"
  7. "github.com/v2ray/v2ray-core/testing/assert"
  8. )
  9. func TestBufferedWriter(t *testing.T) {
  10. v2testing.Current(t)
  11. content := alloc.NewLargeBuffer().Clear()
  12. writer := NewBufferedWriter(content)
  13. assert.Bool(writer.Cached()).IsTrue()
  14. payload := make([]byte, 16)
  15. nBytes, err := writer.Write(payload)
  16. assert.Int(nBytes).Equals(16)
  17. assert.Error(err).IsNil()
  18. assert.Bool(content.IsEmpty()).IsTrue()
  19. writer.SetCached(false)
  20. assert.Int(content.Len()).Equals(16)
  21. }