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 ([EMAIL PROTECTED]) IBM Java technology centre, UK. --------------------------------------------------------------------- Terms of use : http://incubator.apache.org/harmony/mailing.html To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]