common_test.go 860 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. package protocol
  2. import "io"
  3. type TestModel struct {
  4. data []byte
  5. name string
  6. offset uint64
  7. size uint32
  8. hash []byte
  9. closed bool
  10. }
  11. func (t *TestModel) Index(nodeID string, files []FileInfo) {
  12. }
  13. func (t *TestModel) Request(nodeID, name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
  14. t.name = name
  15. t.offset = offset
  16. t.size = size
  17. t.hash = hash
  18. return t.data, nil
  19. }
  20. func (t *TestModel) Close(nodeID string) {
  21. t.closed = true
  22. }
  23. type ErrPipe struct {
  24. io.PipeWriter
  25. written int
  26. max int
  27. err error
  28. closed bool
  29. }
  30. func (e *ErrPipe) Write(data []byte) (int, error) {
  31. if e.closed {
  32. return 0, e.err
  33. }
  34. if e.written+len(data) > e.max {
  35. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  36. e.PipeWriter.CloseWithError(e.err)
  37. e.closed = true
  38. return n, e.err
  39. } else {
  40. return e.PipeWriter.Write(data)
  41. }
  42. }