|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.gobblin.temporal.yarn; |
| 19 | + |
| 20 | +import java.io.FileNotFoundException; |
| 21 | +import java.io.IOException; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.mockito.Mock; |
| 27 | +import org.mockito.Mockito; |
| 28 | +import org.mockito.MockitoAnnotations; |
| 29 | +import org.testng.annotations.BeforeMethod; |
| 30 | +import org.testng.annotations.Test; |
| 31 | + |
| 32 | +import com.typesafe.config.Config; |
| 33 | +import com.typesafe.config.ConfigFactory; |
| 34 | +import com.typesafe.config.ConfigValueFactory; |
| 35 | + |
| 36 | +import org.apache.gobblin.temporal.GobblinTemporalConfigurationKeys; |
| 37 | +import org.apache.gobblin.temporal.dynamic.ScalingDirective; |
| 38 | +import org.apache.gobblin.temporal.dynamic.ScalingDirectiveSource; |
| 39 | +import org.apache.gobblin.temporal.dynamic.DummyScalingDirectiveSource; |
| 40 | + |
| 41 | +/** Tests for {@link AbstractDynamicScalingYarnServiceManager}*/ |
| 42 | +public class DynamicScalingYarnServiceManagerTest { |
| 43 | + |
| 44 | + @Mock private DynamicScalingYarnService mockDynamicScalingYarnService; |
| 45 | + @Mock private ScalingDirectiveSource mockScalingDirectiveSource; |
| 46 | + @Mock private GobblinTemporalApplicationMaster mockGobblinTemporalApplicationMaster; |
| 47 | + |
| 48 | + @BeforeMethod |
| 49 | + public void setup() { |
| 50 | + MockitoAnnotations.openMocks(this); |
| 51 | + // Using 1 second as polling interval so that the test runs faster and |
| 52 | + // GetScalingDirectivesRunnable.run() will be called equal to amount of sleep introduced between startUp |
| 53 | + // and shutDown in seconds |
| 54 | + Config config = ConfigFactory.empty().withValue(GobblinTemporalConfigurationKeys.DYNAMIC_SCALING_POLLING_INTERVAL_SECS, ConfigValueFactory.fromAnyRef(1)); |
| 55 | + Mockito.when(mockGobblinTemporalApplicationMaster.getConfig()).thenReturn(config); |
| 56 | + Mockito.when(mockGobblinTemporalApplicationMaster.get_yarnService()).thenReturn(mockDynamicScalingYarnService); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testWhenScalingDirectivesIsNulOrEmpty() throws IOException, InterruptedException { |
| 61 | + Mockito.when(mockScalingDirectiveSource.getScalingDirectives()).thenReturn(null).thenReturn(new ArrayList<>()); |
| 62 | + TestDynamicScalingYarnServiceManager testDynamicScalingYarnServiceManager = new TestDynamicScalingYarnServiceManager( |
| 63 | + mockGobblinTemporalApplicationMaster, mockScalingDirectiveSource); |
| 64 | + testDynamicScalingYarnServiceManager.startUp(); |
| 65 | + Thread.sleep(3000); |
| 66 | + testDynamicScalingYarnServiceManager.shutDown(); |
| 67 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.never()).reviseWorkforcePlanAndRequestNewContainers(Mockito.anyList()); |
| 68 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(3)).calcDeltasAndRequestContainers(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testWhenScalingDirectivesThrowsFNFE() throws IOException, InterruptedException { |
| 73 | + Mockito.when(mockScalingDirectiveSource.getScalingDirectives()).thenThrow(FileNotFoundException.class); |
| 74 | + TestDynamicScalingYarnServiceManager testDynamicScalingYarnServiceManager = new TestDynamicScalingYarnServiceManager( |
| 75 | + mockGobblinTemporalApplicationMaster, mockScalingDirectiveSource); |
| 76 | + testDynamicScalingYarnServiceManager.startUp(); |
| 77 | + Thread.sleep(2000); |
| 78 | + testDynamicScalingYarnServiceManager.shutDown(); |
| 79 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.never()).reviseWorkforcePlanAndRequestNewContainers(Mockito.anyList()); |
| 80 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(2)).calcDeltasAndRequestContainers(); |
| 81 | + } |
| 82 | + |
| 83 | + /** Note : this test uses {@link DummyScalingDirectiveSource}*/ |
| 84 | + @Test |
| 85 | + public void testWithDummyScalingDirectiveSource() throws IOException, InterruptedException { |
| 86 | + // DummyScalingDirectiveSource returns 2 scaling directives in first 5 invocations and after that it returns empty list |
| 87 | + // so the total number of invocations after five invocations should always be 5 |
| 88 | + TestDynamicScalingYarnServiceManager testDynamicScalingYarnServiceManager = new TestDynamicScalingYarnServiceManager( |
| 89 | + mockGobblinTemporalApplicationMaster, new DummyScalingDirectiveSource()); |
| 90 | + testDynamicScalingYarnServiceManager.startUp(); |
| 91 | + Thread.sleep(7000); // 7 seconds sleep so that GetScalingDirectivesRunnable.run() is called for 7 times |
| 92 | + testDynamicScalingYarnServiceManager.shutDown(); |
| 93 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(5)).reviseWorkforcePlanAndRequestNewContainers(Mockito.anyList()); |
| 94 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(2)).calcDeltasAndRequestContainers(); |
| 95 | + } |
| 96 | + |
| 97 | + @Test |
| 98 | + public void testWithRandomScalingDirectives() throws IOException, InterruptedException { |
| 99 | + ScalingDirective mockScalingDirective = Mockito.mock(ScalingDirective.class); |
| 100 | + List<ScalingDirective> mockedScalingDirectives = Arrays.asList(mockScalingDirective, mockScalingDirective); |
| 101 | + Mockito.when(mockScalingDirectiveSource.getScalingDirectives()) |
| 102 | + .thenReturn(new ArrayList<>()) |
| 103 | + .thenReturn(mockedScalingDirectives) |
| 104 | + .thenReturn(mockedScalingDirectives) |
| 105 | + .thenReturn(null); |
| 106 | + |
| 107 | + TestDynamicScalingYarnServiceManager testDynamicScalingYarnServiceManager = new TestDynamicScalingYarnServiceManager( |
| 108 | + mockGobblinTemporalApplicationMaster, mockScalingDirectiveSource); |
| 109 | + testDynamicScalingYarnServiceManager.startUp(); |
| 110 | + Thread.sleep(5000); |
| 111 | + testDynamicScalingYarnServiceManager.shutDown(); |
| 112 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(2)).reviseWorkforcePlanAndRequestNewContainers(Mockito.anyList()); |
| 113 | + Mockito.verify(mockDynamicScalingYarnService, Mockito.times(3)).calcDeltasAndRequestContainers(); |
| 114 | + } |
| 115 | + |
| 116 | + /** Test implementation of {@link AbstractDynamicScalingYarnServiceManager} which returns passed |
| 117 | + * {@link ScalingDirectiveSource} when {@link #createScalingDirectiveSource()} is called while initialising |
| 118 | + * {@link AbstractDynamicScalingYarnServiceManager.GetScalingDirectivesRunnable} |
| 119 | + * */ |
| 120 | + protected static class TestDynamicScalingYarnServiceManager extends AbstractDynamicScalingYarnServiceManager { |
| 121 | + private final ScalingDirectiveSource _scalingDirectiveSource; |
| 122 | + public TestDynamicScalingYarnServiceManager(GobblinTemporalApplicationMaster appMaster, ScalingDirectiveSource scalingDirectiveSource) { |
| 123 | + super(appMaster); |
| 124 | + this._scalingDirectiveSource = scalingDirectiveSource; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + protected ScalingDirectiveSource createScalingDirectiveSource() { |
| 129 | + return this._scalingDirectiveSource; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | +} |
0 commit comments