import org.apache.commons.io.FileUtils; //导入方法依赖的package包/类
@Test
public void should_execute_sync_event_callback() throws Throwable {
// given: copy exist git to workspace
ClassLoader classLoader = TestBase.class.getClassLoader();
URL resource = classLoader.getResource("hello.git");
File path = new File(resource.getFile());
FileUtils.copyDirectoryToDirectory(path, gitWorkspace.toFile());
// and: register agent to sync service
AgentPath agent = agents.get(0);
syncService.load();
syncService.register(agent);
Assert.assertEquals(1, syncService.get(agent).queueSize());
// when: execute sync task
syncService.syncTask();
// then: the create session cmd should be send
CountMatchingStrategy strategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 1);
verify(strategy, postRequestedFor(urlEqualTo("/cmd/queue/send?priority=10&retry=5")));
// when: mock create session cmd been callback
Cmd mockSessionCallback = new Cmd(agent.getZone(), agent.getName(), CmdType.CREATE_SESSION, null);
mockSessionCallback.setStatus(CmdStatus.SENT);
mockSessionCallback.setSessionId(createSessionCmdResponse.getSessionId());
syncService.onCallback(mockSessionCallback);
// then: send run shell cmd for sync event and sync task queue size not changed
strategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 1);
verify(strategy, postRequestedFor(urlEqualTo("/cmd/send")));
Assert.assertEquals(2, syncService.getSyncTask(agent).getSyncQueue().size());
// when: mock cmd been executed
Cmd mockRunShellSuccess = new Cmd(agent.getZone(), agent.getName(), CmdType.RUN_SHELL, "git pull xxx");
mockRunShellSuccess.setSessionId(mockSessionCallback.getSessionId());
mockRunShellSuccess.setStatus(CmdStatus.LOGGED);
mockRunShellSuccess.setCmdResult(new CmdResult(0));
syncService.onCallback(mockRunShellSuccess);
// then: should send list repos cmd and sync task queue size should be 1
strategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 2);
verify(strategy, postRequestedFor(urlEqualTo("/cmd/send")));
Assert.assertEquals(1, syncService.getSyncTask(agent).getSyncQueue().size());
// when: mock list cmd been executed
Cmd mockListRepoSuccess =
new Cmd(agent.getZone(), agent.getName(), CmdType.RUN_SHELL, "export FLOW_SYNC_LIST=$(ls)");
mockListRepoSuccess.setSessionId(mockSessionCallback.getSessionId());
mockListRepoSuccess.setStatus(CmdStatus.LOGGED);
mockListRepoSuccess.setCmdResult(new CmdResult(0));
mockListRepoSuccess.getCmdResult().getOutput().put(SyncEvent.FLOW_SYNC_LIST, "A[v1]\nB[v2]");
syncService.onCallback(mockListRepoSuccess);
// then: agent repo list size should be 2
Assert.assertEquals(2, syncService.get(agent).getRepos().size());
Assert.assertTrue(syncService.get(agent).getRepos().contains(new SyncRepo("A", "v1")));
Assert.assertTrue(syncService.get(agent).getRepos().contains(new SyncRepo("B", "v2")));
// then: should send delete session cmd and sync task queue size should be zero
strategy = new CountMatchingStrategy(CountMatchingStrategy.EQUAL_TO, 3);
verify(strategy, postRequestedFor(urlEqualTo("/cmd/send")));
Assert.assertEquals(0, syncService.getSyncTask(agent).getSyncQueue().size());
// when: mock delete session cmd
Cmd mockDeleteSession = new Cmd(agent.getZone(), agent.getName(), CmdType.DELETE_SESSION, null);
mockDeleteSession.setSessionId(mockRunShellSuccess.getSessionId());
mockDeleteSession.setStatus(CmdStatus.SENT);
syncService.onCallback(mockDeleteSession);
// then: sync task of agent should be deleted
Assert.assertNull(syncService.getSyncTask(agent));
}