From 3967c91d1a907ab8edfc3bd5f75480d735a8b79c Mon Sep 17 00:00:00 2001 From: kangzhao <34024756+kangzhaok@users.noreply.github.com> Date: Wed, 31 Jul 2024 14:29:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dmem=E8=AE=A1=E7=AE=97?= =?UTF-8?q?=E9=80=BB=E8=BE=91=20(#12401)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 修复mem计算逻辑 * 修复mem计算逻辑单测 * fix ci --- .../com/alibaba/nacos/sys/env/EnvUtil.java | 2 +- .../alibaba/nacos/sys/env/EnvUtilTest.java | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilTest.java 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 c56ab34bfdb..6c87d0a7c5e 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 static float getCpu() { 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 00000000000..f9fdd2ef216 --- /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)); + } +}