-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathcalculator_test.go
128 lines (105 loc) · 3.38 KB
/
calculator_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
* Copyright 2015-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package calculator_test
import (
"testing"
"github.com/cloudfoundry/java-buildpack-memory-calculator/v4/calculator"
"github.com/cloudfoundry/java-buildpack-memory-calculator/v4/flags"
"github.com/cloudfoundry/java-buildpack-memory-calculator/v4/memory"
. "github.com/onsi/gomega"
"github.com/sclevine/spec"
)
func TestCalculator(t *testing.T) {
spec.Run(t, "Calculator", func(t *testing.T, _ spec.G, it spec.S) {
g := NewGomegaWithT(t)
var c calculator.Calculator
it.Before(func() {
h := flags.HeadRoom(0)
j := flags.JVMOptions{}
l := flags.LoadedClassCount(1000)
t := flags.ThreadCount(10)
m := flags.TotalMemory(500 * memory.Mibi)
c = calculator.Calculator{HeadRoom: &h, JvmOptions: &j, LoadedClassCount: &l, ThreadCount: &t, TotalMemory: &m}
})
it("uses default and calculated values", func() {
g.Expect(c.Calculate()).To(ConsistOf(
memory.MaxMetaspace(19800000),
memory.DefaultReservedCodeCache,
memory.DefaultStack,
memory.MaxHeap(242344000),
))
})
it("uses configured direct memory", func() {
d := memory.MaxDirectMemory(memory.Mibi)
c.JvmOptions.MaxDirectMemory = &d
g.Expect(c.Calculate()).To(ConsistOf(
memory.MaxDirectMemory(1048576),
memory.MaxMetaspace(19800000),
memory.DefaultReservedCodeCache,
memory.DefaultStack,
memory.MaxHeap(242344000),
))
})
it("uses configured metaspace", func() {
m := memory.MaxMetaspace(memory.Mibi)
c.JvmOptions.MaxMetaspace = &m
g.Expect(c.Calculate()).To(ConsistOf(
memory.DefaultReservedCodeCache,
memory.DefaultStack,
memory.MaxHeap(261095424),
))
})
it("uses configured reserved code cache", func() {
r := memory.ReservedCodeCache(memory.Mibi)
c.JvmOptions.ReservedCodeCache = &r
g.Expect(c.Calculate()).To(ConsistOf(
memory.MaxMetaspace(19800000),
memory.DefaultStack,
memory.MaxHeap(492953664),
))
})
it("uses configured stack", func() {
s := memory.Stack(memory.Mibi)
c.JvmOptions.Stack = &s
g.Expect(c.Calculate()).To(ConsistOf(
memory.MaxMetaspace(19800000),
memory.DefaultReservedCodeCache,
memory.MaxHeap(242344000),
))
})
it("uses configured heap", func() {
h := memory.MaxHeap(memory.Mibi)
c.JvmOptions.MaxHeap = &h
g.Expect(c.Calculate()).To(ConsistOf(
memory.MaxMetaspace(19800000),
memory.DefaultReservedCodeCache,
memory.DefaultStack,
))
})
it("returns error if overhead is too large", func() {
m := memory.MaxMetaspace(500 * memory.Mibi)
c.JvmOptions.MaxMetaspace = &m
_, err := c.Calculate()
g.Expect(err).To(HaveOccurred())
})
it("returns error if configured heap is too large", func() {
h := memory.MaxHeap(500 * memory.Mibi)
c.JvmOptions.MaxHeap = &h
_, err := c.Calculate()
g.Expect(err).To(HaveOccurred())
})
})
}