deviceactivity_test.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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 https://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 := Availability{protocol.DeviceID([32]byte{1, 2, 3, 4}), false}
  13. n1 := Availability{protocol.DeviceID([32]byte{5, 6, 7, 8}), true}
  14. n2 := Availability{protocol.DeviceID([32]byte{9, 10, 11, 12}), false}
  15. devices := []Availability{n0, n1, n2}
  16. na := newDeviceActivity()
  17. if lb := na.leastBusy(devices); lb != 0 {
  18. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  19. }
  20. if lb := na.leastBusy(devices); lb != 0 {
  21. t.Errorf("Least busy device should still be n0 (%v) not %v", n0, lb)
  22. }
  23. lb := na.leastBusy(devices)
  24. na.using(devices[lb])
  25. if lb := na.leastBusy(devices); lb != 1 {
  26. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  27. }
  28. lb = na.leastBusy(devices)
  29. na.using(devices[lb])
  30. if lb := na.leastBusy(devices); lb != 2 {
  31. t.Errorf("Least busy device should be n2 (%v) not %v", n2, lb)
  32. }
  33. lb = na.leastBusy(devices)
  34. na.using(devices[lb])
  35. if lb := na.leastBusy(devices); lb != 0 {
  36. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  37. }
  38. na.done(n1)
  39. if lb := na.leastBusy(devices); lb != 1 {
  40. t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
  41. }
  42. na.done(n2)
  43. if lb := na.leastBusy(devices); lb != 1 {
  44. t.Errorf("Least busy device should still be n1 (%v) not %v", n1, lb)
  45. }
  46. na.done(n0)
  47. if lb := na.leastBusy(devices); lb != 0 {
  48. t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
  49. }
  50. }