Skip to content

Commit

Permalink
修复mem计算逻辑 (#12401)
Browse files Browse the repository at this point in the history
* 修复mem计算逻辑

* 修复mem计算逻辑单测

* fix ci
  • Loading branch information
kangzhaok authored Jul 31, 2024
1 parent cfa3312 commit 3967c91
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion sys/src/main/java/com/alibaba/nacos/sys/env/EnvUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
53 changes: 53 additions & 0 deletions sys/src/test/java/com/alibaba/nacos/sys/env/EnvUtilTest.java
Original file line number Diff line number Diff line change
@@ -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<OperatingSystemBeanManager> 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));
}
}

0 comments on commit 3967c91

Please sign in to comment.