When I started working as a Java Developer, me and my teammate got a first task to repair all broken tests (great task for new starters!) in some old project. Replacing some old configuration and upgrading a few libraries helped making the tests status green but there was another problem. End-to-end test suite took a few hours to pass making it impossible to get the feedback for a new change quickly. Sometimes build run for a few hours and hanged so E2E tetst became excluded from the feedback loop at the end. 
Most of the tests from that suite did the same thing: 
  • Invoke a few commands on the mainframe through HTTP interface
  • Sleep for 30 seconds (probably just to be extra safe) for each command and check if the result was persisted to database by another system. 
It turned out, that first try to reduce the sleep time to 15 seconds by my teammate, didn’t make any tests fail, so that was a nice first try. But what was the optimal wait time for each test?

After some time, we tried another approach. If the flow has a asynchronous nature then why not testing it asynchronously. 
I introduced this simple class (or at least class which does the same): 
public class Await {
    private static final BiPredicate<Long, Long> notTimedOut =
            (start, timeoutMillis) -> System.currentTimeMillis() - start < timeoutMillis;
    public static void await(Supplier<AssertionResult> assertionVerifier, long timeoutMillis) {
        final long startTime = System.currentTimeMillis();
        Stream.generate(assertionVerifier)
                .takeWhile(result -> notTimedOut.test(startTime, timeoutMillis) || fail(result))
                .filter(AssertionResult::isOK)
                .findFirst();
    }
    public static boolean fail(AssertionResult result) {
        throw new RuntimeException("Assertion not passed after timeout, problem: " + result.optionalMessage);
    }
}

Method await checks some assertion in a loop until either it won’t throw AssertionError or the timeout is passed. This assertion is usually some state we are waiting for that should be ready in the nearest future. If it is not ready after the timeout, the last message from the AssertionError will be thrown wrapped as a RuntimeException. This greatly reduces test time because if the result is ready earlier, we don’t have to spend unnecessary time in the sleep method.

To represent the assertion result we can use classes (minimal implementation):
enum Status { OK, ERROR }
class AssertionResult {
    public static final AssertionResult OK = new AssertionResult(Status.OK, "");
    public final Status status;
    public final String optionalMessage;
    private AssertionResult(Status status, String optionalMessage) {
        this.status = status;
        this.optionalMessage = optionalMessage;
    }
    public static AssertionResult error(String optionalMessage) {
        return new AssertionResult(Status.ERROR, optionalMessage);
    }
    public boolean isOK() {
        return status == Status.OK;
    }
}

We also need to prepare our AssertionResult suppliers - wrappers which fetch the data and invoke assertions on the result. Usually it is possible to define generic supplier if the way we fetch the data is similar. For example we can write this builder method:
public static Supplier<AssertionResult> dbDataAssertion(Consumer<String> userAssertion, long userId) {
    return () -> {
        String usernameFromDB = db.getUserName(userId); //or any other more generic version
        try {
            userAssertion.accept(usernameFromDB);
            return AssertionResult.OK;
        } catch (AssertionError assertionError) {
            return AssertionResult.error(assertionError.getMessage());
        }
    };
}

We are ready to replace Thread.sleep() with call to Await in our tests:
...
long timeoutMillis = 1000L;
long userId = 1234L;
...
//then
Await.await(
        dbDataAssertion(userName -> assertEquals(4, userName.length()), userId),
        timeoutMillis);

Conclusion


Don’t overuse Thread.sleep() in your test code. According to this Google research it is one of the main reasons of flaky tests. Asynchronous approach may complicate your tests but the time savings can be tremendous.  If you are looking for some existing implementations there is a nice class in Spock which does the same.
0

Add a comment

I have recently started implementing different distributed system protocols to get some understanding how they work. I think that using Akka Actors to simulate hosts is a good choice because they are easy to set up. What is more you can kill actors on demand to test some failure scenarios.

First protocol I implemented is Paxos - probably the most important consensus protocol, which guarantees storng consistecy, full transactions, read/write failover and prevents from data loss. This comes with the cost of higher latency and lower throughput. In terms of CAP theorem, Paxos is used to make your system CP. I used this sweet article as a reference.

Implementation can be found on my github repository. Go to the Test.scala and run some simulations.

When I started working as a Java Developer, me and my teammate got a first task to repair all broken tests (great task for new starters!) in some old project. Replacing some old configuration and upgrading a few libraries helped making the tests status green but there was another problem. End-to-end test suite took a few hours to pass making it impossible to get the feedback for a new change quickly.

In this article I am going to share some cool features I stumbled upon while coding with Intellij. These are not the most popular/productivity improving ones - for these you should watch this video. 

1. Set debugger breakpoint by pattern

Problem: You want to check if the debugger steps into some code during the run. Usually you suspect where the flow will end up but sometimes finding the exact line could be hard.

I have recently pushed very simple Map Reduce concept implementation on my Github account (click). My idea was to focus on the concept and mock the rest.

In this article I will try to map methods of Java’s Optional to Kotlin’ssimilar scattered language features and built-in functions. The code in the examples is written in Kotlin, because the language has all the JDK classes available.

Representation

Let’s start with the representation.

Have you ever scrolled someone’s code and bumped into this weird method called flatMap, not knowing what it actually does from the context? Or maybe you compared it with method map but didn’t really see much difference? If that is the case then this article is for you. 

flatMap is extremly usfull when you try to do proper functional programming. In Java it usually means using Streams and Optionals - concepts introduced in version 8.

Fact - End-to-end  tests are critical if you want to make sure your software works as it should. To be 100% sure that you covered every (or almost every) possible branch in your business code, it is worth to check what code has been invoked after your E2E suite finished successfully. The solution I am going to present may be much easier, if you are able to stand up your application locally. Sometimes though, it is not the case.

Functional Programming in Java

Stream and Optional classes - added to Java 8 - allow you to have some fun with functional programming. The problem is Java still misses quite a lot to be taken as a serious FP language. Lambda notation and two monads (Optional and Stream) are just the tip of the iceberg. This leads to arising of libraries like vavr or functionaljava - both deriving from purely functional language Haskell.

In this article, I am going to present you a simple trick that will make using java.util.function.Function.andThen() more useful. 

As an example I will use ExternalSystemGateway class, which job is to call external system along with serializing/mapping the messages:

You can see that every line of the invoke method does some kind of action, which transforms some input type to another output type.
1

Checked exceptions & Java 8

Defining custom exceptions (both checked and unchecked) is a common approach to handling errors in Java applications. It usually leads to creating a new class for every different type of error, marking methods with throws keyword or wrapping code with try-catch blocks. This can lead to the code which is hard to read since every block adds another level of complexity. 

Lambdas in Java 8 started the boom for functional approach in writing the code.
1
Popular Posts
Popular Posts
About Me
About Me
Labels
Blog Archive
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.