common_test.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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, size int, hash []byte, flags uint32, options []Option) ([]byte, error) {
  28. t.folder = folder
  29. t.name = name
  30. t.offset = offset
  31. t.size = size
  32. t.hash = hash
  33. t.flags = flags
  34. t.options = options
  35. return t.data, nil
  36. }
  37. func (t *TestModel) Close(deviceID DeviceID, err error) {
  38. close(t.closedCh)
  39. }
  40. func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  41. }
  42. func (t *TestModel) isClosed() bool {
  43. select {
  44. case <-t.closedCh:
  45. return true
  46. case <-time.After(1 * time.Second):
  47. return false // Timeout
  48. }
  49. }
  50. type ErrPipe struct {
  51. io.PipeWriter
  52. written int
  53. max int
  54. err error
  55. closed bool
  56. }
  57. func (e *ErrPipe) Write(data []byte) (int, error) {
  58. if e.closed {
  59. return 0, e.err
  60. }
  61. if e.written+len(data) > e.max {
  62. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  63. e.PipeWriter.CloseWithError(e.err)
  64. e.closed = true
  65. return n, e.err
  66. }
  67. return e.PipeWriter.Write(data)
  68. }