|
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.ReadyCommand; |
22 | 23 | import com.cloud.agent.api.StartupCommand; |
23 | 24 | import com.cloud.agent.api.StartupRoutingCommand; |
@@ -137,4 +138,84 @@ public void testGetHostSshPortWithKVMHostCustomPort() { |
137 | 138 | int hostSshPort = mgr.getHostSshPort(host); |
138 | 139 | Assert.assertEquals(3922, hostSshPort); |
139 | 140 | } |
| 141 | + |
| 142 | + /* |
| 143 | + * When the first registered listener throws a RuntimeException, |
| 144 | + * handleCommands must catch it, log a warning, and continue so that |
| 145 | + * the second listener still runs. |
| 146 | + */ |
| 147 | + @Test |
| 148 | + public void testHandleCommandsListenerExceptionDoesNotAbortLoop() throws Exception { |
| 149 | + Listener throwingListener = Mockito.mock(Listener.class); |
| 150 | + Mockito.when(throwingListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 151 | + .thenThrow(new RuntimeException("simulated NPE from power-state sync")); |
| 152 | + |
| 153 | + Listener goodListener = Mockito.mock(Listener.class); |
| 154 | + Mockito.when(goodListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 155 | + .thenReturn(true); |
| 156 | + |
| 157 | + mgr._cmdMonitors = new ArrayList<>(); |
| 158 | + mgr._cmdMonitors.add(new Pair<>(1, throwingListener)); |
| 159 | + mgr._cmdMonitors.add(new Pair<>(2, goodListener)); |
| 160 | + |
| 161 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 162 | + |
| 163 | + // Must not throw; the exception from throwingListener must be swallowed. |
| 164 | + mgr.handleCommands(attache, 1L, cmds); |
| 165 | + |
| 166 | + // The second listener must still have been invoked. |
| 167 | + Mockito.verify(goodListener, Mockito.times(1)) |
| 168 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testHandleCommandsAllListenersSucceed() throws Exception { |
| 173 | + Listener listenerA = Mockito.mock(Listener.class); |
| 174 | + Mockito.when(listenerA.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 175 | + .thenReturn(true); |
| 176 | + |
| 177 | + Listener listenerB = Mockito.mock(Listener.class); |
| 178 | + Mockito.when(listenerB.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 179 | + .thenReturn(false); |
| 180 | + |
| 181 | + mgr._cmdMonitors = new ArrayList<>(); |
| 182 | + mgr._cmdMonitors.add(new Pair<>(1, listenerA)); |
| 183 | + mgr._cmdMonitors.add(new Pair<>(2, listenerB)); |
| 184 | + |
| 185 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 186 | + mgr.handleCommands(attache, 1L, cmds); |
| 187 | + |
| 188 | + Mockito.verify(listenerA, Mockito.times(1)) |
| 189 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 190 | + Mockito.verify(listenerB, Mockito.times(1)) |
| 191 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(1L), Mockito.eq(cmds)); |
| 192 | + } |
| 193 | + |
| 194 | + /* |
| 195 | + * Simulates the reported failure: a power-state sync listener throws |
| 196 | + * partway through, and a downstream ping-style listener must still run |
| 197 | + * so pingBy() isn't starved. |
| 198 | + */ |
| 199 | + @Test |
| 200 | + public void testHandleCommandsThrowingListenerDoesNotStarveDownstreamListener() throws Exception { |
| 201 | + Listener powerStateSyncListener = Mockito.mock(Listener.class); |
| 202 | + Mockito.when(powerStateSyncListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 203 | + .thenThrow(new NullPointerException("hostId was null in isPowerStateInSyncWithInstanceState")); |
| 204 | + |
| 205 | + Listener pingListener = Mockito.mock(Listener.class); |
| 206 | + Mockito.when(pingListener.processCommands(Mockito.anyLong(), Mockito.anyLong(), Mockito.any())) |
| 207 | + .thenReturn(false); |
| 208 | + |
| 209 | + mgr._cmdMonitors = new ArrayList<>(); |
| 210 | + mgr._cmdMonitors.add(new Pair<>(1, powerStateSyncListener)); |
| 211 | + mgr._cmdMonitors.add(new Pair<>(2, pingListener)); |
| 212 | + |
| 213 | + Command[] cmds = new Command[]{Mockito.mock(Command.class)}; |
| 214 | + |
| 215 | + // Before the fix this would abort on the NPE and pingListener would never run. |
| 216 | + mgr.handleCommands(attache, 42L, cmds); |
| 217 | + |
| 218 | + Mockito.verify(pingListener, Mockito.times(1)) |
| 219 | + .processCommands(Mockito.eq(attache.getId()), Mockito.eq(42L), Mockito.eq(cmds)); |
| 220 | + } |
140 | 221 | } |
0 commit comments