fileinfobatch_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (C) 2018 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 model
  7. import (
  8. "errors"
  9. "testing"
  10. "github.com/syncthing/syncthing/lib/protocol"
  11. )
  12. func TestFileInfoBatchError(t *testing.T) {
  13. // Verify behaviour of the flush function returning an error.
  14. var errReturn error
  15. var called int
  16. b := NewFileInfoBatch(func([]protocol.FileInfo) error {
  17. called += 1
  18. return errReturn
  19. })
  20. // Flush should work when the flush function error is nil
  21. b.Append(protocol.FileInfo{Name: "test"})
  22. if err := b.Flush(); err != nil {
  23. t.Fatalf("expected nil, got %v", err)
  24. }
  25. if called != 1 {
  26. t.Fatalf("expected 1, got %d", called)
  27. }
  28. // Flush should fail with an error retur
  29. errReturn = errors.New("problem")
  30. b.Append(protocol.FileInfo{Name: "test"})
  31. if err := b.Flush(); err != errReturn {
  32. t.Fatalf("expected %v, got %v", errReturn, err)
  33. }
  34. if called != 2 {
  35. t.Fatalf("expected 2, got %d", called)
  36. }
  37. // Flush function should not be called again when it's already errored,
  38. // same error should be returned by Flush()
  39. if err := b.Flush(); err != errReturn {
  40. t.Fatalf("expected %v, got %v", errReturn, err)
  41. }
  42. if called != 2 {
  43. t.Fatalf("expected 2, got %d", called)
  44. }
  45. // Reset should clear the error (and the file list)
  46. errReturn = nil
  47. b.Reset()
  48. b.Append(protocol.FileInfo{Name: "test"})
  49. if err := b.Flush(); err != nil {
  50. t.Fatalf("expected nil, got %v", err)
  51. }
  52. if called != 3 {
  53. t.Fatalf("expected 3, got %d", called)
  54. }
  55. }