KarmaGYZ commented on a change in pull request #11854: URL: https://github.com/apache/flink/pull/11854#discussion_r425552834
########## File path: flink-yarn/src/test/java/org/apache/flink/yarn/ResourceInformationReflectorTest.java ########## @@ -0,0 +1,171 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.flink.yarn; + +import org.apache.flink.runtime.util.HadoopUtils; +import org.apache.flink.util.TestLogger; + +import org.apache.hadoop.yarn.api.records.Resource; +import org.junit.Test; + +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assume.assumeTrue; + +/** + * Tests for {@link ResourceInformationReflector}. + */ +public class ResourceInformationReflectorTest extends TestLogger { + + private static final String RESOURCE_NAME = "test"; + private static final long RESOURCE_VALUE = 1; + + @Test + public void testSetResourceInformationIfMethodPresent() { + final ResourceInformationReflector resourceInformationReflector = new ResourceInformationReflector(ResourceWithMethod.class.getName(), ResourceInfoWithMethod.class.getName()); + final ResourceWithMethod resourceWithMethod = new ResourceWithMethod(); + resourceInformationReflector.setResourceInformationUnSafe(resourceWithMethod, RESOURCE_NAME, RESOURCE_VALUE); + + assertNotNull(resourceWithMethod.getResourceWithName(RESOURCE_NAME)); + assertThat(resourceWithMethod.getResourceWithName(RESOURCE_NAME).getName(), is(RESOURCE_NAME)); + assertThat(resourceWithMethod.getResourceWithName(RESOURCE_NAME).getValue(), is(RESOURCE_VALUE)); + } + + @Test + public void testGetResourceInformationIfMethodPresent() { + final ResourceInformationReflector resourceInformationReflector = new ResourceInformationReflector(ResourceWithMethod.class.getName(), ResourceInfoWithMethod.class.getName()); + final ResourceWithMethod resourceWithMethod = new ResourceWithMethod(); + resourceWithMethod.setResourceInformation(RESOURCE_NAME, ResourceInfoWithMethod.newInstance(RESOURCE_NAME, RESOURCE_VALUE)); + + final Map<String, Long> externalResources = resourceInformationReflector.getExternalResourcesUnSafe(resourceWithMethod); + assertThat(externalResources.size(), is(1)); + assertTrue(externalResources.containsKey(RESOURCE_NAME)); + assertThat(externalResources.get(RESOURCE_NAME), is(RESOURCE_VALUE)); + } + + @Test + public void testSetResourceInformationIfMethodAbsent() { + final ResourceInformationReflector resourceInformationReflector = new ResourceInformationReflector(ResourceWithoutMethod.class.getName(), ResourceInfoWithMethod.class.getName()); + final ResourceWithMethod resourceWithMethod = new ResourceWithMethod(); + resourceInformationReflector.setResourceInformationUnSafe(resourceWithMethod, RESOURCE_NAME, RESOURCE_VALUE); + + assertNull(resourceWithMethod.getResourceWithName(RESOURCE_NAME)); + } + + @Test + public void testGetResourceInformationIfMethodAbsent() { + final ResourceInformationReflector resourceInformationReflector = new ResourceInformationReflector(ResourceWithoutMethod.class.getName(), ResourceInfoWithMethod.class.getName()); + final ResourceWithMethod resourceWithMethod = new ResourceWithMethod(); + resourceWithMethod.setResourceInformation(RESOURCE_NAME, ResourceInfoWithMethod.newInstance(RESOURCE_NAME, RESOURCE_VALUE)); + + final Map<String, Long> externalResources = resourceInformationReflector.getExternalResourcesUnSafe(resourceWithMethod); + assertThat(externalResources.size(), is(0)); Review comment: If so, it should be `assertThat(externalResources.keySet(), is(empty()));`. For me, I prefer the current one. WDYT? ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org