harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tim Ellison <t.p.elli...@gmail.com>
Subject Re: [classlib][nio] How to deal with unstable but useful tests?
Date Tue, 11 Jul 2006 07:32:48 GMT
Andrew Zhang wrote:
> Hello everybody,
> 
> I noticed such tests in DatagramChannel, which is useful, but potentially
> unstable.  Consider:
> 
> public void testReadWrite_configureBlock() throws Exception {
>        byte[] targetArray = new byte[2];
>        // bind and connect
>        this.channel1.socket().bind(localAddr2);
>        this.channel1.connect(localAddr1);
>        this.channel2.socket().bind(localAddr1);
>        this.channel2.connect(localAddr2);
>        ByteBuffer targetBuf = ByteBuffer.wrap(targetArray);
> 
>        new Thread() {
>            public void run() {
>                try {
>                    Thread.sleep(TIME_UNIT);
>                    channel1.configureBlocking(false); // Label 1
>                    channel1.close(); // Label 2
>                } catch (Exception e) {
>                    //ignore
>                }
>            }
>        }.start();
>        try {
>            this.channel1.read(targetBuf); // Label 3
>            fail("should throw AsynchronousCloseException");
>        } catch (AsynchronousCloseException e) {
>            // ok
>        }
>    }
> This test assumes Label 3  code should execute before Label 1 and Label 2,
> which is right in most cases, because the code invokes "Thread.sleep
> (TIME_UNIT)".
> 
> However, it's potentially unstable. It heavily depends on TIME_UNIT value,
> test environment (Hardware, OS ...)

Urgh.  There are *very* few occasions when you need to use
Thread.sleep(); and 'thread synchronization' is definitely not one of them!

If you have order dependencies between two or more threads then use
wait/notify, or synchronized, and make them explicit.

There are any number of books on multi-threaded programming in Java.

> Indubitably, the test is very useful for testing
> "AsynchronousCloseException" of DatagramChannel.read(ByteBuffer) method.
> 
> How shall we deal with such issue?  Deleting such valuable tests is not a
> wise decision, while keeping them may cause build system fail.
> 
> One solution I could image is TestNG. :) i.e. Use "@dev" or something to
> mark such tests, say, the tests are only for developing purpose.
> 
> Any suggestions?

Fix it!  :-)

Regards,
Tim

-- 

Tim Ellison (t.p.ellison@gmail.com)
IBM Java technology centre, UK.

---------------------------------------------------------------------
Terms of use : http://incubator.apache.org/harmony/mailing.html
To unsubscribe, e-mail: harmony-dev-unsubscribe@incubator.apache.org
For additional commands, e-mail: harmony-dev-help@incubator.apache.org


Mime
View raw message