common_test.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // Copyright (C) 2014 Jakob Borg and Contributors (see the CONTRIBUTORS file).
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package protocol
  16. import (
  17. "io"
  18. "time"
  19. )
  20. type TestModel struct {
  21. data []byte
  22. folder string
  23. name string
  24. offset int64
  25. size int
  26. closedCh chan bool
  27. }
  28. func newTestModel() *TestModel {
  29. return &TestModel{
  30. closedCh: make(chan bool),
  31. }
  32. }
  33. func (t *TestModel) Index(deviceID DeviceID, folder string, files []FileInfo) {
  34. }
  35. func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
  36. }
  37. func (t *TestModel) Request(deviceID DeviceID, folder, name string, offset int64, size int) ([]byte, error) {
  38. t.folder = folder
  39. t.name = name
  40. t.offset = offset
  41. t.size = size
  42. return t.data, nil
  43. }
  44. func (t *TestModel) Close(deviceID DeviceID, err error) {
  45. close(t.closedCh)
  46. }
  47. func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfigMessage) {
  48. }
  49. func (t *TestModel) isClosed() bool {
  50. select {
  51. case <-t.closedCh:
  52. return true
  53. case <-time.After(1 * time.Second):
  54. return false // Timeout
  55. }
  56. }
  57. type ErrPipe struct {
  58. io.PipeWriter
  59. written int
  60. max int
  61. err error
  62. closed bool
  63. }
  64. func (e *ErrPipe) Write(data []byte) (int, error) {
  65. if e.closed {
  66. return 0, e.err
  67. }
  68. if e.written+len(data) > e.max {
  69. n, _ := e.PipeWriter.Write(data[:e.max-e.written])
  70. e.PipeWriter.CloseWithError(e.err)
  71. e.closed = true
  72. return n, e.err
  73. }
  74. return e.PipeWriter.Write(data)
  75. }