common_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 struct{}
  17. closedErr error
  18. }
  19. func newTestModel() *TestModel {
  20. return &TestModel{
  21. closedCh: make(chan struct{}),
  22. }
  23. }
  24. func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  25. }
  26. func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo, flags uint32, options []Option) {
  27. }
  28. func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, hash []byte, flags uint32, options []Option, buf []byte) error {
  29. t.folder = folder
  30. t.name = name
  31. t.offset = offset
  32. t.size = len(buf)
  33. t.hash = hash
  34. t.flags = flags
  35. t.options = options
  36. copy(buf, t.data)
  37. return nil
  38. }
  39. func (t *TestModel) Close(deviceID DeviceID, err error) {
  40. t.closedErr = err
  41. close(t.closedCh)
  42. }
  43. func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  44. }
  45. func (t *TestModel) closedError() error {
  46. select {
  47. case <-t.closedCh:
  48. return t.closedErr
  49. case <-time.After(1 * time.Second):
  50. return nil // Timeout
  51. }
  52. }
  53. type ErrPipe struct {
  54. io.PipeWriter
  55. written int
  56. max int
  57. err error
  58. closed bool
  59. }
  60. func (e *ErrPipe) Write(data []byte) (int, error) {
  61. if e.closed {
  62. return 0, e.err
  63. }
  64. if e.written+len(data) > e.max {
  65. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  66. e.PipeWriter.CloseWithError(e.err)
  67. e.closed = true
  68. return n, e.err
  69. }
  70. return e.PipeWriter.Write(data)
  71. }