slfan1989 commented on code in PR #5335:
URL: https://github.com/apache/hadoop/pull/5335#discussion_r1095999019
##########
hadoop-yarn-project/hadoop-yarn/hadoop-yarn-api/src/test/java/org/apache/hadoop/yarn/api/records/TestResource.java:
##########
@@ -42,4 +42,70 @@ void testCastToIntSafely() {
"Cast to Integer.MAX_VALUE if the long is greater than "
+ "Integer.MAX_VALUE");
}
+
+ @Test
+ public void testResourceFormatted() {
+ // We set 10MB
+ String expectedResult1 = "<memory:10 MB, vCores:1>";
+ MockResource capability1 = new MockResource();
+ capability1.setMemory(10);
+ capability1.setVirtualCores(1);
+ assertEquals(capability1.toFormattedString(), expectedResult1);
+
+ // We set 1024 MB = 1GB
+ String expectedResult2 = "<memory:1 GB, vCores:1>";
+ MockResource capability2 = new MockResource();
+ capability2.setMemory(1024);
+ capability2.setVirtualCores(1);
+ assertEquals(capability2.toFormattedString(), expectedResult2);
+
+ // We set 1024 * 1024 MB = 1024 GB = 1TB
+ String expectedResult3 = "<memory:1 TB, vCores:1>";
+ MockResource capability3 = new MockResource();
+ capability3.setMemory(1024 * 1024);
+ capability3.setVirtualCores(1);
+ assertEquals(capability3.toFormattedString(), expectedResult3);
+
+ // We set 1024 * 1024 * 1024 MB = 1024 * 1024 GB = 1 * 1024 TB = 1 PB
+ String expectedResult4 = "<memory:1 PB, vCores:1>";
+ MockResource capability4 = new MockResource();
+ capability4.setMemory(1024 * 1024 * 1024);
+ capability4.setVirtualCores(1);
+ assertEquals(capability4.toFormattedString(), expectedResult4);
+ }
+
+ class MockResource extends Resource {
Review Comment:
Thanks for reviewing the code. In this unit test, we want to test whether
the return result of `toFormattedString()` is as expected.

The implementation class and definition of Resource are not in the same
package. If we try to call it, classNotFound will be reported.
Use Class `LightWeightResource`, The initialization of this class requires
org.apache.hadoop.yarn.LocalConfigurationProvider, which is under the
`hadoop-yarn-common` package and cannot be referenced.
```
Resource resource = Resource.newInstance(10,1);
String expectedResult1 = "<memory:10 MB, vCores:1>";
assertEquals(resource.toFormattedString(), expectedResult1);
Error Msg:
Caused by: java.lang.ClassNotFoundException:
org.apache.hadoop.yarn.LocalConfigurationProvider
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:264)
...
```
Use Class `ResourcePBImpl`, which is under the `hadoop-yarn-common`
package and cannot be referenced.
```
Resource resource = spy(Resource.class);
String expectedResult1 = "<memory:10 MB, vCores:1>";
when(resource.getResources()).thenReturn(new ResourceInformation[0]);
when(resource.getMemorySize()).thenReturn(10L);
when(resource.getVirtualCores()).thenReturn(1);
assertEquals(resource.toFormattedString(), expectedResult1);
Error Msg:
org.apache.commons.lang3.NotImplementedException: This method is implemented
by ResourcePBImpl
...
```
I modified the code, please help to review again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]