1 /** 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 package org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair; 20 21 import static org.junit.Assert.assertFalse; 22 import static org.junit.Assert.assertTrue; 23 24 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.policies.DominantResourceFairnessPolicy; 25 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.policies.FairSharePolicy; 26 import org.apache.hadoop.yarn.server.resourcemanager.scheduler.fair.policies.FifoPolicy; 27 import org.junit.Test; 28 import org.mockito.Mockito; 29 30 public class TestSchedulingPolicy { 31 32 @Test(timeout = 1000) testParseSchedulingPolicy()33 public void testParseSchedulingPolicy() 34 throws AllocationConfigurationException { 35 36 // Class name 37 SchedulingPolicy sm = SchedulingPolicy 38 .parse(FairSharePolicy.class.getName()); 39 assertTrue("Invalid scheduler name", 40 sm.getName().equals(FairSharePolicy.NAME)); 41 42 // Canonical name 43 sm = SchedulingPolicy.parse(FairSharePolicy.class 44 .getCanonicalName()); 45 assertTrue("Invalid scheduler name", 46 sm.getName().equals(FairSharePolicy.NAME)); 47 48 // Class 49 sm = SchedulingPolicy.getInstance(FairSharePolicy.class); 50 assertTrue("Invalid scheduler name", 51 sm.getName().equals(FairSharePolicy.NAME)); 52 53 // Shortname - drf 54 sm = SchedulingPolicy.parse("drf"); 55 assertTrue("Invalid scheduler name", 56 sm.getName().equals(DominantResourceFairnessPolicy.NAME)); 57 58 // Shortname - fair 59 sm = SchedulingPolicy.parse("fair"); 60 assertTrue("Invalid scheduler name", 61 sm.getName().equals(FairSharePolicy.NAME)); 62 63 // Shortname - fifo 64 sm = SchedulingPolicy.parse("fifo"); 65 assertTrue("Invalid scheduler name", 66 sm.getName().equals(FifoPolicy.NAME)); 67 } 68 69 /** 70 * Trivial tests that make sure 71 * {@link SchedulingPolicy#isApplicableTo(SchedulingPolicy, byte)} works as 72 * expected for the possible values of depth 73 * 74 * @throws AllocationConfigurationException 75 */ 76 @Test(timeout = 1000) testIsApplicableTo()77 public void testIsApplicableTo() throws AllocationConfigurationException { 78 final String ERR = "Broken SchedulingPolicy#isApplicableTo"; 79 80 // fifo 81 SchedulingPolicy policy = SchedulingPolicy.parse("fifo"); 82 assertTrue(ERR, 83 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_LEAF)); 84 assertFalse(ERR, SchedulingPolicy.isApplicableTo( 85 SchedulingPolicy.parse("fifo"), SchedulingPolicy.DEPTH_INTERMEDIATE)); 86 assertFalse(ERR, SchedulingPolicy.isApplicableTo( 87 SchedulingPolicy.parse("fifo"), SchedulingPolicy.DEPTH_ROOT)); 88 89 90 // fair 91 policy = SchedulingPolicy.parse("fair"); 92 assertTrue(ERR, 93 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_LEAF)); 94 assertTrue(ERR, SchedulingPolicy.isApplicableTo(policy, 95 SchedulingPolicy.DEPTH_INTERMEDIATE)); 96 assertTrue(ERR, 97 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ROOT)); 98 assertTrue(ERR, 99 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_PARENT)); 100 assertTrue(ERR, 101 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ANY)); 102 103 // drf 104 policy = SchedulingPolicy.parse("drf"); 105 assertTrue(ERR, 106 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_LEAF)); 107 assertTrue(ERR, SchedulingPolicy.isApplicableTo(policy, 108 SchedulingPolicy.DEPTH_INTERMEDIATE)); 109 assertTrue(ERR, 110 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ROOT)); 111 assertTrue(ERR, 112 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_PARENT)); 113 assertTrue(ERR, 114 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ANY)); 115 116 policy = Mockito.mock(SchedulingPolicy.class); 117 Mockito.when(policy.getApplicableDepth()).thenReturn( 118 SchedulingPolicy.DEPTH_PARENT); 119 assertTrue(ERR, SchedulingPolicy.isApplicableTo(policy, 120 SchedulingPolicy.DEPTH_INTERMEDIATE)); 121 assertTrue(ERR, 122 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ROOT)); 123 assertTrue(ERR, 124 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_PARENT)); 125 assertFalse(ERR, 126 SchedulingPolicy.isApplicableTo(policy, SchedulingPolicy.DEPTH_ANY)); 127 } 128 } 129