Skip to content

Commit bdc3a13

Browse files
committed
add tests for method
1 parent 2602253 commit bdc3a13

File tree

1 file changed

+89
-1
lines changed

1 file changed

+89
-1
lines changed

storm-server/src/test/java/org/apache/storm/daemon/nimbus/NimbusTest.java

+89-1
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,75 @@
2525

2626
import org.apache.storm.Config;
2727
import org.apache.storm.DaemonConfig;
28+
import org.apache.storm.blobstore.BlobStore;
29+
import org.apache.storm.blobstore.KeySequenceNumber;
30+
import org.apache.storm.blobstore.LocalFsBlobStore;
31+
import org.apache.storm.cluster.IStormClusterState;
2832
import org.apache.storm.generated.InvalidTopologyException;
33+
import org.apache.storm.generated.KeyNotFoundException;
2934
import org.apache.storm.generated.StormTopology;
35+
import org.apache.storm.metric.StormMetricsRegistry;
36+
import org.apache.storm.nimbus.ILeaderElector;
37+
import org.apache.storm.nimbus.NimbusInfo;
38+
import org.apache.storm.scheduler.INimbus;
3039
import org.apache.storm.scheduler.resource.strategies.priority.DefaultSchedulingPriorityStrategy;
3140
import org.apache.storm.scheduler.resource.strategies.scheduling.DefaultResourceAwareStrategy;
3241
import org.apache.storm.scheduler.resource.strategies.scheduling.GenericResourceAwareStrategyOld;
3342
import org.apache.storm.scheduler.resource.strategies.scheduling.RoundRobinResourceAwareStrategy;
43+
import org.apache.storm.security.auth.IGroupMappingServiceProvider;
3444
import org.apache.storm.testing.TestWordSpout;
45+
import org.apache.storm.thrift.TException;
3546
import org.apache.storm.topology.TopologyBuilder;
3647
import org.apache.storm.utils.ServerUtils;
3748
import org.apache.storm.utils.Time;
49+
import org.junit.jupiter.api.BeforeEach;
3850
import org.junit.jupiter.api.Test;
51+
import org.mockito.Mock;
52+
import org.mockito.MockedConstruction;
53+
import org.mockito.MockitoAnnotations;
3954

4055
import static org.junit.jupiter.api.Assertions.assertEquals;
56+
import static org.junit.jupiter.api.Assertions.assertThrows;
4157
import static org.junit.jupiter.api.Assertions.assertTrue;
4258
import static org.junit.jupiter.api.Assertions.fail;
4359
import static org.junit.jupiter.api.Assertions.assertNull;
60+
import static org.mockito.ArgumentMatchers.any;
61+
import static org.mockito.ArgumentMatchers.eq;
62+
import static org.mockito.Mockito.doThrow;
63+
import static org.mockito.Mockito.mock;
64+
import static org.mockito.Mockito.mockConstruction;
65+
import static org.mockito.Mockito.never;
66+
import static org.mockito.Mockito.verify;
67+
import static org.mockito.Mockito.when;
68+
69+
class NimbusTest {
70+
private static final String BLOB_FILE_KEY = "file-key";
71+
72+
@Mock
73+
private StormMetricsRegistry metricRegistry;
74+
@Mock
75+
private INimbus iNimbus;
76+
@Mock
77+
private IStormClusterState stormClusterState;
78+
@Mock
79+
private NimbusInfo nimbusInfo;
80+
@Mock
81+
private LocalFsBlobStore localBlobStore;
82+
@Mock
83+
private ILeaderElector leaderElector;
84+
@Mock
85+
private IGroupMappingServiceProvider groupMapper;
86+
87+
private Nimbus nimbus;
88+
89+
@BeforeEach
90+
public void setUp() throws Exception {
91+
MockitoAnnotations.openMocks(this).close();
92+
93+
Map<String, Object> conf = Map.of(DaemonConfig.NIMBUS_MONITOR_FREQ_SECS, 10);
94+
nimbus = new Nimbus(conf, iNimbus, stormClusterState, nimbusInfo, localBlobStore, leaderElector, groupMapper, metricRegistry);
95+
}
4496

45-
public class NimbusTest {
4697
@Test
4798
public void testMemoryLoadLargerThanMaxHeapSize() {
4899
// Topology will not be able to be successfully scheduled: Config TOPOLOGY_WORKER_MAX_HEAP_SIZE_MB=128.0 < 129.0,
@@ -112,4 +163,41 @@ public void validateNoTopoConfOverrides() {
112163
Map<String, Object> normalized = Nimbus.normalizeConf(conf, topoConf, topology);
113164
assertNull(normalized.get(Config.STORM_WORKERS_ARTIFACTS_DIR));
114165
}
166+
167+
@Test
168+
void testCreateStateInZookeeper() throws TException {
169+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
170+
171+
verify(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
172+
}
173+
174+
@Test
175+
void testCreateStateInZookeeperWithoutLocalFsBlobStoreInstanceShouldNotCreate() throws Exception {
176+
BlobStore blobStore = mock(BlobStore.class);
177+
Map<String, Object> conf = Map.of(DaemonConfig.NIMBUS_MONITOR_FREQ_SECS, 10);
178+
nimbus = new Nimbus(conf, iNimbus, stormClusterState, nimbusInfo, blobStore, leaderElector, groupMapper, metricRegistry);
179+
180+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
181+
182+
verify(stormClusterState, never()).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
183+
}
184+
185+
@Test
186+
void testCreateStateInZookeeperWhenFailToSetupBlobWithRuntimeExceptionThrowsRuntimeException() {
187+
doThrow(new RuntimeException("Failed to setup blob")).when(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
188+
189+
assertThrows(RuntimeException.class, () -> nimbus.createStateInZookeeper(BLOB_FILE_KEY));
190+
verify(stormClusterState).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
191+
}
192+
193+
@Test
194+
void testCreateStateInZookeeperWhenKeyNotFoundHandlesException() throws Exception {
195+
try (MockedConstruction<KeySequenceNumber> keySequenceNumber = mockConstruction(KeySequenceNumber.class, (mock, context) ->
196+
when(mock.getKeySequenceNumber(any())).thenThrow(new KeyNotFoundException("Failed to setup blob")))) {
197+
nimbus.createStateInZookeeper(BLOB_FILE_KEY);
198+
199+
verify(keySequenceNumber.constructed().get(0)).getKeySequenceNumber(any());
200+
verify(stormClusterState, never()).setupBlob(eq(BLOB_FILE_KEY), eq(nimbusInfo), any());
201+
}
202+
}
115203
}

0 commit comments

Comments
 (0)