Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PerformanceLogContextTest.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. package org.eclipse.jgit.logging;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import static org.junit.Assert.assertTrue;
  5. import org.junit.Test;
  6. import java.util.List;
  7. /**
  8. * Tests for performance log context utilities.
  9. */
  10. public class PerformanceLogContextTest {
  11. @Test
  12. public void testAddEvent() {
  13. PerformanceLogRecord record = new PerformanceLogRecord("record", 0);
  14. PerformanceLogContext.getInstance().addEvent(record);
  15. List<PerformanceLogRecord> eventRecords = PerformanceLogContext
  16. .getInstance().getEventRecords();
  17. assertTrue(eventRecords.contains(record));
  18. assertEquals(1, eventRecords.size());
  19. }
  20. @Test
  21. public void testCleanEvents() {
  22. PerformanceLogRecord record1 = new PerformanceLogRecord("record1", 0);
  23. PerformanceLogContext.getInstance().addEvent(record1);
  24. PerformanceLogRecord record2 = new PerformanceLogRecord("record2", 0);
  25. PerformanceLogContext.getInstance().addEvent(record2);
  26. PerformanceLogContext.getInstance().cleanEvents();
  27. List<PerformanceLogRecord> eventRecords = PerformanceLogContext
  28. .getInstance().getEventRecords();
  29. assertEquals(0, eventRecords.size());
  30. }
  31. @Test
  32. public void testAddEventsTwoThreads() throws InterruptedException {
  33. TestRunnable runnable1 = new TestRunnable("record1", 1);
  34. TestRunnable runnable2 = new TestRunnable("record2", 2);
  35. Thread thread1 = new Thread(runnable1);
  36. Thread thread2 = new Thread(runnable2);
  37. thread1.start();
  38. thread2.start();
  39. thread1.join();
  40. thread2.join();
  41. assertEquals(1, runnable1.getEventRecordsCount());
  42. assertEquals(1, runnable2.getEventRecordsCount());
  43. assertFalse(runnable1.isThrown());
  44. assertFalse(runnable2.isThrown());
  45. }
  46. private static class TestRunnable implements Runnable {
  47. private String name;
  48. private long durationMs;
  49. private long eventRecordsCount;
  50. private boolean thrown = false;
  51. public TestRunnable(String name, long durationMs) {
  52. this.name = name;
  53. this.durationMs = durationMs;
  54. }
  55. public boolean isThrown() {
  56. return thrown;
  57. }
  58. public long getEventRecordsCount() {
  59. return eventRecordsCount;
  60. }
  61. @Override
  62. public void run() {
  63. PerformanceLogRecord record = new PerformanceLogRecord(name,
  64. durationMs);
  65. try {
  66. PerformanceLogContext.getInstance().addEvent(record);
  67. eventRecordsCount = PerformanceLogContext.getInstance()
  68. .getEventRecords().size();
  69. PerformanceLogContext.getInstance().cleanEvents();
  70. } catch (Exception e) {
  71. thrown = true;
  72. }
  73. }
  74. }
  75. }