common_test.go 1.6 KB

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