deviceactivity_test.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright (C) 2014 The Syncthing Authors.
  2. //
  3. // This program is free software: you can redistribute it and/or modify it
  4. // under the terms of the GNU General Public License as published by the Free
  5. // Software Foundation, either version 3 of the License, or (at your option)
  6. // any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful, but WITHOUT
  9. // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. // more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program. If not, see <http://www.gnu.org/licenses/>.
  15. package model
  16. import (
  17. "testing"
  18. "github.com/syncthing/syncthing/internal/protocol"
  19. )
  20. func TestDeviceActivity(t *testing.T) {
  21. n0 := protocol.DeviceID{1, 2, 3, 4}
  22. n1 := protocol.DeviceID{5, 6, 7, 8}
  23. n2 := protocol.DeviceID{9, 10, 11, 12}
  24. devices := []protocol.DeviceID{n0, n1, n2}
  25. na := newDeviceActivity()
  26. if lb := na.leastBusy(devices); lb != n0 {
  27. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  28. }
  29. if lb := na.leastBusy(devices); lb != n0 {
  30. t.Errorf("Least busy device should still be n0 (%v) not %v", n0, lb)
  31. }
  32. na.using(na.leastBusy(devices))
  33. if lb := na.leastBusy(devices); lb != n1 {
  34. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  35. }
  36. na.using(na.leastBusy(devices))
  37. if lb := na.leastBusy(devices); lb != n2 {
  38. t.Errorf("Least busy device should be n2 (%v) not %v", n2, lb)
  39. }
  40. na.using(na.leastBusy(devices))
  41. if lb := na.leastBusy(devices); lb != n0 {
  42. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  43. }
  44. na.done(n1)
  45. if lb := na.leastBusy(devices); lb != n1 {
  46. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  47. }
  48. na.done(n2)
  49. if lb := na.leastBusy(devices); lb != n1 {
  50. t.Errorf("Least busy device should still be n1 (%v) not %v", n1, lb)
  51. }
  52. na.done(n0)
  53. if lb := na.leastBusy(devices); lb != n0 {
  54. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  55. }
  56. }