|
18 | 18 |
|
19 | 19 | import com.cloud.agent.Listener; |
20 | 20 | import com.cloud.agent.api.Answer; |
| 21 | +import com.cloud.agent.api.Command; |
21 | 22 | import com.cloud.agent.api.ReadyAnswer; |
22 | 23 | import com.cloud.agent.api.ReadyCommand; |
23 | 24 | import com.cloud.agent.api.StartupCommand; |
@@ -172,4 +173,84 @@ public void testGetHostSshPortWithKVMHostCustomPort() { |
172 | 173 | int hostSshPort = mgr.getHostSshPort(host); |
173 | 174 | Assert.assertEquals(3922, hostSshPort); |
174 | 175 | } |
| 176 | + |
| 177 | + /* |
| 178 | + * When the first registered listener throws a RuntimeException, |
| 179 | + * handleCommands must catch it, log a warning, and continue so that |
| 180 | + * the second listener still runs. |
| 181 | + */ |
| 182 | + @Test |
| 183 | + public void testHandleCommandsListenerExceptionDoesNotAbortLoop() throws Exception { |
| 184 | + Listener throwingListener = Mockito.mock(Listener.class); |
| 185 | + Mockito.when(throwingListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 186 | + .thenThrow(new RuntimeException("simulated NPE from power-state sync")); |
| 187 | + |
| 188 | + Listener goodListener = Mockito.mock(Listener.class); |
| 189 | + Mockito.when(goodListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 190 | + .thenReturn(true); |
| 191 | + |
| 192 | + mgr._cmdMonitors = new ArrayList<>(); |
| 193 | + mgr._cmdMonitors.add(new Pair<>(1, throwingListener)); |
| 194 | + mgr._cmdMonitors.add(new Pair<>(2, goodListener)); |
| 195 | + |
| 196 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 197 | + |
| 198 | + // Must not throw; the exception from throwingListener must be swallowed. |
| 199 | + mgr.handleCommands(attache, 1L, cmds); |
| 200 | + |
| 201 | + // The second listener must still have been invoked. |
| 202 | + Mockito.verify(goodListener, Mockito.times(1)) |
| 203 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 204 | + } |
| 205 | + |
| 206 | + @Test |
| 207 | + public void testHandleCommandsAllListenersSucceed() throws Exception { |
| 208 | + Listener listenerA = Mockito.mock(Listener.class); |
| 209 | + Mockito.when(listenerA.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 210 | + .thenReturn(true); |
| 211 | + |
| 212 | + Listener listenerB = Mockito.mock(Listener.class); |
| 213 | + Mockito.when(listenerB.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 214 | + .thenReturn(false); |
| 215 | + |
| 216 | + mgr._cmdMonitors = new ArrayList<>(); |
| 217 | + mgr._cmdMonitors.add(new Pair<>(1, listenerA)); |
| 218 | + mgr._cmdMonitors.add(new Pair<>(2, listenerB)); |
| 219 | + |
| 220 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 221 | + mgr.handleCommands(attache, 1L, cmds); |
| 222 | + |
| 223 | + Mockito.verify(listenerA, Mockito.times(1)) |
| 224 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 225 | + Mockito.verify(listenerB, Mockito.times(1)) |
| 226 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 227 | + } |
| 228 | + |
| 229 | + /* |
| 230 | + * Simulates the reported failure: a power-state sync listener throws |
| 231 | + * partway through, and a downstream ping-style listener must still run |
| 232 | + * so pingBy() isn't starved. |
| 233 | + */ |
| 234 | + @Test |
| 235 | + public void testHandleCommandsThrowingListenerDoesNotStarveDownstreamListener() throws Exception { |
| 236 | + Listener powerStateSyncListener = Mockito.mock(Listener.class); |
| 237 | + Mockito.when(powerStateSyncListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 238 | + .thenThrow(new NullPointerException("hostId was null in isPowerStateInSyncWithInstanceState")); |
| 239 | + |
| 240 | + Listener pingListener = Mockito.mock(Listener.class); |
| 241 | + Mockito.when(pingListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 242 | + .thenReturn(false); |
| 243 | + |
| 244 | + mgr._cmdMonitors = new ArrayList<>(); |
| 245 | + mgr._cmdMonitors.add(new Pair<>(1, powerStateSyncListener)); |
| 246 | + mgr._cmdMonitors.add(new Pair<>(2, pingListener)); |
| 247 | + |
| 248 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 249 | + |
| 250 | + // Before the fix this would abort on the NPE and pingListener would never run. |
| 251 | + mgr.handleCommands(attache, 42L, cmds); |
| 252 | + |
| 253 | + Mockito.verify(pingListener, Mockito.times(1)) |
| 254 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(42L), Mockito.eq(cmds)); |
| 255 | + } |
175 | 256 | } |
0 commit comments