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
[email protected]
https://lists.sourceforge.net/lists/listinfo/geoserver-devel

Reply via email to