common_test.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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(DeviceID, string, []FileInfo)
  14. ccFn func(DeviceID, 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(deviceID DeviceID, folder string, files []FileInfo) {
  24. if t.indexFn != nil {
  25. t.indexFn(deviceID, folder, files)
  26. }
  27. }
  28. func (t *TestModel) IndexUpdate(deviceID DeviceID, folder string, files []FileInfo) {
  29. }
  30. func (t *TestModel) Request(deviceID DeviceID, folder, name string, size int32, offset int64, hash []byte, weakHash uint32, fromTemporary bool) (RequestResponse, error) {
  31. t.folder = folder
  32. t.name = name
  33. t.offset = offset
  34. t.size = size
  35. t.hash = hash
  36. t.weakHash = weakHash
  37. t.fromTemporary = fromTemporary
  38. buf := make([]byte, len(t.data))
  39. copy(buf, t.data)
  40. return &fakeRequestResponse{buf}, nil
  41. }
  42. func (t *TestModel) Closed(conn Connection, err error) {
  43. t.closedErr = err
  44. close(t.closedCh)
  45. }
  46. func (t *TestModel) ClusterConfig(deviceID DeviceID, config ClusterConfig) {
  47. if t.ccFn != nil {
  48. t.ccFn(deviceID, config)
  49. }
  50. }
  51. func (t *TestModel) DownloadProgress(DeviceID, string, []FileDownloadProgressUpdate) {
  52. }
  53. func (t *TestModel) closedError() error {
  54. select {
  55. case <-t.closedCh:
  56. return t.closedErr
  57. case <-time.After(1 * time.Second):
  58. return nil // Timeout
  59. }
  60. }
  61. type fakeRequestResponse struct {
  62. data []byte
  63. }
  64. func (r *fakeRequestResponse) Data() []byte {
  65. return r.data
  66. }
  67. func (r *fakeRequestResponse) Close() {}
  68. func (r *fakeRequestResponse) Wait() {}