You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ThreadSafeProgressMonitorTest.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (C) 2010, Google Inc. and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.lib;
  11. import static org.junit.Assert.assertEquals;
  12. import static org.junit.Assert.assertFalse;
  13. import static org.junit.Assert.assertNull;
  14. import static org.junit.Assert.assertTrue;
  15. import static org.junit.Assert.fail;
  16. import java.util.concurrent.CountDownLatch;
  17. import java.util.concurrent.TimeUnit;
  18. import org.junit.Test;
  19. public class ThreadSafeProgressMonitorTest {
  20. @Test
  21. public void testFailsMethodsOnBackgroundThread()
  22. throws InterruptedException {
  23. final MockProgressMonitor mock = new MockProgressMonitor();
  24. final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
  25. runOnThread(() -> {
  26. try {
  27. pm.start(1);
  28. fail("start did not fail on background thread");
  29. } catch (IllegalStateException notMainThread) {
  30. // Expected result
  31. }
  32. try {
  33. pm.beginTask("title", 1);
  34. fail("beginTask did not fail on background thread");
  35. } catch (IllegalStateException notMainThread) {
  36. // Expected result
  37. }
  38. try {
  39. pm.endTask();
  40. fail("endTask did not fail on background thread");
  41. } catch (IllegalStateException notMainThread) {
  42. // Expected result
  43. }
  44. });
  45. // Ensure we didn't alter the mock above when checking threads.
  46. assertNull(mock.taskTitle);
  47. assertEquals(0, mock.value);
  48. }
  49. @Test
  50. public void testMethodsOkOnMainThread() {
  51. final MockProgressMonitor mock = new MockProgressMonitor();
  52. final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
  53. pm.start(1);
  54. assertEquals(1, mock.value);
  55. pm.beginTask("title", 42);
  56. assertEquals("title", mock.taskTitle);
  57. assertEquals(42, mock.value);
  58. pm.update(1);
  59. pm.pollForUpdates();
  60. assertEquals(43, mock.value);
  61. pm.update(2);
  62. pm.pollForUpdates();
  63. assertEquals(45, mock.value);
  64. pm.endTask();
  65. assertNull(mock.taskTitle);
  66. assertEquals(0, mock.value);
  67. }
  68. @Test
  69. public void testUpdateOnBackgroundThreads() throws InterruptedException {
  70. final MockProgressMonitor mock = new MockProgressMonitor();
  71. final ThreadSafeProgressMonitor pm = new ThreadSafeProgressMonitor(mock);
  72. pm.startWorker();
  73. final CountDownLatch doUpdate = new CountDownLatch(1);
  74. final CountDownLatch didUpdate = new CountDownLatch(1);
  75. final CountDownLatch doEndWorker = new CountDownLatch(1);
  76. final Thread bg = new Thread() {
  77. @Override
  78. public void run() {
  79. assertFalse(pm.isCancelled());
  80. await(doUpdate);
  81. pm.update(2);
  82. didUpdate.countDown();
  83. await(doEndWorker);
  84. pm.update(1);
  85. pm.endWorker();
  86. }
  87. };
  88. bg.start();
  89. pm.pollForUpdates();
  90. assertEquals(0, mock.value);
  91. doUpdate.countDown();
  92. await(didUpdate);
  93. pm.pollForUpdates();
  94. assertEquals(2, mock.value);
  95. doEndWorker.countDown();
  96. pm.waitForCompletion();
  97. assertEquals(3, mock.value);
  98. }
  99. private static void await(CountDownLatch cdl) {
  100. try {
  101. assertTrue("latch released", cdl.await(1000, TimeUnit.MILLISECONDS));
  102. } catch (InterruptedException ie) {
  103. fail("Did not expect to be interrupted");
  104. }
  105. }
  106. private static void runOnThread(Runnable task) throws InterruptedException {
  107. Thread t = new Thread(task);
  108. t.start();
  109. t.join(1000);
  110. assertFalse("thread has stopped", t.isAlive());
  111. }
  112. private static class MockProgressMonitor implements ProgressMonitor {
  113. String taskTitle;
  114. int value;
  115. @Override
  116. public void update(int completed) {
  117. value += completed;
  118. }
  119. @Override
  120. public void start(int totalTasks) {
  121. value = totalTasks;
  122. }
  123. @Override
  124. public void beginTask(String title, int totalWork) {
  125. taskTitle = title;
  126. value = totalWork;
  127. }
  128. @Override
  129. public void endTask() {
  130. taskTitle = null;
  131. value = 0;
  132. }
  133. @Override
  134. public boolean isCancelled() {
  135. return false;
  136. }
  137. }
  138. }