forked from Azure/azure-sdk-for-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from moarychan/moary/fix-jacoco-converage-not-met
Add UTs for default Jacoco coverage threshold
- Loading branch information
Showing
20 changed files
with
599 additions
and
298 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
...n/java/com/azure/spring/integration/instrumentation/AbstractProcessorInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.integration.instrumentation; | ||
|
||
import java.time.Duration; | ||
|
||
/** | ||
* Abstract instrumentation class. | ||
*/ | ||
public abstract class AbstractProcessorInstrumentation<T> implements Instrumentation { | ||
|
||
private final String name; | ||
|
||
private final Type type; | ||
|
||
private long lastErrorTimestamp = Long.MIN_VALUE; | ||
|
||
private final Duration noneErrorWindow; | ||
|
||
private T errorContext; | ||
|
||
/** | ||
* Construct a {@link AbstractProcessorInstrumentation} with the specified name, {@link Type} and the period of a none error window. | ||
* | ||
* @param name the name | ||
* @param type the type | ||
* @param noneErrorWindow the period of a none error window | ||
*/ | ||
public AbstractProcessorInstrumentation(String name, Type type, Duration noneErrorWindow) { | ||
this.name = name; | ||
this.type = type; | ||
this.noneErrorWindow = noneErrorWindow; | ||
} | ||
|
||
/** | ||
* Get type. | ||
* | ||
* @return type the type | ||
* @see Type | ||
*/ | ||
public Type getType() { | ||
return type; | ||
} | ||
|
||
/** | ||
* Check whether is down. | ||
* | ||
* @return true if the status is down,false otherwise | ||
*/ | ||
public boolean isDown() { | ||
if (System.currentTimeMillis() > lastErrorTimestamp + noneErrorWindow.toMillis()) { | ||
this.errorContext = null; | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Check whether is up. | ||
* | ||
* @return false if the status is up,true otherwise | ||
*/ | ||
public boolean isUp() { | ||
return !isDown(); | ||
} | ||
|
||
/** | ||
* Mark error. | ||
* | ||
* @param errorContext the error context | ||
*/ | ||
public void markError(T errorContext) { | ||
this.errorContext = errorContext; | ||
this.lastErrorTimestamp = System.currentTimeMillis(); | ||
} | ||
|
||
/** | ||
* Get error context. | ||
* | ||
* @return errorContext the error context | ||
*/ | ||
public T getErrorContext() { | ||
return errorContext; | ||
} | ||
|
||
/** | ||
* Get the name of destination entity. | ||
* | ||
* @return name the name of destination entity | ||
*/ | ||
public String getName() { | ||
return name; | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
...a/com/azure/spring/integration/instrumentation/AbstractProcessorInstrumentationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package com.azure.spring.integration.instrumentation; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.EnumSource; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
public abstract class AbstractProcessorInstrumentationTests<T> { | ||
|
||
private IllegalArgumentException exception; | ||
private T errorContext; | ||
private Duration window = Duration.ofSeconds(2); | ||
|
||
public abstract T getErrorContext(RuntimeException exception); | ||
public abstract AbstractProcessorInstrumentation<T> getProcessorInstrumentation(Instrumentation.Type type, | ||
Duration window); | ||
|
||
@BeforeEach | ||
void setUp() { | ||
exception = new IllegalArgumentException(); | ||
errorContext = getErrorContext(exception); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(value = Instrumentation.Type.class) | ||
void instrumentationId(Instrumentation.Type type) { | ||
AbstractProcessorInstrumentation<T> instrumentation = getProcessorInstrumentation(type, window); | ||
assertEquals(type.name() + ":test", instrumentation.getId()); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(value = Instrumentation.Type.class) | ||
void isUp(Instrumentation.Type type) { | ||
AbstractProcessorInstrumentation<T> instrumentation = getProcessorInstrumentation(type, window); | ||
assertTrue(instrumentation.isUp()); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(value = Instrumentation.Type.class) | ||
void isDown(Instrumentation.Type type) { | ||
AbstractProcessorInstrumentation<T> instrumentation = getProcessorInstrumentation(type, window); | ||
instrumentation.markError(errorContext); | ||
assertTrue(instrumentation.isDown()); | ||
sleep(1); | ||
assertTrue(instrumentation.isDown()); | ||
sleep(1); | ||
assertFalse(instrumentation.isDown()); | ||
} | ||
|
||
@ParameterizedTest | ||
@EnumSource(value = Instrumentation.Type.class) | ||
void makeError(Instrumentation.Type type) { | ||
AbstractProcessorInstrumentation<T> instrumentation = getProcessorInstrumentation(type, window); | ||
instrumentation.markError(errorContext); | ||
assertEquals(exception, instrumentation.getException()); | ||
instrumentation.markError(null); | ||
assertNull(instrumentation.getException()); | ||
} | ||
|
||
private void sleep(long sleep) { | ||
try { | ||
TimeUnit.of(ChronoUnit.SECONDS).sleep(sleep); | ||
} catch (InterruptedException e) { | ||
|
||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.