Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue 1968 and correct visitor pattern code #1990

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
init fix for issue #1957
  • Loading branch information
xyllq999 committed May 9, 2022
commit d5bb7225c97362ab5a0f7b4de2abc5fd0d9b3dfe
2 changes: 1 addition & 1 deletion circuit-breaker/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
int failureCount;
private final int failureThreshold;
private State state;
private final long futureTime = 1000 * 1000 * 1000 * 1000;
private final long futureTime = 1000 * 1000 * 1000 * 1000L; //use L to prevent overflow

/**
* Constructor to create an instance of Circuit Breaker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DefaultCircuitBreaker implements CircuitBreaker {
int failureCount;
private final int failureThreshold;
private State state;
private final long futureTime = 1000 * 1000 * 1000 * 1000;
private final long futureTime = 1000 * 1000 * 1000 * 1000L; // use L to prevent overflow

/**
* Constructor to create an instance of Circuit Breaker.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public DelayedRemoteService() {
*/
@Override
public String call() throws RemoteServiceException {
var currentTime = System.nanoTime();
long currentTime = System.nanoTime();
//Since currentTime and serverStartTime are both in nanoseconds, we convert it to
//seconds by diving by 10e9 and ensure floating point division by multiplying it
//with 1.0 first. We then check if it is greater or less than specified delay and then
//send the reply
if ((currentTime - serverStartTime) * 1.0 / (1000 * 1000 * 1000) < delay) {
//Can use Thread.sleep() here to block and simulate a hung server
if ((currentTime - serverStartTime) * 1.0 / (1000 * 1000 * 1000L) < delay) {
//Can use Thread.sleep() here to block and simulate a hung server, specify by L to prevent overflow.
throw new RemoteServiceException("Delayed service is down");
}
return "Delayed service is working";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,12 @@ public void setupCircuitBreakers() {
//Set the circuit Breaker parameters
delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000,
FAILURE_THRESHOLD,
RETRY_PERIOD * 1000 * 1000 * 1000);
RETRY_PERIOD * 1000 * 1000 * 1000L); //specify by L to prevent overflow.

var quickService = new QuickRemoteService();
//Set the circuit Breaker parameters
quickServiceCircuitBreaker = new DefaultCircuitBreaker(quickService, 3000, FAILURE_THRESHOLD,
RETRY_PERIOD * 1000 * 1000 * 1000);
RETRY_PERIOD * 1000 * 1000 * 1000L); //specify by L to prevent overflow.

monitoringService = new MonitoringService(delayedServiceCircuitBreaker,
quickServiceCircuitBreaker);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void testEvaluateState() {
assertEquals(circuitBreaker.getState(), "HALF_OPEN");
//Since failureCount>failureThreshold, and lastFailureTime is much lesser current time,
//state should be open
circuitBreaker.lastFailureTime = System.nanoTime() - 1000 * 1000 * 1000 * 1000;
circuitBreaker.lastFailureTime = System.nanoTime() - 1000 * 1000 * 1000 * 1000L; // specify by L to prevent overflow.
circuitBreaker.evaluateState();
assertEquals(circuitBreaker.getState(), "OPEN");
//Now set it back again to closed to test idempotency
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ void testDefaultConstructor() throws RemoteServiceException {
*/
@Test
public void testParameterizedConstructor() throws RemoteServiceException {
var obj = new DelayedRemoteService(System.nanoTime()-2000*1000*1000,1);
var obj = new DelayedRemoteService(System.nanoTime()-2000 * 1000 * 1000L,1); //specify by L to prevent overflow.
assertEquals("Delayed service is working",obj.call());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ void testLocalResponse() {

@Test
void testDelayedRemoteResponseSuccess() {
var delayedService = new DelayedRemoteService(System.nanoTime()-2*1000*1000*1000, 2);
var delayedService = new DelayedRemoteService(System.nanoTime()-2 * 1000 * 1000 * 1000L, 2); //specify by L to prevent overflow.
var delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000,
1,
2 * 1000 * 1000 * 1000);
Expand All @@ -58,7 +58,7 @@ void testDelayedRemoteResponseFailure() {
var delayedService = new DelayedRemoteService(System.nanoTime(), 2);
var delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000,
1,
2 * 1000 * 1000 * 1000);
2 * 1000 * 1000 * 1000L); //specify by L to prevent overflow.
var monitoringService = new MonitoringService(delayedServiceCircuitBreaker,null);
//Set time as current time as initially server fails
var response = monitoringService.delayedServiceResponse();
Expand All @@ -70,7 +70,7 @@ void testQuickRemoteServiceResponse() {
var delayedService = new QuickRemoteService();
var delayedServiceCircuitBreaker = new DefaultCircuitBreaker(delayedService, 3000,
1,
2 * 1000 * 1000 * 1000);
2 * 1000 * 1000 * 1000L); // specify by L to prevent overflow.
var monitoringService = new MonitoringService(delayedServiceCircuitBreaker,null);
//Set time as current time as initially server fails
var response = monitoringService.delayedServiceResponse();
Expand Down