|
31 | 31 | import com.cloud.host.dao.HostDetailsDao; |
32 | 32 | import com.cloud.hypervisor.Hypervisor; |
33 | 33 | import com.cloud.utils.Pair; |
| 34 | +import org.apache.cloudstack.framework.jobs.AsyncJobManager; |
| 35 | +import org.apache.cloudstack.framework.jobs.impl.AsyncJobVO; |
34 | 36 | import org.junit.Assert; |
35 | 37 | import org.junit.Before; |
36 | 38 | import org.junit.Test; |
37 | 39 | import org.mockito.Mockito; |
38 | 40 |
|
39 | 41 | import java.util.ArrayList; |
| 42 | +import java.util.Collections; |
40 | 43 | import java.util.HashMap; |
| 44 | +import java.util.HashSet; |
41 | 45 | import java.util.Map; |
42 | 46 |
|
43 | 47 | public class AgentManagerImplTest { |
@@ -172,4 +176,41 @@ public void testGetHostSshPortWithKVMHostCustomPort() { |
172 | 176 | int hostSshPort = mgr.getHostSshPort(host); |
173 | 177 | Assert.assertEquals(3922, hostSshPort); |
174 | 178 | } |
| 179 | + |
| 180 | + private AsyncJobManager cancelledJobOnThisServer(final long jobId) { |
| 181 | + final AsyncJobManager asyncJobManager = Mockito.mock(AsyncJobManager.class); |
| 182 | + mgr.asyncJobManager = asyncJobManager; |
| 183 | + final AsyncJobVO job = new AsyncJobVO(); |
| 184 | + job.setId(jobId); |
| 185 | + Mockito.when(asyncJobManager.listCancelledJobsExecutingOn(mgr._nodeId)).thenReturn(Collections.singletonList(job)); |
| 186 | + return asyncJobManager; |
| 187 | + } |
| 188 | + |
| 189 | + @Test |
| 190 | + public void testCancelledJobWithoutInFlightCommandsIsFinalisedAtOnce() { |
| 191 | + final AsyncJobManager asyncJobManager = cancelledJobOnThisServer(42L); |
| 192 | + |
| 193 | + mgr.new CancelledJobsCheckTask().runInContext(); |
| 194 | + |
| 195 | + Mockito.verify(asyncJobManager).finalizeCancelledJob(42L); |
| 196 | + Assert.assertTrue(mgr.isJobCancelled(42L)); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testCancelledJobStaysAssignedUntilItsCommandCanBeStopped() throws Exception { |
| 201 | + final AsyncJobManager asyncJobManager = cancelledJobOnThisServer(42L); |
| 202 | + final AgentAttache attache = Mockito.mock(AgentAttache.class); |
| 203 | + Mockito.doReturn(attache).when(mgr).getAttache(1L); |
| 204 | + mgr._jobToHostIdAndReqSequenceMap.put(42L, new HashSet<>(Collections.singleton(new Pair<>(1L, 11L)))); |
| 205 | + |
| 206 | + Mockito.when(attache.isExecutionCancellable(11L)).thenReturn(false); |
| 207 | + mgr.new CancelledJobsCheckTask().runInContext(); |
| 208 | + Mockito.verify(asyncJobManager, Mockito.never()).finalizeCancelledJob(Mockito.anyLong()); |
| 209 | + Mockito.verify(attache, Mockito.never()).cancelExecution(Mockito.anyLong()); |
| 210 | + |
| 211 | + Mockito.when(attache.isExecutionCancellable(11L)).thenReturn(true); |
| 212 | + Mockito.when(attache.cancelExecution(11L)).thenReturn(true); |
| 213 | + mgr.new CancelledJobsCheckTask().runInContext(); |
| 214 | + Mockito.verify(asyncJobManager).finalizeCancelledJob(42L); |
| 215 | + } |
175 | 216 | } |
0 commit comments