-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresources_test.go
81 lines (73 loc) · 2.19 KB
/
resources_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
package schedule
import (
"testing"
)
func TestResourceVectorPoolRequest(t *testing.T) {
pool := NewResourceVectorPool([]int{1, 2})
requesting := &resourceVector{resources: []int{0, 0}}
returned := pool.Request(requesting)
if returned == nil {
t.Error("expected valid resource request")
}
if !(pool.resources[0] == 1 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
requesting = &resourceVector{resources: []int{2, 0}}
returned = pool.Request(requesting)
if returned != nil {
t.Error("expected invalid resource request")
}
if !(pool.resources[0] == 1 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
requesting = &resourceVector{resources: []int{1, 0}}
returned = pool.Request(requesting)
if returned == nil {
t.Error("expected valid resource request")
}
if !(pool.resources[0] == 0 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
requesting = &resourceVector{resources: []int{1}}
returned = pool.Request(requesting)
if returned != nil {
t.Error("expected invalid resource request")
}
if !(pool.resources[0] == 0 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
}
func TestResourceVectorReturn(t *testing.T) {
pool := NewResourceVectorPool([]int{1, 2})
requesting := &resourceVector{resources: []int{1, 0}}
returned := pool.Request(requesting)
if !(pool.resources[0] == 0 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
vec := returned.(*resourceVector)
if vec.pool == nil {
t.Error("expected pool present")
}
if !(vec.resources[0] == 1 && vec.resources[1] == 0) {
t.Error("unexpected vector resources")
}
// return the first time should replenish the pool of the resources
res := vec.Return()
if !res {
t.Error("expected successful return")
}
if vec.pool != nil {
t.Error("expected pool not present")
}
if !(pool.resources[0] == 1 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
// return a second time should be idempotent
res = vec.Return()
if res {
t.Error("expected unsuccessful return")
}
if !(pool.resources[0] == 1 && pool.resources[1] == 2) {
t.Error("unexpected pool resource values")
}
}