deviceactivity_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (C) 2014 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 http://mozilla.org/MPL/2.0/.
  6. package model
  7. import (
  8. "testing"
  9. "github.com/syncthing/syncthing/lib/protocol"
  10. )
  11. func TestDeviceActivity(t *testing.T) {
  12. n0 := protocol.DeviceID([32]byte{1, 2, 3, 4})
  13. n1 := protocol.DeviceID([32]byte{5, 6, 7, 8})
  14. n2 := protocol.DeviceID([32]byte{9, 10, 11, 12})
  15. devices := []protocol.DeviceID{n0, n1, n2}
  16. na := newDeviceActivity()
  17. if lb := na.leastBusy(devices); lb != n0 {
  18. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  19. }
  20. if lb := na.leastBusy(devices); lb != n0 {
  21. t.Errorf("Least busy device should still be n0 (%v) not %v", n0, lb)
  22. }
  23. na.using(na.leastBusy(devices))
  24. if lb := na.leastBusy(devices); lb != n1 {
  25. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  26. }
  27. na.using(na.leastBusy(devices))
  28. if lb := na.leastBusy(devices); lb != n2 {
  29. t.Errorf("Least busy device should be n2 (%v) not %v", n2, lb)
  30. }
  31. na.using(na.leastBusy(devices))
  32. if lb := na.leastBusy(devices); lb != n0 {
  33. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  34. }
  35. na.done(n1)
  36. if lb := na.leastBusy(devices); lb != n1 {
  37. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  38. }
  39. na.done(n2)
  40. if lb := na.leastBusy(devices); lb != n1 {
  41. t.Errorf("Least busy device should still be n1 (%v) not %v", n1, lb)
  42. }
  43. na.done(n0)
  44. if lb := na.leastBusy(devices); lb != n0 {
  45. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  46. }
  47. }