Skip to content

Commit 1b73f0c

Browse files
committed
[skip ci] add TestRound
1 parent 8d6ff17 commit 1b73f0c

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

cluster/calcium/realloc.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (c *Calcium) doUpdateContainerWithMemoryPrior(
137137
cpu float64, memory int64) {
138138
for node, containers := range nodeContainers {
139139
for _, container := range containers {
140-
newCPU := utils.Round(container.Quota+cpu, 1)
140+
newCPU := utils.Round(container.Quota+cpu, 2)
141141
newMemory := container.Memory + memory
142142
// 内存不能低于 4MB
143143
if newCPU <= 0 || newMemory <= minMemory {

utils/utils_test.go

+16-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"fmt"
88
"io/ioutil"
99
"os"
10+
"strconv"
1011
"strings"
1112
"testing"
1213

@@ -125,15 +126,14 @@ func TestParseContainerName(t *testing.T) {
125126
}
126127

127128
func TestPublishInfo(t *testing.T) {
128-
node := &types.Node{Endpoint: "tcp://127.0.0.1:12345"}
129129
ports := []string{"123", "233"}
130130
n1 := map[string]*network.EndpointSettings{
131131
"n1": &network.EndpointSettings{
132132
IPAddress: "233.233.233.233",
133133
},
134134
"host": &network.EndpointSettings{},
135135
}
136-
r := MakePublishInfo(n1, node, ports)
136+
r := MakePublishInfo(n1, "127.0.0.1", ports)
137137
assert.Equal(t, len(r), 2)
138138
assert.Equal(t, len(r["n1"]), 2)
139139
assert.Equal(t, r["n1"][0], "233.233.233.233:123")
@@ -227,3 +227,17 @@ func TestCreateTarStream(t *testing.T) {
227227
_, err = CreateTarStream(fname)
228228
assert.NoError(t, err)
229229
}
230+
231+
func TestRound(t *testing.T) {
232+
f := func(f float64) string {
233+
return strconv.FormatFloat(f, 'f', -1, 64)
234+
}
235+
a := 0.0199999998
236+
assert.Equal(t, f(Round(a, 2)), "0.02")
237+
a = 0.1999998
238+
assert.Equal(t, f(Round(a, 2)), "0.2")
239+
a = 1.999998
240+
assert.Equal(t, f(Round(a, 2)), "2")
241+
a = 19.99998
242+
assert.Equal(t, f(Round(a, 2)), "20")
243+
}

0 commit comments

Comments
 (0)