common_test.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package protocol
  7. import (
  8. "time"
  9. )
  10. type TestModel struct {
  11. data []byte
  12. folder string
  13. name string
  14. offset int64
  15. size int32
  16. hash []byte
  17. fromTemporary bool
  18. indexFn func(string, []FileInfo)
  19. ccFn func(*ClusterConfig)
  20. closedCh chan struct{}
  21. closedErr error
  22. }
  23. func newTestModel() *TestModel {
  24. return &TestModel{
  25. closedCh: make(chan struct{}),
  26. }
  27. }
  28. func (t *TestModel) Index(_ Connection, idx *Index) error {
  29. if t.indexFn != nil {
  30. t.indexFn(idx.Folder, idx.Files)
  31. }
  32. return nil
  33. }
  34. func (*TestModel) IndexUpdate(Connection, *IndexUpdate) error {
  35. return nil
  36. }
  37. func (t *TestModel) Request(_ Connection, req *Request) (RequestResponse, error) {
  38. t.folder = req.Folder
  39. t.name = req.Name
  40. t.offset = req.Offset
  41. t.size = int32(req.Size)
  42. t.hash = req.Hash
  43. t.fromTemporary = req.FromTemporary
  44. buf := make([]byte, len(t.data))
  45. copy(buf, t.data)
  46. return &fakeRequestResponse{buf}, nil
  47. }
  48. func (t *TestModel) Closed(_ Connection, err error) {
  49. t.closedErr = err
  50. close(t.closedCh)
  51. }
  52. func (t *TestModel) ClusterConfig(_ Connection, config *ClusterConfig) error {
  53. if t.ccFn != nil {
  54. t.ccFn(config)
  55. }
  56. return nil
  57. }
  58. func (*TestModel) DownloadProgress(Connection, *DownloadProgress) error {
  59. return nil
  60. }
  61. func (t *TestModel) closedError() error {
  62. select {
  63. case <-t.closedCh:
  64. return t.closedErr
  65. case <-time.After(1 * time.Second):
  66. return nil // Timeout
  67. }
  68. }
  69. type fakeRequestResponse struct {
  70. data []byte
  71. }
  72. func (r *fakeRequestResponse) Data() []byte {
  73. return r.data
  74. }
  75. func (*fakeRequestResponse) Close() {}
  76. func (*fakeRequestResponse) Wait() {}