common_test.go 930 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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) IndexUpdate(nodeID string, files []FileInfo) {
  14. }
  15. func (t *TestModel) Request(nodeID, name string, offset uint64, size uint32, hash []byte) ([]byte, error) {
  16. t.name = name
  17. t.offset = offset
  18. t.size = size
  19. t.hash = hash
  20. return t.data, nil
  21. }
  22. func (t *TestModel) Close(nodeID string) {
  23. t.closed = true
  24. }
  25. type ErrPipe struct {
  26. io.PipeWriter
  27. written int
  28. max int
  29. err error
  30. closed bool
  31. }
  32. func (e *ErrPipe) Write(data []byte) (int, error) {
  33. if e.closed {
  34. return 0, e.err
  35. }
  36. if e.written+len(data) > e.max {
  37. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  38. e.PipeWriter.CloseWithError(e.err)
  39. e.closed = true
  40. return n, e.err
  41. } else {
  42. return e.PipeWriter.Write(data)
  43. }
  44. }