|
32 | 32 | import java.util.Collections; |
33 | 33 | import java.util.Deque; |
34 | 34 | import java.util.List; |
35 | | - |
36 | 35 | import org.junit.Before; |
37 | 36 | import org.junit.Rule; |
38 | 37 | import org.junit.Test; |
|
42 | 41 |
|
43 | 42 | @RunWith(JUnit4.class) |
44 | 43 | public class TasksRunnerTest { |
45 | | - @Rule |
46 | | - public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); |
| 44 | + @Rule public final SystemOutRule systemOutRule = new SystemOutRule().enableLog(); |
47 | 45 |
|
48 | 46 | @Before |
49 | 47 | public void setUp() { |
50 | 48 | System.setProperty("org.slf4j.simpleLogger.logFile", "System.out"); |
51 | | - System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); } |
| 49 | + System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info"); |
| 50 | + } |
52 | 51 |
|
53 | 52 | @Test |
54 | 53 | public void testCreateContext_returnsValidTaskRunContext() throws IOException { |
@@ -89,131 +88,131 @@ public void testCreateContext_returnsValidTaskRunContext() throws IOException { |
89 | 88 | } |
90 | 89 | } |
91 | 90 |
|
92 | | -@Test |
93 | | -public void testGetTaskDuration_ReturnsMaxOfAllAndLatest() throws Exception { |
94 | | - OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
95 | | - Handle mockHandle = mock(Handle.class); |
96 | | - int threadPoolSize = 2; |
97 | | - TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
98 | | - ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
99 | | - List<Task<?>> tasks = Collections.nCopies(10, mock(Task.class)); |
100 | | - TasksRunner runner = |
101 | | - new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
102 | | - |
103 | | - java.lang.reflect.Field completedField = |
104 | | - TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
105 | | - completedField.setAccessible(true); |
106 | | - completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(5)); |
107 | | - |
108 | | - java.lang.reflect.Field stopwatchField = |
109 | | - TasksRunner.class.getDeclaredField("stopwatch"); |
110 | | - stopwatchField.setAccessible(true); |
111 | | - com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
112 | | - when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(50)); |
113 | | - stopwatchField.set(runner, mockStopwatch); |
114 | | - |
115 | | - java.lang.reflect.Field durationsField = |
116 | | - TasksRunner.class.getDeclaredField("lastTaskDurations"); |
117 | | - durationsField.setAccessible(true); |
118 | | - @SuppressWarnings("unchecked") |
119 | | - Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
120 | | - durations.clear(); |
121 | | - durations.add(Duration.ofSeconds(10)); |
122 | | - durations.add(Duration.ofSeconds(20)); |
123 | | - durations.add(Duration.ofSeconds(30)); |
124 | | - durations.add(Duration.ofSeconds(40)); |
125 | | - durations.add(Duration.ofSeconds(50)); |
126 | | - |
127 | | - // getAverageTaskDurationFromAllTasks = 50s / 5 = 10s |
128 | | - // getAverageTaskDurationFromLatestTasks = (50s - 10s) / 5 = 8s |
129 | | - // getTaskDuration should return max(10s, 8s) = 10s |
130 | | - java.lang.reflect.Method getTaskDuration = TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
131 | | - getTaskDuration.setAccessible(true); |
132 | | - Duration result = (Duration) getTaskDuration.invoke(runner); |
133 | | - |
134 | | - assertEquals(Duration.ofSeconds(10), result); |
135 | | -} |
| 91 | + @Test |
| 92 | + public void testGetTaskDuration_ReturnsMaxOfAllAndLatest() throws Exception { |
| 93 | + OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
| 94 | + Handle mockHandle = mock(Handle.class); |
| 95 | + int threadPoolSize = 2; |
| 96 | + TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
| 97 | + ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
| 98 | + List<Task<?>> tasks = Collections.nCopies(10, mock(Task.class)); |
| 99 | + TasksRunner runner = |
| 100 | + new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
| 101 | + |
| 102 | + java.lang.reflect.Field completedField = |
| 103 | + TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
| 104 | + completedField.setAccessible(true); |
| 105 | + completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(5)); |
| 106 | + |
| 107 | + java.lang.reflect.Field stopwatchField = TasksRunner.class.getDeclaredField("stopwatch"); |
| 108 | + stopwatchField.setAccessible(true); |
| 109 | + com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
| 110 | + when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(50)); |
| 111 | + stopwatchField.set(runner, mockStopwatch); |
| 112 | + |
| 113 | + java.lang.reflect.Field durationsField = |
| 114 | + TasksRunner.class.getDeclaredField("lastTaskDurations"); |
| 115 | + durationsField.setAccessible(true); |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
| 118 | + durations.clear(); |
| 119 | + durations.add(Duration.ofSeconds(10)); |
| 120 | + durations.add(Duration.ofSeconds(20)); |
| 121 | + durations.add(Duration.ofSeconds(30)); |
| 122 | + durations.add(Duration.ofSeconds(40)); |
| 123 | + durations.add(Duration.ofSeconds(50)); |
| 124 | + |
| 125 | + // getAverageTaskDurationFromAllTasks = 50s / 5 = 10s |
| 126 | + // getAverageTaskDurationFromLatestTasks = (50s - 10s) / 5 = 8s |
| 127 | + // getTaskDuration should return max(10s, 8s) = 10s |
| 128 | + java.lang.reflect.Method getTaskDuration = |
| 129 | + TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
| 130 | + getTaskDuration.setAccessible(true); |
| 131 | + Duration result = (Duration) getTaskDuration.invoke(runner); |
| 132 | + |
| 133 | + assertEquals(Duration.ofSeconds(10), result); |
| 134 | + } |
136 | 135 |
|
137 | | -@Test |
138 | | -public void testGetTaskDuration_EmptyLastTaskDurations() throws Exception { |
139 | | - OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
140 | | - Handle mockHandle = mock(Handle.class); |
141 | | - int threadPoolSize = 2; |
142 | | - TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
143 | | - ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
144 | | - List<Task<?>> tasks = Collections.nCopies(3, mock(Task.class)); |
145 | | - TasksRunner runner = |
146 | | - new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
147 | | - |
148 | | - java.lang.reflect.Field completedField = |
149 | | - TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
150 | | - completedField.setAccessible(true); |
151 | | - completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(3)); |
152 | | - |
153 | | - java.lang.reflect.Field stopwatchField = |
154 | | - TasksRunner.class.getDeclaredField("stopwatch"); |
155 | | - stopwatchField.setAccessible(true); |
156 | | - com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
157 | | - when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(9)); |
158 | | - stopwatchField.set(runner, mockStopwatch); |
159 | | - |
160 | | - java.lang.reflect.Field durationsField = |
161 | | - TasksRunner.class.getDeclaredField("lastTaskDurations"); |
162 | | - durationsField.setAccessible(true); |
163 | | - @SuppressWarnings("unchecked") |
164 | | - Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
165 | | - durations.clear(); |
166 | | - |
167 | | - // getAverageTaskDurationFromAllTasks = 9s / 3 = 3s |
168 | | - // getAverageTaskDurationFromLatestTasks = 0s |
169 | | - // getTaskDuration should return max(3s, 0s) = 3s |
170 | | - java.lang.reflect.Method getTaskDuration = TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
171 | | - getTaskDuration.setAccessible(true); |
172 | | - Duration result = (Duration) getTaskDuration.invoke(runner); |
173 | | - |
174 | | - assertEquals(Duration.ofSeconds(3), result); |
175 | | -} |
| 136 | + @Test |
| 137 | + public void testGetTaskDuration_EmptyLastTaskDurations() throws Exception { |
| 138 | + OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
| 139 | + Handle mockHandle = mock(Handle.class); |
| 140 | + int threadPoolSize = 2; |
| 141 | + TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
| 142 | + ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
| 143 | + List<Task<?>> tasks = Collections.nCopies(3, mock(Task.class)); |
| 144 | + TasksRunner runner = |
| 145 | + new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
| 146 | + |
| 147 | + java.lang.reflect.Field completedField = |
| 148 | + TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
| 149 | + completedField.setAccessible(true); |
| 150 | + completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(3)); |
| 151 | + |
| 152 | + java.lang.reflect.Field stopwatchField = TasksRunner.class.getDeclaredField("stopwatch"); |
| 153 | + stopwatchField.setAccessible(true); |
| 154 | + com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
| 155 | + when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(9)); |
| 156 | + stopwatchField.set(runner, mockStopwatch); |
| 157 | + |
| 158 | + java.lang.reflect.Field durationsField = |
| 159 | + TasksRunner.class.getDeclaredField("lastTaskDurations"); |
| 160 | + durationsField.setAccessible(true); |
| 161 | + @SuppressWarnings("unchecked") |
| 162 | + Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
| 163 | + durations.clear(); |
| 164 | + |
| 165 | + // getAverageTaskDurationFromAllTasks = 9s / 3 = 3s |
| 166 | + // getAverageTaskDurationFromLatestTasks = 0s |
| 167 | + // getTaskDuration should return max(3s, 0s) = 3s |
| 168 | + java.lang.reflect.Method getTaskDuration = |
| 169 | + TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
| 170 | + getTaskDuration.setAccessible(true); |
| 171 | + Duration result = (Duration) getTaskDuration.invoke(runner); |
| 172 | + |
| 173 | + assertEquals(Duration.ofSeconds(3), result); |
| 174 | + } |
176 | 175 |
|
177 | | -@Test |
178 | | -public void testGetTaskDuration_LastTaskDurationsGreaterThanAllTasks() throws Exception { |
179 | | - OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
180 | | - Handle mockHandle = mock(Handle.class); |
181 | | - int threadPoolSize = 2; |
182 | | - TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
183 | | - ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
184 | | - List<Task<?>> tasks = Collections.nCopies(4, mock(Task.class)); |
185 | | - TasksRunner runner = |
186 | | - new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
187 | | - |
188 | | - // Set numberOfCompletedTasks to 2 |
189 | | - java.lang.reflect.Field completedField = |
190 | | - TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
191 | | - completedField.setAccessible(true); |
192 | | - completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(2)); |
193 | | - |
194 | | - java.lang.reflect.Field stopwatchField = |
195 | | - TasksRunner.class.getDeclaredField("stopwatch"); |
196 | | - stopwatchField.setAccessible(true); |
197 | | - com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
198 | | - when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(6)); |
199 | | - stopwatchField.set(runner, mockStopwatch); |
200 | | - |
201 | | - java.lang.reflect.Field durationsField = |
202 | | - TasksRunner.class.getDeclaredField("lastTaskDurations"); |
203 | | - durationsField.setAccessible(true); |
204 | | - @SuppressWarnings("unchecked") |
205 | | - Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
206 | | - durations.clear(); |
207 | | - durations.add(Duration.ofSeconds(2)); |
208 | | - durations.add(Duration.ofSeconds(10)); |
209 | | - |
210 | | - // getAverageTaskDurationFromAllTasks = 6s / 2 = 3s |
211 | | - // getAverageTaskDurationFromLatestTasks = (10s - 2s) / 2 = 4s |
212 | | - // getTaskDuration should return max(3s, 4s) = 4s |
213 | | - java.lang.reflect.Method getTaskDuration = TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
214 | | - getTaskDuration.setAccessible(true); |
215 | | - Duration result = (Duration) getTaskDuration.invoke(runner); |
216 | | - |
217 | | - assertEquals(Duration.ofSeconds(4), result); |
218 | | -} |
| 176 | + @Test |
| 177 | + public void testGetTaskDuration_LastTaskDurationsGreaterThanAllTasks() throws Exception { |
| 178 | + OutputHandleFactory mockSinkFactory = mock(OutputHandleFactory.class); |
| 179 | + Handle mockHandle = mock(Handle.class); |
| 180 | + int threadPoolSize = 2; |
| 181 | + TaskSetState.Impl mockState = mock(TaskSetState.Impl.class); |
| 182 | + ConnectorArguments arguments = new ConnectorArguments("--connector", "test"); |
| 183 | + List<Task<?>> tasks = Collections.nCopies(4, mock(Task.class)); |
| 184 | + TasksRunner runner = |
| 185 | + new TasksRunner(mockSinkFactory, mockHandle, threadPoolSize, mockState, tasks, arguments); |
| 186 | + |
| 187 | + // Set numberOfCompletedTasks to 2 |
| 188 | + java.lang.reflect.Field completedField = |
| 189 | + TasksRunner.class.getDeclaredField("numberOfCompletedTasks"); |
| 190 | + completedField.setAccessible(true); |
| 191 | + completedField.set(runner, new java.util.concurrent.atomic.AtomicInteger(2)); |
| 192 | + |
| 193 | + java.lang.reflect.Field stopwatchField = TasksRunner.class.getDeclaredField("stopwatch"); |
| 194 | + stopwatchField.setAccessible(true); |
| 195 | + com.google.common.base.Stopwatch mockStopwatch = mock(com.google.common.base.Stopwatch.class); |
| 196 | + when(mockStopwatch.elapsed()).thenReturn(Duration.ofSeconds(6)); |
| 197 | + stopwatchField.set(runner, mockStopwatch); |
| 198 | + |
| 199 | + java.lang.reflect.Field durationsField = |
| 200 | + TasksRunner.class.getDeclaredField("lastTaskDurations"); |
| 201 | + durationsField.setAccessible(true); |
| 202 | + @SuppressWarnings("unchecked") |
| 203 | + Deque<Duration> durations = (Deque<Duration>) durationsField.get(runner); |
| 204 | + durations.clear(); |
| 205 | + durations.add(Duration.ofSeconds(2)); |
| 206 | + durations.add(Duration.ofSeconds(10)); |
| 207 | + |
| 208 | + // getAverageTaskDurationFromAllTasks = 6s / 2 = 3s |
| 209 | + // getAverageTaskDurationFromLatestTasks = (10s - 2s) / 2 = 4s |
| 210 | + // getTaskDuration should return max(3s, 4s) = 4s |
| 211 | + java.lang.reflect.Method getTaskDuration = |
| 212 | + TasksRunner.class.getDeclaredMethod("getTaskDuration"); |
| 213 | + getTaskDuration.setAccessible(true); |
| 214 | + Duration result = (Duration) getTaskDuration.invoke(runner); |
| 215 | + |
| 216 | + assertEquals(Duration.ofSeconds(4), result); |
| 217 | + } |
219 | 218 | } |
0 commit comments