This repository has been archived by the owner on Aug 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resources_test.go
78 lines (70 loc) · 2.09 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
package main
import (
. "github.com/smartystreets/goconvey/convey"
"testing"
)
func TestResources(t *testing.T) {
c := Config{3000, 2, 4}
a := Resources{}
a.Init(c)
Convey("Empty valid list", t, func() {
list := a.List()
So(list, ShouldContainSubstring, "\"allocated\":{}")
})
Convey("Allocates first resource", t, func() {
answer, err := a.tryAllocate("alice", c.Workers)
So(err, ShouldBeNil)
So(answer, ShouldEqual, 1)
})
Convey("Allocates second resource", t, func() {
answer, err := a.tryAllocate("bob", c.Workers)
So(err, ShouldBeNil)
So(answer, ShouldEqual, 2)
})
Convey("Limit exists", t, func() {
_, err := a.tryAllocate("limit", c.Workers)
So(err, ShouldNotBeNil)
})
Convey("List has valid users", t, func() {
list := a.List()
So(list, ShouldContainSubstring, "alice")
So(list, ShouldContainSubstring, "bob")
})
Convey("Reseting gives empty list", t, func() {
a.Reset(c.Workers)
list := a.List()
So(list, ShouldNotContainSubstring, "alice")
So(list, ShouldNotContainSubstring, "bob")
})
Convey("Deallocating invalid id gives error", t, func() {
err := a.tryDeallocate(1, c.Workers)
So(err, ShouldNotBeNil)
})
Convey("Deallocating removes user", t, func() {
john, _ := a.tryAllocate("john", c.Workers)
a.tryAllocate("ted", c.Workers)
err := a.tryDeallocate(john, c.Workers)
list := a.List()
So(err, ShouldBeNil)
So(list, ShouldNotContainSubstring, "john")
So(list, ShouldContainSubstring, "ted")
})
Convey("Search works properly", t, func() {
a.Reset(c.Workers)
a.tryAllocate("bob", c.Workers)
a.tryAllocate("alice", c.Workers)
empty_search := a.Search("empty")
bob_search := a.Search("bob")
alice_search := a.Search("alice")
So(empty_search, ShouldContainSubstring, "[]")
So(bob_search, ShouldContainSubstring, "[\"r1\"]")
So(alice_search, ShouldContainSubstring, "[\"r2\"]")
})
Convey("Search works properly for several resources", t, func() {
a.Reset(c.Workers)
a.tryAllocate("bob", c.Workers)
a.tryAllocate("bob", c.Workers)
bob_search := a.Search("bob")
So(bob_search, ShouldContainSubstring, "[\"r1\",\"r2\"]")
})
}