-
Notifications
You must be signed in to change notification settings - Fork 0
/
starvation_test.go
103 lines (87 loc) · 1.85 KB
/
starvation_test.go
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
package problems
// Starvation: threads are unable to progress due to greedy or high priority threads blocking shared resources.
import (
"sync"
"testing"
"time"
)
// shared resource
var l = &lock{}
type lock struct {
m sync.Mutex
}
var thr = &threads{}
type threads struct {
created int
finished int
m sync.Mutex
}
func (t *threads) create() {
t.m.Lock()
t.created++
t.m.Unlock()
}
func (t *threads) finish() {
t.m.Lock()
t.finished++
t.m.Unlock()
}
func (t *threads) values() (int, int) {
t.m.Lock()
defer t.m.Unlock()
return t.created, t.finished
}
// greedy function that locks the shared resource for a long time
func greedy() {
l.m.Lock()
time.Sleep(2 * time.Second)
l.m.Unlock()
}
// normal function that locks the shared resource for a small amount of time
func normal(finish chan bool) {
l.m.Lock()
time.Sleep(100 * time.Millisecond)
finish <- true
l.m.Unlock()
}
func Test_Starvation(t *testing.T) {
create := make(chan bool)
finish := make(chan bool)
// register created/finished thread counters
go func() {
for {
select {
case <-create:
thr.create()
case <-finish:
thr.finish()
}
}
}()
threads := time.Tick(100 * time.Millisecond)
stop := time.After(5 * time.Second)
greed := time.After(2 * time.Second)
loop:
for {
select {
case <-greed:
// start a single greedy go routine after 2s
go greedy()
case <-threads:
// start a new normal go routine every 100ms
go normal(finish)
create <- true
case <-stop:
// wait 100ms to let normal go routines finish and break the loop after 5s
time.Sleep(100 * time.Millisecond)
break loop
}
}
c, f := thr.values()
if c != f {
// assert that number of finished threads is different than the number of created threads
t.Logf("number of created threads: %v", c)
t.Logf("number of finished threads: %v", f)
t.Fatal()
}
}