Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复mem计算逻辑 #12401

Merged
merged 3 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
36 changes: 36 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,36 @@
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 test() {
systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getFreePhysicalMem()).thenReturn(123l);
systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getTotalPhysicalMem()).thenReturn(2048l);
assertEquals(EnvUtil.getMem(), 1 - ((double) 123 / (double) 2048));

systemBeanManagerMocked.when(() -> OperatingSystemBeanManager.getFreePhysicalMem()).thenReturn(0l);
assertEquals(EnvUtil.getMem(), 1 - ((double) 0 / (double) 2048));
}
}
Loading