diff --git a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java index c56ab34bf..6c87d0a7c 100644 --- a/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java +++ b/sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java @@ -328,7 +328,7 @@ public class EnvUtil { public static float getMem() { return (float) (1 - - OperatingSystemBeanManager.getFreePhysicalMem() / OperatingSystemBeanManager.getTotalPhysicalMem()); + - (double) OperatingSystemBeanManager.getFreePhysicalMem() / (double) OperatingSystemBeanManager.getTotalPhysicalMem()); } public static String getConfPath() { diff --git a/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilTest.java b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilTest.java new file mode 100644 index 000000000..f9fdd2ef2 --- /dev/null +++ b/sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 1999-2020 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.sys.env; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; + +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class EnvUtilTest { + + MockedStatic systemBeanManagerMocked; + + @BeforeEach + void before() { + systemBeanManagerMocked = Mockito.mockStatic(OperatingSystemBeanManager.class); + } + + @AfterEach + void after() { + if (!systemBeanManagerMocked.isClosed()) { + systemBeanManagerMocked.close(); + } + } + + @Test + public void testGetMem() { + systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getFreePhysicalMem()).thenReturn(123L); + systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getTotalPhysicalMem()).thenReturn(2048L); + assertEquals(EnvUtil.getMem(), 1 - ((double) 123L / (double) 2048L)); + + systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getFreePhysicalMem()).thenReturn(0L); + assertEquals(EnvUtil.getMem(), 1 - ((double) 0L / (double) 2048L)); + } +}