fix: Fix sonar issues (#2925)

* Remove unused member which was also causing a false positive sonar issue.
Fixes sonar issue https://sonarcloud.io/project/issues?open=AY3gHwu5DIZTZkppqVEG&id=iluwatar_java-design-patterns

* Fixes sonar issue https://sonarcloud.io/project/issues?open=AXK0OzDA-CiGJS70dLki&id=iluwatar_java-design-patterns
related to "Refactor the code of the lambda to not have multiple invocations throwing the same checked exception."

Also, updated the code to use Instant and Duration to deal with time instead of int. Added the awaitility
library to perform assertions in test which is more reliable than using Thread.sleep directly to wait for events to happen.

* checkstyle fix

* Add sneaky throws to fix sonar lint issue. This is fine as the newFile method is not being tested but instead the new SimpleFileWriter(...) is.

* The first booking needs to happen outside the assertions. Fixed other warnings

* Use records to pass around related objects instead of using a large number of
individual params, which sonar did not like.

* Checkstyle fixes

* checkstyle fixes

* Remove complexity to keep sonar happy.

* Split into different methods to reduce complexity. Could be broken down even further but currently Sonar is happy.

* Move files to correct package

* Add valid assertions to tests

* rename constants to avoid confusion

* Sonar warning related to cognitive complexity can be suppressed as the methods are quite generic and not functional enough to be separated out.

* Use constants to keep Sonar happy

* Use correct constant naming conventions

* Use correct constant naming conventions

* Use lombok to define noargsconstructor

* Use a single method to do the logging

* Remove unused constructor and redundant method

* Use a reusable method for logging
This commit is contained in:
k1w1dev
2024-05-04 17:20:01 +12:00
committed by GitHub
parent 5e12bc588a
commit ef42e169b9
46 changed files with 571 additions and 554 deletions
@@ -25,6 +25,7 @@
package com.iluwatar.event.asynchronous;
import java.io.IOException;
import java.time.Duration;
import java.util.Properties;
import java.util.Scanner;
import lombok.extern.slf4j.Slf4j;
@@ -115,13 +116,13 @@ public class App {
try {
// Create an Asynchronous event.
var asyncEventId = eventManager.createAsync(60);
var asyncEventId = eventManager.createAsync(Duration.ofSeconds(60));
LOGGER.info("Async Event [{}] has been created.", asyncEventId);
eventManager.start(asyncEventId);
LOGGER.info("Async Event [{}] has been started.", asyncEventId);
// Create a Synchronous event.
var syncEventId = eventManager.create(60);
var syncEventId = eventManager.create(Duration.ofSeconds(60));
LOGGER.info("Sync Event [{}] has been created.", syncEventId);
eventManager.start(syncEventId);
LOGGER.info("Sync Event [{}] has been started.", syncEventId);
@@ -207,7 +208,7 @@ public class App {
LOGGER.info("Boil multiple eggs at once (A) or boil them one-by-one (S)?: ");
var eventType = s.nextLine();
LOGGER.info("How long should this egg be boiled for (in seconds)?: ");
var eventTime = s.nextInt();
var eventTime = Duration.ofSeconds(s.nextInt());
if (eventType.equalsIgnoreCase("A")) {
try {
var eventId = eventManager.createAsync(eventTime);
@@ -231,4 +232,4 @@ public class App {
}
}
}
}
@@ -24,6 +24,8 @@
*/
package com.iluwatar.event.asynchronous;
import java.time.Duration;
import java.time.Instant;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import lombok.Getter;
@@ -38,11 +40,11 @@ import lombok.extern.slf4j.Slf4j;
public class AsyncEvent implements Event, Runnable {
private final int eventId;
private final int eventTime;
private final Duration eventTime;
@Getter
private final boolean synchronous;
private Thread thread;
private AtomicBoolean isComplete = new AtomicBoolean(false);
private final AtomicBoolean isComplete = new AtomicBoolean(false);
private ThreadCompleteListener eventListener;
@Override
@@ -71,9 +73,9 @@ public class AsyncEvent implements Event, Runnable {
@Override
public void run() {
var currentTime = System.currentTimeMillis();
var endTime = currentTime + (eventTime * 1000L);
while (System.currentTimeMillis() < endTime) {
var currentTime = Instant.now();
var endTime = currentTime.plusSeconds(eventTime.getSeconds());
while (Instant.now().compareTo(endTime) < 0) {
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
@@ -90,13 +92,9 @@ public class AsyncEvent implements Event, Runnable {
this.eventListener = listener;
}
public final void removeListener() {
this.eventListener = null;
}
private void completed() {
if (eventListener != null) {
eventListener.completedEventHandler(eventId);
}
}
}
}
@@ -25,6 +25,7 @@
package com.iluwatar.event.asynchronous;
import java.security.SecureRandom;
import java.time.Duration;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import lombok.Getter;
@@ -43,7 +44,7 @@ public class EventManager implements ThreadCompleteListener {
// Just don't want to have too many running events. :)
public static final int MIN_ID = 1;
public static final int MAX_ID = MAX_RUNNING_EVENTS;
public static final int MAX_EVENT_TIME = 1800; // in seconds / 30 minutes.
public static final Duration MAX_EVENT_TIME = Duration.ofSeconds(1800); // 30 minutes.
private int currentlyRunningSyncEvent = -1;
private final SecureRandom rand;
@@ -71,7 +72,7 @@ public class EventManager implements ThreadCompleteListener {
* already running.
* @throws LongRunningEventException Long-running events are not allowed in the app.
*/
public int create(int eventTime)
public int create(Duration eventTime)
throws MaxNumOfEventsAllowedException, InvalidOperationException, LongRunningEventException {
if (currentlyRunningSyncEvent != -1) {
throw new InvalidOperationException("Event [" + currentlyRunningSyncEvent + "] is still"
@@ -92,19 +93,23 @@ public class EventManager implements ThreadCompleteListener {
* @throws MaxNumOfEventsAllowedException When too many events are running at a time.
* @throws LongRunningEventException Long-running events are not allowed in the app.
*/
public int createAsync(int eventTime) throws MaxNumOfEventsAllowedException,
public int createAsync(Duration eventTime) throws MaxNumOfEventsAllowedException,
LongRunningEventException {
return createEvent(eventTime, false);
}
private int createEvent(int eventTime, boolean isSynchronous)
private int createEvent(Duration eventTime, boolean isSynchronous)
throws MaxNumOfEventsAllowedException, LongRunningEventException {
if (eventTime.isNegative()) {
throw new IllegalArgumentException("eventTime cannot be negative");
}
if (eventPool.size() == MAX_RUNNING_EVENTS) {
throw new MaxNumOfEventsAllowedException("Too many events are running at the moment."
+ " Please try again later.");
}
if (eventTime >= MAX_EVENT_TIME) {
if (eventTime.getSeconds() > MAX_EVENT_TIME.getSeconds()) {
throw new LongRunningEventException(
"Maximum event time allowed is " + MAX_EVENT_TIME + " seconds. Please try again.");
}
@@ -215,4 +220,4 @@ public class EventManager implements ThreadCompleteListener {
public int numOfCurrentlyRunningSyncEvent() {
return currentlyRunningSyncEvent;
}
}
}
@@ -24,142 +24,109 @@
*/
package com.iluwatar.event.asynchronous;
import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import lombok.SneakyThrows;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.time.Duration;
/**
* Application test
*/
class EventAsynchronousTest {
private static final Logger LOGGER = LoggerFactory.getLogger(EventAsynchronousTest.class);
@Test
@SneakyThrows
void testAsynchronousEvent() {
var eventManager = new EventManager();
try {
var aEventId = eventManager.createAsync(60);
eventManager.start(aEventId);
assertEquals(1, eventManager.getEventPool().size());
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertEquals(-1, eventManager.numOfCurrentlyRunningSyncEvent());
eventManager.cancel(aEventId);
assertTrue(eventManager.getEventPool().isEmpty());
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
var aEventId = eventManager.createAsync(Duration.ofSeconds(60));
assertDoesNotThrow(() ->eventManager.start(aEventId));
assertEquals(1, eventManager.getEventPool().size());
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertEquals(-1, eventManager.numOfCurrentlyRunningSyncEvent());
assertDoesNotThrow(() -> eventManager.cancel(aEventId));
assertTrue(eventManager.getEventPool().isEmpty());
}
@Test
@SneakyThrows
void testSynchronousEvent() {
var eventManager = new EventManager();
try {
var sEventId = eventManager.create(60);
eventManager.start(sEventId);
assertEquals(1, eventManager.getEventPool().size());
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertNotEquals(-1, eventManager.numOfCurrentlyRunningSyncEvent());
eventManager.cancel(sEventId);
assertTrue(eventManager.getEventPool().isEmpty());
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
LOGGER.error(e.getMessage());
}
var sEventId = eventManager.create(Duration.ofSeconds(60));
assertDoesNotThrow(() -> eventManager.start(sEventId));
assertEquals(1, eventManager.getEventPool().size());
assertTrue(eventManager.getEventPool().size() < EventManager.MAX_RUNNING_EVENTS);
assertNotEquals(-1, eventManager.numOfCurrentlyRunningSyncEvent());
assertDoesNotThrow(() -> eventManager.cancel(sEventId));
assertTrue(eventManager.getEventPool().isEmpty());
}
@Test
@SneakyThrows
void testFullSynchronousEvent() {
var eventManager = new EventManager();
var eventTime = Duration.ofSeconds(1);
var sEventId = eventManager.create(eventTime);
assertEquals(1, eventManager.getEventPool().size());
eventManager.start(sEventId);
await().until(() -> eventManager.getEventPool().isEmpty());
}
@Test
@SneakyThrows
void testUnsuccessfulSynchronousEvent() {
assertThrows(InvalidOperationException.class, () -> {
var eventManager = new EventManager();
try {
var sEventId = eventManager.create(60);
eventManager.start(sEventId);
sEventId = eventManager.create(60);
eventManager.start(sEventId);
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
var sEventId = assertDoesNotThrow(() -> eventManager.create(Duration.ofSeconds(60)));
eventManager.start(sEventId);
sEventId = eventManager.create(Duration.ofSeconds(60));
eventManager.start(sEventId);
});
}
@Test
void testFullSynchronousEvent() {
var eventManager = new EventManager();
try {
var eventTime = 1;
var sEventId = eventManager.create(eventTime);
assertEquals(1, eventManager.getEventPool().size());
eventManager.start(sEventId);
var currentTime = System.currentTimeMillis();
// +2 to give a bit of buffer time for event to complete properly.
var endTime = currentTime + (eventTime + 2 * 1000);
long sleepTime = endTime - System.currentTimeMillis();
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
LOGGER.error("Thread interrupted: ", e);
Thread.currentThread().interrupt();
}
}
assertTrue(eventManager.getEventPool().isEmpty());
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException
| InvalidOperationException e) {
LOGGER.error(e.getMessage());
}
}
@Test
@SneakyThrows
void testFullAsynchronousEvent() {
var eventManager = new EventManager();
try {
var eventTime = 1;
var eventTime = Duration.ofSeconds(1);
var aEventId1 = eventManager.createAsync(eventTime);
var aEventId2 = eventManager.createAsync(eventTime);
var aEventId3 = eventManager.createAsync(eventTime);
assertEquals(3, eventManager.getEventPool().size());
var aEventId1 = assertDoesNotThrow(() -> eventManager.createAsync(eventTime));
var aEventId2 = assertDoesNotThrow(() -> eventManager.createAsync(eventTime));
var aEventId3 = assertDoesNotThrow(() -> eventManager.createAsync(eventTime));
assertEquals(3, eventManager.getEventPool().size());
eventManager.start(aEventId1);
eventManager.start(aEventId2);
eventManager.start(aEventId3);
eventManager.start(aEventId1);
eventManager.start(aEventId2);
eventManager.start(aEventId3);
var currentTime = System.currentTimeMillis();
// +2 to give a bit of buffer time for event to complete properly.
var endTime = currentTime + (eventTime + 2 * 1000);
await().until(() -> eventManager.getEventPool().isEmpty());
long sleepTime = endTime - System.currentTimeMillis();
if (sleepTime > 0) {
try {
Thread.sleep(sleepTime);
} catch (InterruptedException e) {
LOGGER.error("Thread interrupted: ", e);
Thread.currentThread().interrupt();
}
}
assertTrue(eventManager.getEventPool().isEmpty());
} catch (MaxNumOfEventsAllowedException | LongRunningEventException | EventDoesNotExistException e) {
LOGGER.error(e.getMessage());
}
}
@Test
void testLongRunningEventException(){
assertThrows(LongRunningEventException.class, () -> {
var eventManager = new EventManager();
eventManager.createAsync(2000);
eventManager.createAsync(Duration.ofMinutes(31));
});
}
@@ -169,7 +136,7 @@ class EventAsynchronousTest {
assertThrows(MaxNumOfEventsAllowedException.class, () -> {
final var eventManager = new EventManager();
for(int i=0;i<1100;i++){
eventManager.createAsync(i);
eventManager.createAsync(Duration.ofSeconds(i));
}
});
}