common_test.go 1.7 KB

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