common_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. // Copyright (C) 2014 The Protocol Authors.
  2. package protocol
  3. import "time"
  4. type TestModel struct {
  5. data []byte
  6. folder string
  7. name string
  8. offset int64
  9. size int32
  10. hash []byte
  11. weakHash uint32
  12. fromTemporary bool
  13. indexFn func(string, []FileInfo)
  14. ccFn func(ClusterConfig)
  15. closedCh chan struct{}
  16. closedErr error
  17. }
  18. func newTestModel() *TestModel {
  19. return &TestModel{
  20. closedCh: make(chan struct{}),
  21. }
  22. }
  23. func (t *TestModel) Index(_ Connection, folder string, files []FileInfo) error {
  24. if t.indexFn != nil {
  25. t.indexFn(folder, files)
  26. }
  27. return nil
  28. }
  29. func (*TestModel) IndexUpdate(Connection, string, []FileInfo) error {
  30. return nil
  31. }
  32. func (t *TestModel) Request(_ Connection, folder, name string, _, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
  33. t.folder = folder
  34. t.name = name
  35. t.offset = offset
  36. t.size = size
  37. t.hash = hash
  38. t.weakHash = weakHash
  39. t.fromTemporary = fromTemporary
  40. buf := make([]byte, len(t.data))
  41. copy(buf, t.data)
  42. return &fakeRequestResponse{buf}, nil
  43. }
  44. func (t *TestModel) Closed(_ Connection, err error) {
  45. t.closedErr = err
  46. close(t.closedCh)
  47. }
  48. func (t *TestModel) ClusterConfig(_ Connection, config ClusterConfig) error {
  49. if t.ccFn != nil {
  50. t.ccFn(config)
  51. }
  52. return nil
  53. }
  54. func (*TestModel) DownloadProgress(Connection, string, []FileDownloadProgressUpdate) error {
  55. return nil
  56. }
  57. func (t *TestModel) closedError() error {
  58. select {
  59. case <-t.closedCh:
  60. return t.closedErr
  61. case <-time.After(1 * time.Second):
  62. return nil // Timeout
  63. }
  64. }
  65. type fakeRequestResponse struct {
  66. data []byte
  67. }
  68. func (r *fakeRequestResponse) Data() []byte {
  69. return r.data
  70. }
  71. func (*fakeRequestResponse) Close() {}
  72. func (*fakeRequestResponse) Wait() {}