common_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import (
  4. "io"
  5. "time"
  6. )
  7. type TestModel struct {
  8. data []byte
  9. folder string
  10. name string
  11. offset int64
  12. size int
  13. hash []byte
  14. flags uint32
  15. options []Option
  16. closedCh chan bool
  17. }
  18. func newTestModel() *TestModel {
  19. return &TestModel{
  20. closedCh: make(chan bool),
  21. }
  22. }
  23. func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  24. }
  25. func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  26. }
  27. func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
  28. t.folder = folder
  29. t.name = name
  30. t.offset = offset
  31. t.size = len(buf)
  32. t.hash = hash
  33. t.flags = flags
  34. t.options = options
  35. copy(buf, t.data)
  36. return nil
  37. }
  38. func (t *TestModel) Close(deviceID DeviceID, err error) {
  39. close(t.closedCh)
  40. }
  41. func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  42. }
  43. func (t *TestModel) isClosed() bool {
  44. select {
  45. case <-t.closedCh:
  46. return true
  47. case <-time.After(1 * time.Second):
  48. return false // Timeout
  49. }
  50. }
  51. type ErrPipe struct {
  52. io.PipeWriter
  53. written int
  54. max int
  55. err error
  56. closed bool
  57. }
  58. func (e *ErrPipe) Write(data []byte) (int, error) {
  59. if e.closed {
  60. return 0, e.err
  61. }
  62. if e.written+len(data) > e.max {
  63. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  64. e.PipeWriter.CloseWithError(e.err)
  65. e.closed = true
  66. return n, e.err
  67. }
  68. return e.PipeWriter.Write(data)
  69. }