-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring changes proposed on STORM-3693 and adding an unit test
- Loading branch information
Showing
3 changed files
with
110 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
storm-client/test/jvm/org/apache/storm/executor/SpoutExecutorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package org.apache.storm.executor; | ||
|
||
import org.apache.storm.Constants; | ||
import org.apache.storm.cluster.IStateStorage; | ||
import org.apache.storm.daemon.worker.WorkerState; | ||
import org.apache.storm.executor.spout.SpoutExecutor; | ||
import org.apache.storm.generated.ComponentCommon; | ||
import org.apache.storm.metrics2.RateCounter; | ||
import org.apache.storm.metrics2.StormMetricRegistry; | ||
import org.apache.storm.task.WorkerTopologyContext; | ||
import org.apache.storm.tuple.AddressedTuple; | ||
import org.apache.storm.tuple.TupleImpl; | ||
import org.apache.storm.utils.RotatingMap; | ||
import org.apache.storm.utils.Utils; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.commons.util.ReflectionUtils; | ||
import org.mockito.Mockito; | ||
|
||
import java.lang.reflect.Field; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
|
||
public class SpoutExecutorTest { | ||
|
||
|
||
@Test | ||
public void testPendingTuplesRotateShouldBeCalledOnlyOnce() throws Exception { | ||
|
||
RateCounter rateCounter = Mockito.mock(RateCounter.class); | ||
|
||
StormMetricRegistry stormMetricRegistry = Mockito.mock(StormMetricRegistry.class); | ||
Mockito.when(stormMetricRegistry.rateCounter(anyString(),anyString(),anyInt())).thenReturn(rateCounter); | ||
|
||
Map<String,Object> hashmap = Utils.readDefaultConfig(); | ||
|
||
IStateStorage stateStorage = Mockito.mock(IStateStorage.class); | ||
|
||
ComponentCommon componentCommon = Mockito.mock(ComponentCommon.class); | ||
Mockito.when(componentCommon.get_json_conf()).thenReturn(null); | ||
|
||
WorkerTopologyContext workerTopologyContext = Mockito.mock(WorkerTopologyContext.class); | ||
Mockito.when(workerTopologyContext.getComponentId(anyInt())).thenReturn("1"); | ||
Mockito.when(workerTopologyContext.getComponentCommon(anyString())).thenReturn(componentCommon); | ||
|
||
WorkerState workerState = Mockito.mock(WorkerState.class); | ||
Mockito.when(workerState.getWorkerTopologyContext()).thenReturn(workerTopologyContext); | ||
Mockito.when(workerState.getStateStorage()).thenReturn(stateStorage); | ||
Mockito.when(workerState.getTopologyConf()).thenReturn(hashmap); | ||
Mockito.when(workerState.getMetricRegistry()).thenReturn(stormMetricRegistry); | ||
|
||
SpoutExecutor spoutExecutor = new SpoutExecutor(workerState,List.of(1L,5L),new HashMap<>()); | ||
|
||
TupleImpl tuple = Mockito.mock(TupleImpl.class); | ||
Mockito.when(tuple.getSourceStreamId()).thenReturn(Constants.SYSTEM_TICK_STREAM_ID); | ||
AddressedTuple addressedTuple = Mockito.mock(AddressedTuple.class); | ||
Mockito.when(addressedTuple.getDest()).thenReturn(AddressedTuple.BROADCAST_DEST); | ||
Mockito.when(addressedTuple.getTuple()).thenReturn(tuple); | ||
|
||
RotatingMap rotatingMap = Mockito.mock(RotatingMap.class); | ||
Field fieldRotatingMap = ReflectionUtils | ||
.findFields(SpoutExecutor.class, f -> f.getName().equals("pending"), | ||
ReflectionUtils.HierarchyTraversalMode.TOP_DOWN) | ||
.get(0); | ||
fieldRotatingMap.setAccessible(true); | ||
fieldRotatingMap.set(spoutExecutor, rotatingMap); | ||
|
||
spoutExecutor.accept(addressedTuple); | ||
|
||
Mockito.verify(rotatingMap,Mockito.times(1)).rotate(); | ||
} | ||
} |