use of org.mockito.Mock in project dropwizard by dropwizard.
the class HealthCheckManagerTest method shouldNotChangeServerStateWhenNonCriticalHealthCheckRecovers.
@Test
void shouldNotChangeServerStateWhenNonCriticalHealthCheckRecovers() {
final List<HealthCheckConfiguration> configs = new ArrayList<>();
final HealthCheckConfiguration nonCriticalConfig = new HealthCheckConfiguration();
nonCriticalConfig.setName(NAME);
nonCriticalConfig.setCritical(false);
nonCriticalConfig.setSchedule(new Schedule());
configs.add(nonCriticalConfig);
final HealthCheckConfiguration criticalConfig = new HealthCheckConfiguration();
criticalConfig.setName(NAME_2);
criticalConfig.setCritical(true);
criticalConfig.setSchedule(new Schedule());
configs.add(criticalConfig);
final HealthCheckManager manager = new HealthCheckManager(unmodifiableList(configs), scheduler, new MetricRegistry(), SHUTDOWN_WAIT, true, Collections.emptyList());
final HealthCheck check = mock(HealthCheck.class);
manager.onHealthCheckAdded(NAME, check);
manager.onHealthCheckAdded(NAME_2, check);
manager.onStateChanged(NAME, false);
manager.onStateChanged(NAME_2, false);
assertThat(manager.isHealthy()).isFalse();
manager.onStateChanged(NAME, true);
assertThat(manager.isHealthy()).isFalse();
ArgumentCaptor<ScheduledHealthCheck> checkCaptor = ArgumentCaptor.forClass(ScheduledHealthCheck.class);
ArgumentCaptor<Boolean> healthyCaptor = ArgumentCaptor.forClass(Boolean.class);
verify(scheduler, times(2)).scheduleInitial(checkCaptor.capture());
verify(scheduler, times(3)).schedule(checkCaptor.capture(), healthyCaptor.capture());
assertThat(checkCaptor.getAllValues()).hasSize(5).satisfies(values -> assertThat(values).element(0).satisfies(value -> assertThat(value.getName()).isEqualTo(NAME)).satisfies(value -> assertThat(value.isCritical()).isFalse())).satisfies(values -> assertThat(values).element(1).satisfies(value -> assertThat(value.getName()).isEqualTo(NAME_2)).satisfies(value -> assertThat(value.isCritical()).isTrue())).satisfies(values -> assertThat(values).element(2).satisfies(value -> assertThat(value.getName()).isEqualTo(NAME)).satisfies(value -> assertThat(value.isCritical()).isFalse())).satisfies(values -> assertThat(values).element(3).satisfies(value -> assertThat(value.getName()).isEqualTo(NAME_2)).satisfies(value -> assertThat(value.isCritical()).isTrue())).satisfies(values -> assertThat(values).element(4).satisfies(value -> assertThat(value.getName()).isEqualTo(NAME)).satisfies(value -> assertThat(value.isCritical()).isFalse()));
assertThat(healthyCaptor.getAllValues()).containsExactly(false, false, true);
}
use of org.mockito.Mock in project dropwizard by dropwizard.
the class HealthCheckManagerTest method shouldMarkServerHealthyWhenCriticalHealthCheckRecovers.
@Test
void shouldMarkServerHealthyWhenCriticalHealthCheckRecovers() {
final HealthCheckConfiguration config = new HealthCheckConfiguration();
config.setName(NAME);
config.setCritical(true);
config.setSchedule(new Schedule());
final HealthCheckManager manager = new HealthCheckManager(singletonList(config), scheduler, new MetricRegistry(), SHUTDOWN_WAIT, true, Collections.emptyList());
final HealthCheck check = mock(HealthCheck.class);
manager.onHealthCheckAdded(NAME, check);
manager.onStateChanged(NAME, false);
assertThat(manager.isHealthy()).isFalse();
manager.onStateChanged(NAME, true);
assertThat(manager.isHealthy()).isTrue();
ArgumentCaptor<ScheduledHealthCheck> checkCaptor = ArgumentCaptor.forClass(ScheduledHealthCheck.class);
ArgumentCaptor<Boolean> healthyCaptor = ArgumentCaptor.forClass(Boolean.class);
verify(scheduler).scheduleInitial(checkCaptor.capture());
verify(scheduler, times(2)).schedule(checkCaptor.capture(), healthyCaptor.capture());
assertThat(checkCaptor.getAllValues()).hasSize(3).allMatch(value -> NAME.equals(value.getName())).allMatch(ScheduledHealthCheck::isCritical);
assertThat(healthyCaptor.getAllValues()).containsExactly(false, true);
}
use of org.mockito.Mock in project AuthMeReloaded by AuthMe.
the class HasPermissionCheckerTest method shouldShowUsageInfo.
@Test
public void shouldShowUsageInfo() {
// given
CommandSender sender = mock(CommandSender.class);
// when
hasPermissionChecker.execute(sender, emptyList());
// then
ArgumentCaptor<String> msgCaptor = ArgumentCaptor.forClass(String.class);
verify(sender, atLeast(2)).sendMessage(msgCaptor.capture());
assertThat(msgCaptor.getAllValues().stream().anyMatch(msg -> msg.contains("/authme debug perm bobby my.perm.node")), equalTo(true));
}
use of org.mockito.Mock in project beam by apache.
the class BaseFirestoreV1WriteFnTest method endToEnd_deadlineExceededOnAnIndividualWriteResultsInThrottling.
@Test
public final void endToEnd_deadlineExceededOnAnIndividualWriteResultsInThrottling() throws Exception {
final long totalDocCount = 1_000_000;
final int numWorkers = 100;
final long docCount = totalDocCount / numWorkers;
LOG.info("docCount = {}", docCount);
RpcQosOptions options = rpcQosOptions.toBuilder().withHintMaxNumWorkers(numWorkers).withSamplePeriod(Duration.standardMinutes(10)).withReportDiagnosticMetrics().build();
LOG.debug("options = {}", options);
FirestoreStatefulComponentFactory ff = mock(FirestoreStatefulComponentFactory.class);
when(ff.getFirestoreStub(any())).thenReturn(stub);
Random random = new Random(12345);
TestClock clock = new TestClock(Instant.EPOCH, Duration.standardSeconds(1));
Sleeper sleeper = millis -> clock.setNext(advanceClockBy(Duration.millis(millis)));
RpcQosImpl qos = new RpcQosImpl(options, random, sleeper, metricsFixture.counterFactory, metricsFixture.distributionFactory);
RpcQos qosSpy = mock(RpcQos.class, invocation -> {
Method method = invocation.getMethod();
LOG.debug("method = {}", method);
Method actualMethod = qos.getClass().getMethod(method.getName(), method.getParameterTypes());
return actualMethod.invoke(qos, invocation.getArguments());
});
when(ff.getRpcQos(options)).thenReturn(qosSpy);
int defaultDocumentWriteLatency = 30;
final AtomicLong writeCounter = new AtomicLong();
when(processContext.element()).thenAnswer(invocation -> newWrite(writeCounter.getAndIncrement()));
when(callable.call(any())).thenAnswer(new Answer<BatchWriteResponse>() {
private final Random rand = new Random(84572908);
private final Instant threshold = Instant.ofEpochMilli(Duration.standardMinutes(20).getMillis());
@Override
public BatchWriteResponse answer(InvocationOnMock invocation) throws Throwable {
BatchWriteRequest request = invocation.getArgument(0, BatchWriteRequest.class);
LOG.debug("request = {}", request);
long requestDurationMs = 0;
BatchWriteResponse.Builder builder = BatchWriteResponse.newBuilder();
for (Write ignored : request.getWritesList()) {
builder.addWriteResults(WriteResult.newBuilder().build());
if (clock.prev.isBefore(threshold)) {
requestDurationMs += defaultDocumentWriteLatency;
builder.addStatus(STATUS_OK);
} else {
int latency = rand.nextInt(1500);
LOG.debug("latency = {}", latency);
if (latency > 300) {
builder.addStatus(STATUS_DEADLINE_EXCEEDED);
} else {
builder.addStatus(STATUS_OK);
}
requestDurationMs += latency;
}
}
clock.setNext(advanceClockBy(Duration.millis(requestDurationMs)));
return builder.build();
}
});
LOG.info("### parameters: {defaultDocumentWriteLatency: {}, rpcQosOptions: {}}", defaultDocumentWriteLatency, options);
FnT fn = getFn(clock, ff, options, metricsFixture.counterFactory, metricsFixture.distributionFactory);
fn.setup();
fn.startBundle(startBundleContext);
while (writeCounter.get() < docCount) {
fn.processElement(processContext, window);
}
fn.finishBundle(finishBundleContext);
LOG.info("writeCounter = {}", writeCounter.get());
LOG.info("clock.prev = {}", clock.prev);
MyDistribution qosAdaptiveThrottlerThrottlingMs = metricsFixture.distributions.get("qos_adaptiveThrottler_throttlingMs");
assertNotNull(qosAdaptiveThrottlerThrottlingMs);
List<Long> updateInvocations = qosAdaptiveThrottlerThrottlingMs.updateInvocations;
assertFalse(updateInvocations.isEmpty());
}
use of org.mockito.Mock in project powermock by powermock.
the class AnnotationEnabler method standardInject.
private void standardInject(Object testInstance) throws IllegalAccessException {
Set<Field> fields = Whitebox.getFieldsAnnotatedWith(testInstance, getMockAnnotations());
for (Field field : fields) {
if (field.get(testInstance) != null) {
continue;
}
final Class<?> type = field.getType();
if (field.isAnnotationPresent(org.mockito.Mock.class)) {
org.mockito.Mock mockAnnotation = field.getAnnotation(org.mockito.Mock.class);
MockSettings mockSettings = withSettings();
Answers answers = mockAnnotation.answer();
if (answers != null) {
mockSettings.defaultAnswer(answers);
}
Class<?>[] extraInterfaces = mockAnnotation.extraInterfaces();
if (extraInterfaces != null && extraInterfaces.length > 0) {
mockSettings.extraInterfaces(extraInterfaces);
}
String name = mockAnnotation.name();
if (name != null && name.length() > 0) {
mockSettings.name(name);
}
field.set(testInstance, mock(type, mockSettings));
} else {
field.set(testInstance, mock(type));
}
}
}
Aggregations