Skip to content

Commit

Permalink
Merge pull request #51 from palantir/feature/enable-extra-way-of-gene…
Browse files Browse the repository at this point in the history
…rating-success-or-failure

Generate success or failure from lambda
  • Loading branch information
joelea committed Apr 14, 2016
2 parents 54ca750 + 3808b92 commit 7daa2d9
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2016 Palantir Technologies, Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.palantir.docker.compose.connection.waiting;

@FunctionalInterface
public interface Attempt {
boolean attempt() throws Exception;
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

@Value.Immutable
public abstract class SuccessOrFailure {
public static SuccessOrFailure onResultOf(Attempt attempt) {
try {
return fromBoolean(attempt.attempt(), "Attempt to complete healthcheck failed");
} catch(Exception e) {
return fromException(e);
}
}

@Value.Parameter protected abstract Optional<String> optionalFailureMessage();

public static SuccessOrFailure success() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,30 @@ public void fail_from_an_exception() {
containsString("oh no")
))));
}

@Test
public void succeed_on_a_lambda_that_returns_true() {
SuccessOrFailure successFromLambda = SuccessOrFailure.onResultOf(() -> true);
assertThat(successFromLambda, is(successful()));
}

@Test
public void fail_on_a_lambda_that_throws_an_exception() {
SuccessOrFailure failureFromLambda = SuccessOrFailure.onResultOf(() -> {
throw new IllegalArgumentException("oh no");
});

assertThat(failureFromLambda,
is(failureWithMessage(both(
containsString("IllegalArgumentException")).and(
containsString("oh no")
))));
}

@Test
public void fail_on_a_lambda_that_returns_false() {
SuccessOrFailure failureFromLambda = SuccessOrFailure.onResultOf(() -> false);

assertThat(failureFromLambda, is(failureWithMessage("Attempt to complete healthcheck failed")));
}
}

0 comments on commit 7daa2d9

Please sign in to comment.