[infinispan-dev] eventually...

Mircea Markus mircea.markus at jboss.com
Thu Sep 2 06:31:58 EDT 2010


Hi,

While testing, especially on async behaviour, one needs to wait until a certain state is reached.
E.g. let's say we want to test _async_ repl works fine,  one way to do it is:

1. first approach
cache1.put(k,v);

//now expect this to be replicated at some point in future on cache2
for (int i=0; i < 10; i++) { 
	if (v.equals(cache2.get(k)) break;
	Thread.sleep(1000);
}
assert v.equals(cache2.get(k));

the code bellow waits 10 seconds for the replication to occur, if not fails the test.
Problem with this code is that it is rather hard to read, and not reusable. 
This code can be found in many paces in our tests.

2. second approach.
I've added AbstractInfinispanTest.eventually(Condition ec) method that does the same.
Code bellow now looks like:

         eventually(new Condition() {
            public boolean isSatisfied() throws Exception {
               return v.equals(cache2.get(k));
            }
         });
While the amount of code itself hasn't change significantly, the second approach is more readable and much more quicker to write as it is generated by the IDE.

I've added this in 4.2, let me know your thoughts around it.

Cheers,
Mircea

 






More information about the infinispan-dev mailing list