Skip to content

Commit c16d54d

Browse files
(PR) Updating The Developemnt Branch After HotFix
(PR) Updating The Developemnt Branch After HotFix
2 parents 6d50209 + 6d2e4aa commit c16d54d

File tree

3 files changed

+98
-68
lines changed

3 files changed

+98
-68
lines changed

lib/src/main/java/org/assignment/ListSynchronization.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public boolean simpleTestMethod() {
99
return true;
1010
}
1111

12-
public boolean delayedMethod(int delayMillis) {
12+
public boolean add(int delayMillis) {
1313
try {
1414
Thread.sleep(delayMillis);
1515
} catch (InterruptedException e) {
@@ -19,4 +19,12 @@ public boolean delayedMethod(int delayMillis) {
1919

2020
return true;
2121
}
22+
23+
public boolean update(int delayMillis) {
24+
return add(delayMillis);
25+
}
26+
27+
public boolean delete(int delayMillis) {
28+
return add(delayMillis);
29+
}
2230
}

lib/src/test/java/org/assignment/CustomTestRunner.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
public class CustomTestRunner extends Runner {
2020

2121
private final Class<?> testClass;
22+
private int passedTests = 0;
23+
private int failedTests = 0;
2224

2325
public CustomTestRunner(Class<?> testClass) {
2426
this.testClass = testClass;
@@ -31,6 +33,7 @@ public Description getDescription() {
3133

3234
@Override
3335
public void run(RunNotifier notifier) {
36+
long startTime = System.currentTimeMillis();
3437
try {
3538
Object testInstance = testClass.getDeclaredConstructor().newInstance();
3639
Method[] methods = testClass.getDeclaredMethods();
@@ -54,6 +57,21 @@ public void run(RunNotifier notifier) {
5457
}
5558
} catch (Exception e) {
5659
e.printStackTrace();
60+
} finally {
61+
long endTime = System.currentTimeMillis();
62+
long totalTime = endTime - startTime;
63+
int totalTests = passedTests + failedTests;
64+
65+
// ANSI escape codes for colors
66+
final String ANSI_RESET = "\u001B[0m";
67+
final String ANSI_GREEN = "\u001B[32m";
68+
final String ANSI_RED = "\u001B[31m";
69+
final String ANSI_BLUE = "\u001B[34m";
70+
71+
System.out.println(ANSI_GREEN + "Total tests: " + totalTests + ANSI_RESET);
72+
System.out.println(ANSI_GREEN + "Passed tests: " + passedTests + ANSI_RESET);
73+
System.out.println(ANSI_RED + "Failed tests: " + failedTests + ANSI_RESET);
74+
System.out.println(ANSI_BLUE + "Total time taken: " + totalTime + " ms" + ANSI_RESET);
5775
}
5876
}
5977

@@ -62,8 +80,10 @@ private void runSequentialTest(Method method, Object testInstance, RunNotifier n
6280
notifier.fireTestStarted(description);
6381
try {
6482
method.invoke(testInstance);
83+
passedTests++;
6584
notifier.fireTestFinished(description);
6685
} catch (Throwable t) {
86+
failedTests++;
6787
notifier.fireTestFailure(new org.junit.runner.notification.Failure(description, t));
6888
}
6989
}
@@ -76,8 +96,10 @@ private void runConcurrentTest(Method method, Object testInstance, RunNotifier n
7696

7797
executor.submit(() -> {
7898
try {
99+
passedTests++;
79100
method.invoke(testInstance);
80101
} catch (Throwable t) {
102+
failedTests++;
81103
notifier.fireTestFailure(new org.junit.runner.notification.Failure(description, t));
82104
} finally {
83105
latch.countDown();
@@ -87,6 +109,7 @@ private void runConcurrentTest(Method method, Object testInstance, RunNotifier n
87109
try {
88110
latch.await();
89111
} catch (InterruptedException e) {
112+
failedTests++;
90113
notifier.fireTestFailure(new org.junit.runner.notification.Failure(description, e));
91114
} finally {
92115
executor.shutdown();

lib/src/test/java/org/assignment/TestRunner.java

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -25,123 +25,111 @@ public void someLibraryMethodReturnsTrue() {
2525

2626
@Test
2727
@Order(2)
28-
@RunMode(ExecutionMode.CONCURRENT)
29-
public void concurrentLibraryTest() throws InterruptedException {
28+
@RunMode(ExecutionMode.SEQUENTIAL)
29+
public void sequentialLibraryTest() {
3030
ListSynchronization classUnderTest = new ListSynchronization();
3131
int iterations = 10;
32-
CountDownLatch latch = new CountDownLatch(iterations);
33-
ThreadPool threadPool = new ThreadPool(Runtime.getRuntime().availableProcessors());
3432
AtomicBoolean hasFailed = new AtomicBoolean(false);
3533

3634
long startTime = System.nanoTime();
3735
for (int i = 0; i < iterations; i++) {
38-
threadPool.submitTask(() -> {
39-
try {
40-
if (!classUnderTest.delayedMethod(5000)) {
41-
hasFailed.set(true);
42-
}
43-
} finally {
44-
latch.countDown();
45-
}
46-
});
36+
if (!classUnderTest.add(5000)) {
37+
hasFailed.set(true);
38+
}
4739
}
48-
49-
latch.await();
50-
threadPool.shutdown();
5140
long endTime = System.nanoTime();
5241

5342
double executionTime = (endTime - startTime) / 1_000_000.0;
54-
System.out.printf("Concurrent test with %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
43+
System.out.printf("Sequential test completed in %.2f ms%n", executionTime);
5544

56-
assertFalse("Concurrent test should not have any failures", hasFailed.get());
57-
assertTrue("Concurrent test should complete in a reasonable time", executionTime < 60000);
45+
assertFalse("Sequential test should not have any failures", hasFailed.get());
46+
assertTrue("Sequential test should complete in a reasonable time", executionTime < 60000);
5847
}
5948

6049
@Test
6150
@Order(3)
6251
@RunMode(ExecutionMode.SEQUENTIAL)
63-
public void sequentialLibraryTest() {
52+
public void sequentialLibraryTestShortDelay() {
6453
ListSynchronization classUnderTest = new ListSynchronization();
65-
int iterations = 10;
54+
int iterations = 10;
6655
AtomicBoolean hasFailed = new AtomicBoolean(false);
6756

6857
long startTime = System.nanoTime();
6958
for (int i = 0; i < iterations; i++) {
70-
if (!classUnderTest.delayedMethod(5000)) {
59+
if (!classUnderTest.update(1000)) {
7160
hasFailed.set(true);
7261
}
7362
}
7463
long endTime = System.nanoTime();
7564

7665
double executionTime = (endTime - startTime) / 1_000_000.0;
77-
System.out.printf("Sequential test completed in %.2f ms%n", executionTime);
66+
System.out.printf("Sequential test with short delay completed in %.2f ms%n", executionTime);
7867

79-
assertFalse("Sequential test should not have any failures", hasFailed.get());
80-
assertTrue("Sequential test should complete in a reasonable time", executionTime < 60000);
68+
assertFalse("Sequential test with short delay should not have any failures", hasFailed.get());
69+
assertTrue("Sequential test with short delay should complete in a reasonable time", executionTime < 60000);
8170
}
8271

83-
8472
@Test
8573
@Order(4)
86-
@RunMode(ExecutionMode.CONCURRENT)
87-
public void concurrentLibraryTestShortDelay() throws InterruptedException {
74+
@RunMode(ExecutionMode.SEQUENTIAL)
75+
public void sequentialLibraryTestMediumDelay() {
8876
ListSynchronization classUnderTest = new ListSynchronization();
8977
int iterations = 10;
90-
CountDownLatch latch = new CountDownLatch(iterations);
91-
ThreadPool threadPool = new ThreadPool(Runtime.getRuntime().availableProcessors());
9278
AtomicBoolean hasFailed = new AtomicBoolean(false);
9379

9480
long startTime = System.nanoTime();
9581
for (int i = 0; i < iterations; i++) {
96-
threadPool.submitTask(() -> {
97-
try {
98-
if (!classUnderTest.delayedMethod(1000)) {
99-
hasFailed.set(true);
100-
}
101-
} finally {
102-
latch.countDown();
103-
}
104-
});
82+
if (!classUnderTest.delete(3000)) {
83+
hasFailed.set(true);
84+
}
10585
}
106-
107-
latch.await();
108-
threadPool.shutdown();
10986
long endTime = System.nanoTime();
11087

11188
double executionTime = (endTime - startTime) / 1_000_000.0;
112-
System.out.printf("Concurrent test with short delay and %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
89+
System.out.printf("Sequential test with medium delay completed in %.2f ms%n", executionTime);
11390

114-
assertFalse("Concurrent test with short delay should not have any failures", hasFailed.get());
115-
assertTrue("Concurrent test with short delay should complete in a reasonable time", executionTime < 60000);
91+
assertFalse("Sequential test with medium delay should not have any failures", hasFailed.get());
92+
assertTrue("Sequential test with medium delay should complete in a reasonable time", executionTime < 60000);
11693
}
11794

11895
@Test
11996
@Order(5)
120-
@RunMode(ExecutionMode.SEQUENTIAL)
121-
public void sequentialLibraryTestShortDelay() {
97+
@RunMode(ExecutionMode.CONCURRENT)
98+
public void concurrentLibraryTestMediumDelay() throws InterruptedException {
12299
ListSynchronization classUnderTest = new ListSynchronization();
123100
int iterations = 10;
101+
CountDownLatch latch = new CountDownLatch(iterations);
102+
ThreadPool threadPool = new ThreadPool(Runtime.getRuntime().availableProcessors());
124103
AtomicBoolean hasFailed = new AtomicBoolean(false);
125104

126105
long startTime = System.nanoTime();
127106
for (int i = 0; i < iterations; i++) {
128-
if (!classUnderTest.delayedMethod(1000)) {
129-
hasFailed.set(true);
130-
}
107+
threadPool.submitTask(() -> {
108+
try {
109+
if (!classUnderTest.add(5000)) {
110+
hasFailed.set(true);
111+
}
112+
} finally {
113+
latch.countDown();
114+
}
115+
});
131116
}
117+
118+
latch.await();
119+
threadPool.shutdown();
132120
long endTime = System.nanoTime();
133121

134122
double executionTime = (endTime - startTime) / 1_000_000.0;
135-
System.out.printf("Sequential test with short delay completed in %.2f ms%n", executionTime);
123+
System.out.printf("Concurrent test with medium delay and %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
136124

137-
assertFalse("Sequential test with short delay should not have any failures", hasFailed.get());
138-
assertTrue("Sequential test with short delay should complete in a reasonable time", executionTime < 60000);
125+
assertFalse("Concurrent test with medium delay should not have any failures", hasFailed.get());
126+
assertTrue("Concurrent test with medium delay should complete in a reasonable time", executionTime < 60000);
139127
}
140128

141129
@Test
142130
@Order(6)
143131
@RunMode(ExecutionMode.CONCURRENT)
144-
public void concurrentLibraryTestMediumDelay() throws InterruptedException {
132+
public void concurrentLibraryTestShortDelay() throws InterruptedException {
145133
ListSynchronization classUnderTest = new ListSynchronization();
146134
int iterations = 10;
147135
CountDownLatch latch = new CountDownLatch(iterations);
@@ -152,7 +140,7 @@ public void concurrentLibraryTestMediumDelay() throws InterruptedException {
152140
for (int i = 0; i < iterations; i++) {
153141
threadPool.submitTask(() -> {
154142
try {
155-
if (!classUnderTest.delayedMethod(3000)) {
143+
if (!classUnderTest.update(1000)) {
156144
hasFailed.set(true);
157145
}
158146
} finally {
@@ -166,32 +154,43 @@ public void concurrentLibraryTestMediumDelay() throws InterruptedException {
166154
long endTime = System.nanoTime();
167155

168156
double executionTime = (endTime - startTime) / 1_000_000.0;
169-
System.out.printf("Concurrent test with medium delay and %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
157+
System.out.printf("Concurrent test with short delay and %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
170158

171-
assertFalse("Concurrent test with medium delay should not have any failures", hasFailed.get());
172-
assertTrue("Concurrent test with medium delay should complete in a reasonable time", executionTime < 60000);
159+
assertFalse("Concurrent test with short delay should not have any failures", hasFailed.get());
160+
assertTrue("Concurrent test with short delay should complete in a reasonable time", executionTime < 60000);
173161
}
174162

175163
@Test
176164
@Order(7)
177-
@RunMode(ExecutionMode.SEQUENTIAL)
178-
public void sequentialLibraryTestMediumDelay() {
165+
@RunMode(ExecutionMode.CONCURRENT)
166+
public void concurrentLibraryTest() throws InterruptedException {
179167
ListSynchronization classUnderTest = new ListSynchronization();
180-
int iterations = 10;
168+
int iterations = 10;
169+
CountDownLatch latch = new CountDownLatch(iterations);
170+
ThreadPool threadPool = new ThreadPool(Runtime.getRuntime().availableProcessors());
181171
AtomicBoolean hasFailed = new AtomicBoolean(false);
182172

183173
long startTime = System.nanoTime();
184174
for (int i = 0; i < iterations; i++) {
185-
if (!classUnderTest.delayedMethod(3000)) {
186-
hasFailed.set(true);
187-
}
175+
threadPool.submitTask(() -> {
176+
try {
177+
if (!classUnderTest.delete(3000)) {
178+
hasFailed.set(true);
179+
}
180+
} finally {
181+
latch.countDown();
182+
}
183+
});
188184
}
185+
186+
latch.await();
187+
threadPool.shutdown();
189188
long endTime = System.nanoTime();
190189

191190
double executionTime = (endTime - startTime) / 1_000_000.0;
192-
System.out.printf("Sequential test with medium delay completed in %.2f ms%n", executionTime);
191+
System.out.printf("Concurrent test with %d threads completed in %.2f ms%n", Runtime.getRuntime().availableProcessors(), executionTime);
193192

194-
assertFalse("Sequential test with medium delay should not have any failures", hasFailed.get());
195-
assertTrue("Sequential test with medium delay should complete in a reasonable time", executionTime < 60000);
193+
assertFalse("Concurrent test should not have any failures", hasFailed.get());
194+
assertTrue("Concurrent test should complete in a reasonable time", executionTime < 60000);
196195
}
197196
}

0 commit comments

Comments
 (0)