common_test.go 975 B

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