|
1 | | -from hashtopolis import AgentAssignment |
| 1 | +import time |
| 2 | +from hashtopolis import AgentAssignment, Chunk |
2 | 3 |
|
3 | | -from utils import BaseTest, do_create_dummy_agent |
| 4 | +from utils import BaseTest, do_create_dummy_agent, do_create_agentassignent |
4 | 5 |
|
5 | 6 |
|
6 | 7 | class AgentStatTest(BaseTest): |
@@ -50,3 +51,33 @@ def test_agent_assign_task(self): |
50 | 51 | def test_acl(self): |
51 | 52 | model_obj = self.create_test_object() |
52 | 53 | self._test_acl_list(model_obj, {'permAgentAssignmentRead': True}) |
| 54 | + |
| 55 | + def test_cracking_time_aggregation(self): |
| 56 | + dummy_agent, agent, _, task = self.create_agent_with_task().values() |
| 57 | + time.sleep(1) # Simulate cracking time |
| 58 | + dummy_agent.send_process(progress=100) |
| 59 | + dummy_agent.get_chunk() |
| 60 | + time.sleep(1) # Simulate cracking time |
| 61 | + dummy_agent.send_process(progress=100) |
| 62 | + dummy_agent.get_chunk() |
| 63 | + time.sleep(1) # Simulate cracking time |
| 64 | + dummy_agent.send_process(progress=100) |
| 65 | + dummy_agent.get_chunk() # Leave the last chunk unfinished for a more meaningful test |
| 66 | + |
| 67 | + # Calculate a reference value for the cracking time by applying an interval merge algorithm |
| 68 | + chunks = Chunk.objects.filter(agentId=agent.id, taskId=task.id) |
| 69 | + totalEnd = totalSum = 0 |
| 70 | + for chunk in sorted(chunks, key=lambda c: c.dispatchTime): # Expects list to be sorted by time |
| 71 | + if chunk.dispatchTime > 0 and chunk.solveTime > 0: |
| 72 | + totalSum = ( |
| 73 | + totalSum + (chunk.solveTime - chunk.dispatchTime) |
| 74 | + - max(0, totalEnd - chunk.dispatchTime) |
| 75 | + + max(0, totalEnd - chunk.solveTime) |
| 76 | + ) |
| 77 | + totalEnd = max(totalEnd, chunk.solveTime) |
| 78 | + |
| 79 | + # Get the aggregate cracking time from the application |
| 80 | + aggregate_attrs = ['crackingTime'] |
| 81 | + assignment = AgentAssignment.objects.params(**{"aggregate[assignment]": ','.join(aggregate_attrs)}).get(agentId=agent.id, taskId=task.id) |
| 82 | + |
| 83 | + self.assertEqual(totalSum, assignment.crackingTime) |
0 commit comments