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

Remove hamcrest dependency from styx common production code. #478

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,6 +24,7 @@

import static com.hotels.styx.common.MorePreconditions.checkArgument;
import static com.hotels.styx.common.MorePreconditions.checkNotEmpty;
import static java.lang.String.format;

/**
* A factory for creating Epoll-based client event loops.
Expand All @@ -39,8 +40,10 @@ public class EpollClientEventLoopGroupFactory implements ClientEventLoopFactory
* @param clientWorkerThreadsCount number of threads
*/
public EpollClientEventLoopGroupFactory(String name, int clientWorkerThreadsCount) {
this.name = checkNotEmpty(name);
this.clientWorkerThreadsCount = checkArgument(clientWorkerThreadsCount, clientWorkerThreadsCount > -1);
this.name = checkNotEmpty(name, "Event loop group name is null or empty");
this.clientWorkerThreadsCount = checkArgument(clientWorkerThreadsCount,
clientWorkerThreadsCount > -1,
format("Invalid client worker thread count (%d). It must be > -1.", clientWorkerThreadsCount));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.hotels.styx.client.origincommands.DisableOrigin;
import com.hotels.styx.client.origincommands.EnableOrigin;
import com.hotels.styx.support.matchers.LoggingTestSupport;
import org.hamcrest.Matcher;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand All @@ -44,7 +43,6 @@
import static com.hotels.styx.api.extension.service.ConnectionPoolSettings.defaultConnectionPoolSettings;
import static com.hotels.styx.client.OriginsInventory.OriginState.ACTIVE;
import static com.hotels.styx.client.OriginsInventory.OriginState.DISABLED;
import static com.hotels.styx.common.testing.matcher.TransformingMatcher.hasDerivedValue;
import static com.hotels.styx.support.matchers.ContainsExactlyOneMatcher.containsExactlyOne;
import static com.hotels.styx.support.matchers.IsOptional.isAbsent;
import static com.hotels.styx.support.matchers.IsOptional.isValue;
Expand Down Expand Up @@ -93,7 +91,7 @@ public void stop() {
public void startsMonitoringNewOrigins() {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));

Expand All @@ -110,14 +108,14 @@ public void updatesOriginPortNumber() throws Exception {

inventory.setOrigins(originV1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE),is(1));
verify(monitor).monitor(singleton(originV1));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
verify(eventBus).post(any(OriginsSnapshot.class));

inventory.setOrigins(originV2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(originV1));
verify(monitor).monitor(singleton(originV2));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
Expand All @@ -131,14 +129,14 @@ public void updatesOriginHostName() throws Exception {

inventory.setOrigins(originV1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).monitor(singleton(originV1));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
verify(eventBus).post(any(OriginsSnapshot.class));

inventory.setOrigins(originV2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(originV1));
verify(monitor).monitor(singleton(originV2));
assertThat(gaugeValue("origins.generic-app.acme-01.status"), isValue(1));
Expand Down Expand Up @@ -187,7 +185,7 @@ public void shutsConnectionPoolForModifiedOrigin() {
public void ignoresUnchangedOrigins() throws Exception {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
Expand All @@ -196,7 +194,7 @@ public void ignoresUnchangedOrigins() throws Exception {

inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor, times(1)).monitor(singleton(ORIGIN_1));
verify(monitor, times(1)).monitor(singleton(ORIGIN_2));
verify(eventBus).post(any(OriginsSnapshot.class));
Expand All @@ -206,7 +204,7 @@ public void ignoresUnchangedOrigins() throws Exception {
public void removesOrigin() throws Exception {
inventory.setOrigins(ORIGIN_1, ORIGIN_2);

assertThat(inventory, hasActiveOrigins(2));
assertThat(inventory.originCount(ACTIVE), is(2));
verify(monitor).monitor(singleton(ORIGIN_1));
verify(monitor).monitor(singleton(ORIGIN_2));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
Expand All @@ -215,7 +213,7 @@ public void removesOrigin() throws Exception {

inventory.setOrigins(ORIGIN_2);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isAbsent());
assertThat(gaugeValue("origins.generic-app.app-02.status"), isValue(1));
Expand Down Expand Up @@ -268,7 +266,7 @@ public void willNotDisableOriginsNotBelongingToTheApp() {

inventory.onCommand(new DisableOrigin(id("some-other-app"), ORIGIN_1.id()));

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(eventBus).post(any(OriginsSnapshot.class));
}

Expand All @@ -279,7 +277,7 @@ public void willNotEnableOriginsNotBelongingToTheApp() {
inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));
inventory.onCommand(new EnableOrigin(id("some-other-app"), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory.originCount(ACTIVE), is(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

Expand All @@ -289,8 +287,8 @@ public void disablingAnOriginRemovesItFromActiveSetAndStopsHealthCheckMonitoring

inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory, hasDisabledOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(inventory.originCount(DISABLED), is(1));

verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(-1));
Expand All @@ -304,8 +302,8 @@ public void disablingAnOriginRemovesItFromInactiveSetAndStopsHealthCheckMonitori
inventory.originUnhealthy(ORIGIN_1);
inventory.onCommand(new DisableOrigin(ORIGIN_1.applicationId(), ORIGIN_1.id()));

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory, hasDisabledOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(inventory.originCount(DISABLED), is(1));

verify(monitor).stopMonitoring(singleton(ORIGIN_1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(-1));
Expand All @@ -327,51 +325,51 @@ public void enablingAnOriginWillReInitiateHealthCheckMonitoring() {
@Test
public void removesUnhealthyOriginsFromActiveSet() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);

assertThat(inventory, hasNoActiveOrigins());
assertThat(inventory.originCount(ACTIVE), is(0));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

@Test
public void putsHealthyOriginsBackIntoActiveSet() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
assertThat(gaugeValue("origins.generic-app.app-01.status"), isValue(1));
verify(eventBus, times(3)).post(any(OriginsSnapshot.class));
}

@Test
public void reportingUpRepeatedlyDoesNotAffectCurrentActiveOrigins() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originHealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);
inventory.originHealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));
verify(eventBus, times(1)).post(any(OriginsSnapshot.class));
}

@Test
public void reportingDownRepeatedlyDoesNotAffectCurrentActiveOrigins() {
inventory.setOrigins(ORIGIN_1);
assertThat(inventory, hasActiveOrigins(1));
assertThat(inventory.originCount(ACTIVE), is(1));

inventory.originUnhealthy(ORIGIN_1);
inventory.originUnhealthy(ORIGIN_1);
inventory.originUnhealthy(ORIGIN_1);

assertThat(inventory, hasActiveOrigins(0));
assertThat(inventory.originCount(ACTIVE), is(0));
verify(eventBus, times(2)).post(any(OriginsSnapshot.class));
}

Expand Down Expand Up @@ -475,22 +473,6 @@ public void stopsMonitoringAndUnregistersWhenClosed() {
verify(eventBus).unregister(eq(inventory));
}

private static Matcher<OriginsInventory> hasNoActiveOrigins() {
return hasActiveOrigins(0);
}

private static Matcher<OriginsInventory> hasActiveOrigins(int numberOfActiveOrigins) {
return hasOrigins(numberOfActiveOrigins, ACTIVE);
}

private static Matcher<OriginsInventory> hasDisabledOrigins(int numberOfDisabledOrigins) {
return hasOrigins(numberOfDisabledOrigins, DISABLED);
}

private static Matcher<OriginsInventory> hasOrigins(int numberOfOrigins, OriginsInventory.OriginState state) {
return hasDerivedValue(inventory -> inventory.originCount(state), is(numberOfOrigins));
}

private Optional<Integer> gaugeValue(String name) {
return gauge(name)
.map(Gauge::getValue)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright (C) 2013-2018 Expedia Inc.
Copyright (C) 2013-2019 Expedia Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -15,15 +15,7 @@
*/
package com.hotels.styx.common;

import com.google.common.base.Preconditions;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.StringDescription;

import static com.google.common.base.Strings.isNullOrEmpty;
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.hamcrest.Matchers.lessThanOrEqualTo;

/**
* Static convenience methods that help a method or constructor check whether it was invoked
Expand All @@ -37,8 +29,8 @@ public final class MorePreconditions {
* @return the same string if non-empty
* @throws NullPointerException if {@code value} is null or empty
*/
public static String checkNotEmpty(String value) {
Preconditions.checkArgument(!isNullOrEmpty(value));
public static String checkNotEmpty(String value, String message) {
checkArgument(value, !isNullOrEmpty(value), message);
return value;
}

Expand All @@ -51,61 +43,13 @@ public static String checkNotEmpty(String value) {
* @return the same reference passed in (if {@code expression} is true)
* @throws IllegalArgumentException if {@code expression} is false
*/
public static <T> T checkArgument(T reference, boolean expression) {
Preconditions.checkArgument(expression);
return reference;
}


/**
* Ensures that an object reference passed as a parameter to the calling method satisfies
* the condition specified by the matcher.
*
* @param reference an object reference
* @param condition the condition to be satisfied
* @param <T> reference type
* @return the same reference passed in
* @throws IllegalArgumentException if {@code condition} is false
*/
public static <T> T checkArgument(T reference, Matcher<T> condition) {
return checkArgument(reference, condition, "argument");
}

/**
* Ensures that an object reference passed as a parameter to the calling method satisfies
* the condition specified by the matcher.
*
* @param reference an object reference
* @param condition the condition to be satisfied
* @param referenceName the name of the argument to use in the error description
* @param <T> reference type
* @return the same reference passed in
* @throws IllegalArgumentException if {@code condition} is false
*/
public static <T> T checkArgument(T reference, Matcher<T> condition, String referenceName) {
if (!condition.matches(reference)) {
Description description = new StringDescription();
description.appendText(referenceName)
.appendText("\nExpected: ")
.appendDescriptionOf(condition)
.appendText("\n but: ");
condition.describeMismatch(reference, description);
throw new IllegalArgumentException(description.toString());
public static <T> T checkArgument(T reference, boolean expression, String message) {
if (!expression) {
throw new IllegalArgumentException(message);
}
return reference;
}

/**
* A matcher that checks whether an integer is between a given minimum and maximum (inclusive).
*
* @param minimum minimum value (inclusive)
* @param maximum maximum value (inclusive)
* @return the matcher
*/
public static Matcher<Integer> inRange(int minimum, int maximum) {
return allOf(greaterThanOrEqualTo(minimum), lessThanOrEqualTo(maximum));
}

private MorePreconditions() {
}
}
Loading