|
25 | 25 | import java.util.Collections; |
26 | 26 | import java.util.concurrent.TimeUnit; |
27 | 27 |
|
28 | | -import org.eclipse.core.runtime.IProgressMonitor; |
29 | 28 | import org.eclipse.core.runtime.IStatus; |
30 | 29 | import org.eclipse.core.runtime.Status; |
31 | 30 | import org.eclipse.core.runtime.jobs.Job; |
@@ -152,36 +151,49 @@ public void testItemOrder() throws Exception { |
152 | 151 | // allJobs.add(keptJob); |
153 | 152 |
|
154 | 153 | try { |
| 154 | + // Set all jobs to not finish immediately so they stay running for comparison |
| 155 | + for (DummyJob job : jobsToSchedule) { |
| 156 | + job.shouldFinish = false; |
| 157 | + } |
| 158 | + |
| 159 | + // Close and reopen the progress view to get a fresh comparator with empty lastIndexes |
| 160 | + hideProgressView(); |
| 161 | + openProgressView(); |
| 162 | + |
| 163 | + // Schedule all jobs in random order rapidly to minimize throttled updates between schedules |
155 | 164 | ArrayList<DummyJob> shuffledJobs = new ArrayList<>(jobsToSchedule); |
156 | 165 | Collections.shuffle(shuffledJobs); |
157 | 166 | StringBuilder scheduleOrder = new StringBuilder("Jobs schedule order: "); |
158 | | - progressView.getViewer().refresh(); // order will only hold on the first time. |
159 | | - Thread.sleep(200); // wait till throttled update ran. |
160 | | - Job dummyJob = new Job("dummy throttled caller") { |
161 | | - @Override |
162 | | - protected IStatus run(IProgressMonitor monitor) { |
163 | | - return Status.OK_STATUS; |
164 | | - } |
165 | | - }; |
166 | | - dummyJob.schedule(); // trigger throttled update to clear ProgressViewerComparator.lastIndexes |
167 | | - // now hope the loop is executed before next throttled update (could fail if VM |
168 | | - // is busy otherwise): |
169 | 167 | for (DummyJob job : shuffledJobs) { |
170 | | - job.shouldFinish = false; |
171 | | - job.schedule(); // if the schedule updates the progress View (throttled) the sort order is |
172 | | - // affected |
| 168 | + job.schedule(); |
173 | 169 | scheduleOrder.append(job.getName()).append(", "); |
174 | 170 | } |
175 | 171 | TestPlugin.getDefault().getLog() |
176 | 172 | .log(new Status(IStatus.OK, TestPlugin.PLUGIN_ID, scheduleOrder.toString())); |
177 | 173 |
|
| 174 | + // Wait for all jobs to be running |
178 | 175 | for (DummyJob job : allJobs) { |
179 | 176 | processEventsUntil(() -> job.inProgress, TimeUnit.SECONDS.toMillis(3)); |
180 | 177 | } |
181 | | - progressView.getViewer().refresh(); |
| 178 | + |
| 179 | + // Wait for the progress view to be updated with all jobs |
| 180 | + processEventsUntil(() -> progressView.getViewer().getProgressInfoItems().length == allJobs.size(), |
| 181 | + TimeUnit.SECONDS.toMillis(5)); |
| 182 | + |
| 183 | + // The ProgressViewerComparator uses lastIndexes for stable sorting. After throttled updates |
| 184 | + // during job scheduling, lastIndexes may contain jobs in schedule order rather than priority order. |
| 185 | + // Close and reopen the view again to reset the comparator's state, then do a single refresh |
| 186 | + // to sort all jobs by priority from a clean state. |
| 187 | + hideProgressView(); |
| 188 | + openProgressView(); |
| 189 | + |
| 190 | + // Wait for the view to be populated again |
182 | 191 | processEventsUntil(() -> progressView.getViewer().getProgressInfoItems().length == allJobs.size(), |
183 | 192 | TimeUnit.SECONDS.toMillis(5)); |
184 | 193 |
|
| 194 | + // Give time for any pending updates |
| 195 | + processEventsUntil(() -> false, 500); |
| 196 | + |
185 | 197 | ProgressInfoItem[] progressInfoItems = progressView.getViewer().getProgressInfoItems(); |
186 | 198 | assertEquals("Not all jobs visible in progress view", allJobs.size(), progressInfoItems.length); |
187 | 199 | Object[] expected = allJobs.toArray(); |
|
0 commit comments