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
7package model
8
9import (
10	"testing"
11
12	"github.com/syncthing/syncthing/lib/protocol"
13)
14
15func TestDeviceActivity(t *testing.T) {
16	n0 := Availability{protocol.DeviceID([32]byte{1, 2, 3, 4}), false}
17	n1 := Availability{protocol.DeviceID([32]byte{5, 6, 7, 8}), true}
18	n2 := Availability{protocol.DeviceID([32]byte{9, 10, 11, 12}), false}
19	devices := []Availability{n0, n1, n2}
20	na := newDeviceActivity()
21
22	if lb, ok := na.leastBusy(devices); !ok || lb != n0 {
23		t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
24	}
25	if lb, ok := na.leastBusy(devices); !ok || lb != n0 {
26		t.Errorf("Least busy device should still be n0 (%v) not %v", n0, lb)
27	}
28
29	lb, _ := na.leastBusy(devices)
30	na.using(lb)
31	if lb, ok := na.leastBusy(devices); !ok || lb != n1 {
32		t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
33	}
34	lb, _ = na.leastBusy(devices)
35	na.using(lb)
36	if lb, ok := na.leastBusy(devices); !ok || lb != n2 {
37		t.Errorf("Least busy device should be n2 (%v) not %v", n2, lb)
38	}
39
40	lb, _ = na.leastBusy(devices)
41	na.using(lb)
42	if lb, ok := na.leastBusy(devices); !ok || lb != n0 {
43		t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
44	}
45
46	na.done(n1)
47	if lb, ok := na.leastBusy(devices); !ok || lb != n1 {
48		t.Errorf("Least busy device should be n1 (%v) not %v", n1, lb)
49	}
50
51	na.done(n2)
52	if lb, ok := na.leastBusy(devices); !ok || lb != n1 {
53		t.Errorf("Least busy device should still be n1 (%v) not %v", n1, lb)
54	}
55
56	na.done(n0)
57	if lb, ok := na.leastBusy(devices); !ok || lb != n0 {
58		t.Errorf("Least busy device should be n0 (%v) not %v", n0, lb)
59	}
60}
61