[Geoserver-devel] writing an thread with concurrent notification

I spent a bit of time today figuring out something I should of done a while ago … how to make a good clean concurrent notification test.

Here is the test snippet:

AssertResourceListener listenerD = new AssertResourceListener();
store.addListener( “DirC/FileD”, listenerD );
Thread.sleep(1000);
d.setLastModified(System.currentTimeMillis());
listenerD.await();

Normally I would handle this with a while loop, that wakes up and checks if the requested event has come in yet.

Here is today’s alternative (using Lock and a Condition):

static class AssertResourceListener implements ResourceListener {
Lock lock = new ReentrantLock(true);
Condition notified = lock.newCondition();
private ResourceNotification notify = null;
@Override
public void changed(ResourceNotification notify) {
if ( notify != null ){
lock.lock();
try {
this.notify = notify;
notified.signalAll();
}
finally {
lock.unlock();
}
}
}
public void await() throws InterruptedException {
lock.lock();
try {
while (notify == null){
notified.await();
}
}
finally {
lock.unlock();
}
}
}

The result is a faster build and more reliable tests.

Jody Garnett

Hi Jody,
looks cool, do you have the whole test using it handy?

Cheers
Andrea

···

On Tue, Mar 25, 2014 at 11:25 AM, Jody Garnett <jody.garnett@anonymised.com> wrote:

I spent a bit of time today figuring out something I should of done a while ago … how to make a good clean concurrent notification test.

Here is the test snippet:

AssertResourceListener listenerD = new AssertResourceListener();
store.addListener( “DirC/FileD”, listenerD );
Thread.sleep(1000);
d.setLastModified(System.currentTimeMillis());
listenerD.await();

Normally I would handle this with a while loop, that wakes up and checks if the requested event has come in yet.

Here is today’s alternative (using Lock and a Condition):

static class AssertResourceListener implements ResourceListener {
Lock lock = new ReentrantLock(true);
Condition notified = lock.newCondition();
private ResourceNotification notify = null;
@Override
public void changed(ResourceNotification notify) {
if ( notify != null ){
lock.lock();
try {
this.notify = notify;
notified.signalAll();
}
finally {
lock.unlock();
}
}
}
public void await() throws InterruptedException {
lock.lock();
try {
while (notify == null){
notified.await();
}
}
finally {
lock.unlock();
}
}
}

The result is a faster build and more reliable tests.

Jody Garnett


Learn Graph Databases - Download FREE O’Reilly Book
“Graph Databases” is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech


Geoserver-devel mailing list
Geoserver-devel@anonymised.comsts.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

==
Meet us at GEO Business 2014! in London! Visit http://goo.gl/fES3aK
for more information.

Ing. Andrea Aime

@geowolf
Technical Lead

GeoSolutions S.A.S.
Via Poggio alle Viti 1187
55054 Massarosa (LU)
Italy
phone: +39 0584 962313
fax: +39 0584 1660272
mob: +39 339 8844549

http://www.geo-solutions.it
http://twitter.com/geosolutions_it


It will be part of my next pull request, going to work on it a bit more so the test fails if the expected event does not arrive within a time limit. But yeah so far I like the approach.

···

Jody Garnett

On Wed, Mar 26, 2014 at 11:03 PM, Andrea Aime <andrea.aime@anonymised.com> wrote:

Hi Jody,
looks cool, do you have the whole test using it handy?

Cheers
Andrea

On Tue, Mar 25, 2014 at 11:25 AM, Jody Garnett <jody.garnett@anonymised.com.> wrote:

I spent a bit of time today figuring out something I should of done a while ago … how to make a good clean concurrent notification test.

Here is the test snippet:

AssertResourceListener listenerD = new AssertResourceListener();
store.addListener( “DirC/FileD”, listenerD );
Thread.sleep(1000);
d.setLastModified(System.currentTimeMillis());
listenerD.await();

Normally I would handle this with a while loop, that wakes up and checks if the requested event has come in yet.

Here is today’s alternative (using Lock and a Condition):

static class AssertResourceListener implements ResourceListener {
Lock lock = new ReentrantLock(true);
Condition notified = lock.newCondition();
private ResourceNotification notify = null;
@Override
public void changed(ResourceNotification notify) {
if ( notify != null ){
lock.lock();
try {
this.notify = notify;
notified.signalAll();
}
finally {
lock.unlock();
}
}
}
public void await() throws InterruptedException {
lock.lock();
try {
while (notify == null){
notified.await();
}
}
finally {
lock.unlock();
}
}
}

The result is a faster build and more reliable tests.

Jody Garnett


Learn Graph Databases - Download FREE O’Reilly Book
“Graph Databases” is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech


Geoserver-devel mailing list
Geoserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

==
Meet us at GEO Business 2014! in London! Visit http://goo.gl/fES3aK
for more information.

Ing. Andrea Aime

@geowolf
Technical Lead

GeoSolutions S.A.S.
Via Poggio alle Viti 1187
55054 Massarosa (LU)
Italy
phone: +39 0584 962313
fax: +39 0584 1660272
mob: +39 339 8844549

http://www.geo-solutions.it
http://twitter.com/geosolutions_it


Here (finally) is the whole test (see line 178):

https://github.com/geoserver/geoserver/blob/master/src/platform/src/test/java/org/geoserver/platform/resource/FileSystemResourceTheoryTest.java#L178

I would like to move this Await class to somewhere useful for testing. I guess that is gs-main/src/test/Java directory … does not help me test platform.

···

Jody Garnett

On Wed, Mar 26, 2014 at 11:03 PM, Andrea Aime <andrea.aime@anonymised.com> wrote:

Hi Jody,
looks cool, do you have the whole test using it handy?

Cheers
Andrea

On Tue, Mar 25, 2014 at 11:25 AM, Jody Garnett <jody.garnett@anonymised.com.> wrote:

I spent a bit of time today figuring out something I should of done a while ago … how to make a good clean concurrent notification test.

Here is the test snippet:

AssertResourceListener listenerD = new AssertResourceListener();
store.addListener( “DirC/FileD”, listenerD );
Thread.sleep(1000);
d.setLastModified(System.currentTimeMillis());
listenerD.await();

Normally I would handle this with a while loop, that wakes up and checks if the requested event has come in yet.

Here is today’s alternative (using Lock and a Condition):

static class AssertResourceListener implements ResourceListener {
Lock lock = new ReentrantLock(true);
Condition notified = lock.newCondition();
private ResourceNotification notify = null;
@Override
public void changed(ResourceNotification notify) {
if ( notify != null ){
lock.lock();
try {
this.notify = notify;
notified.signalAll();
}
finally {
lock.unlock();
}
}
}
public void await() throws InterruptedException {
lock.lock();
try {
while (notify == null){
notified.await();
}
}
finally {
lock.unlock();
}
}
}

The result is a faster build and more reliable tests.

Jody Garnett


Learn Graph Databases - Download FREE O’Reilly Book
“Graph Databases” is the definitive new guide to graph databases and their
applications. Written by three acclaimed leaders in the field,
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/13534_NeoTech


Geoserver-devel mailing list
Geoserver-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

==
Meet us at GEO Business 2014! in London! Visit http://goo.gl/fES3aK
for more information.

Ing. Andrea Aime

@geowolf
Technical Lead

GeoSolutions S.A.S.
Via Poggio alle Viti 1187
55054 Massarosa (LU)
Italy
phone: +39 0584 962313
fax: +39 0584 1660272
mob: +39 339 8844549

http://www.geo-solutions.it
http://twitter.com/geosolutions_it