1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
|
package tui
import (
"sync"
"testing"
)
func TestDirty_MarkDirty(t *testing.T) {
testApp.resetDirty()
if testApp.checkAndClearDirty() {
t.Error("checkAndClearDirty() should return false when not marked dirty")
}
testApp.MarkDirty()
if !testApp.checkAndClearDirty() {
t.Error("checkAndClearDirty() should return true after MarkDirty()")
}
}
func TestDirty_CheckAndClearDirty(t *testing.T) {
type tc struct {
markDirty bool
expectFirst bool
expectSecond bool
}
tests := map[string]tc{
"returns true and clears flag when dirty": {
markDirty: true,
expectFirst: true,
expectSecond: false,
},
"returns false when not dirty": {
markDirty: false,
expectFirst: false,
expectSecond: false,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
testApp.resetDirty()
if tt.markDirty {
testApp.MarkDirty()
}
first := testApp.checkAndClearDirty()
if first != tt.expectFirst {
t.Errorf("first checkAndClearDirty() = %v, want %v", first, tt.expectFirst)
}
second := testApp.checkAndClearDirty()
if second != tt.expectSecond {
t.Errorf("second checkAndClearDirty() = %v, want %v", second, tt.expectSecond)
}
})
}
}
func TestDirty_ConcurrentMarkDirty(t *testing.T) {
testApp.resetDirty()
var wg sync.WaitGroup
const numGoroutines = 100
for range numGoroutines {
wg.Go(func() {
testApp.MarkDirty()
})
}
wg.Wait()
if !testApp.checkAndClearDirty() {
t.Error("checkAndClearDirty() should return true after concurrent MarkDirty() calls")
}
if testApp.checkAndClearDirty() {
t.Error("checkAndClearDirty() should return false after first check cleared the flag")
}
}
func TestDirty_ResetDirty(t *testing.T) {
testApp.MarkDirty()
testApp.resetDirty()
if testApp.checkAndClearDirty() {
t.Error("checkAndClearDirty() should return false after resetDirty()")
}
}
|