common_test.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. weakHash uint32
  18. fromTemporary bool
  19. indexFn func(string, []FileInfo)
  20. ccFn func(*ClusterConfig)
  21. closedCh chan struct{}
  22. closedErr error
  23. }
  24. func newTestModel() *TestModel {
  25. return &TestModel{
  26. closedCh: make(chan struct{}),
  27. }
  28. }
  29. func (t *TestModel) Index(_ Connection, idx *Index) error {
  30. if t.indexFn != nil {
  31. t.indexFn(idx.Folder, idx.Files)
  32. }
  33. return nil
  34. }
  35. func (*TestModel) IndexUpdate(Connection, *IndexUpdate) error {
  36. return nil
  37. }
  38. func (t *TestModel) Request(_ Connection, req *Request) (RequestResponse, error) {
  39. t.folder = req.Folder
  40. t.name = req.Name
  41. t.offset = req.Offset
  42. t.size = int32(req.Size)
  43. t.hash = req.Hash
  44. t.weakHash = req.WeakHash
  45. t.fromTemporary = req.FromTemporary
  46. buf := make([]byte, len(t.data))
  47. copy(buf, t.data)
  48. return &fakeRequestResponse{buf}, nil
  49. }
  50. func (t *TestModel) Closed(_ Connection, err error) {
  51. t.closedErr = err
  52. close(t.closedCh)
  53. }
  54. func (t *TestModel) ClusterConfig(_ Connection, config *ClusterConfig) error {
  55. if t.ccFn != nil {
  56. t.ccFn(config)
  57. }
  58. return nil
  59. }
  60. func (*TestModel) DownloadProgress(Connection, *DownloadProgress) error {
  61. return nil
  62. }
  63. func (t *TestModel) closedError() error {
  64. select {
  65. case <-t.closedCh:
  66. return t.closedErr
  67. case <-time.After(1 * time.Second):
  68. return nil // Timeout
  69. }
  70. }
  71. type fakeRequestResponse struct {
  72. data []byte
  73. }
  74. func (r *fakeRequestResponse) Data() []byte {
  75. return r.data
  76. }
  77. func (*fakeRequestResponse) Close() {}
  78. func (*fakeRequestResponse) Wait() {}