1package screensaver
2
3import "testing"
4
5func TestRegistry(t *testing.T) {
6 r := newRegistry()
7 a1, first, ok := r.inhibit(":1.1")
8 if !ok || !first || !r.held() {
9 t.Fatal("first inhibit")
10 }
11 a2, first, _ := r.inhibit(":1.1")
12 if first || a2 == a1 {
13 t.Fatalf("second inhibit: first %v, cookies %d %d", first, a1, a2)
14 }
15 b, first, _ := r.inhibit(":1.2")
16 if !first {
17 t.Fatal("other sender not first")
18 }
19 // Another connection cannot release a cookie it does not own.
20 if r.uninhibit(":1.2", a1) || r.uninhibit(":1.1", 999) {
21 t.Fatal("foreign or unknown cookie released")
22 }
23 if !r.uninhibit(":1.1", a1) || !r.held() {
24 t.Fatal("uninhibit")
25 }
26 r.drop(":1.1")
27 if !r.held() {
28 t.Fatal("drop removed another sender's cookie")
29 }
30 if !r.uninhibit(":1.2", b) || r.held() {
31 t.Fatal("still held")
32 }
33 if _, first, _ := r.inhibit(":1.1"); !first {
34 t.Fatal("dropped sender is not first again")
35 }
36}
37
38func TestRegistryBoundsSender(t *testing.T) {
39 r := newRegistry()
40 for range maxPerSender {
41 if _, _, ok := r.inhibit(":1.1"); !ok {
42 t.Fatal("refused under the bound")
43 }
44 }
45 if _, _, ok := r.inhibit(":1.1"); ok {
46 t.Fatal("accepted over the bound")
47 }
48 if _, _, ok := r.inhibit(":1.2"); !ok {
49 t.Fatal("bound is per sender")
50 }
51}
52
53func TestRegistryCookieWrapSkipsZeroAndUsed(t *testing.T) {
54 r := newRegistry()
55 r.next = ^uint32(0) - 1
56 c1, _, _ := r.inhibit(":1.1") // max
57 c2, _, _ := r.inhibit(":1.1") // wraps past 0
58 if c1 != ^uint32(0) || c2 != 1 {
59 t.Fatalf("cookies %d %d", c1, c2)
60 }
61 r.next = 0
62 if c3, _, _ := r.inhibit(":1.1"); c3 != 2 {
63 t.Fatalf("reused cookie %d", c3)
64 }
65}