<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
</head>
<body text="#000000" bgcolor="#ffffff">
1) the scanner's time doesn't really need to be part of the unit
tests, as that can be tested separately and we know it'll work. So
we can just call scan() instead. See code in 2)<br>
<br>
2) I've also moved it to use a CountDownLatch on a
afterChangeSetApplied event.<br>
private void scan(KnowledgeAgent kagent) {<br>
// Calls the Resource Scanner and sets up a listener and a
latch so we can wait until it's finished processing, instead of
using timers<br>
final CountDownLatch latch = new CountDownLatch( 1 );<br>
<br>
KnowledgeAgentEventListener l = new
KnowledgeAgentEventListener() {<br>
...<br>
<br>
public void
afterChangeSetApplied(AfterChangeSetAppliedEvent event) {<br>
latch.countDown();<br>
}<br>
}; <br>
<br>
kagent.addEventListener( l );<br>
<br>
this.scanner.scan();<br>
<br>
try {<br>
latch.await( 10, TimeUnit.SECONDS );<br>
} catch ( InterruptedException e ) {<br>
throw new RuntimeException( "Unable to wait for latch
countdown", e);<br>
}<br>
<br>
if ( latch.getCount() > 0 ) { <br>
throw new RuntimeException( "Event for KnowlegeBase
update, due to scan, was never received" );<br>
}<br>
<br>
kagent.removeEventListener( l );<br>
}<br>
<br>
3) All file deletes go through the FileManager. I always do multiple
attempts at deleting a file and throw an exception if it was not
successfuly<br>
public boolean deleteFile(File file) {<br>
// This will attempt to delete a file 5 times, calling GC
and Sleep between each iteration<br>
// Sometimes windows takes a while to release a lock on a
file<br>
if ( !file.delete() ) {<br>
int count = 0;<br>
while ( !file.delete() && count++ < 5 ) {<br>
System.gc();<br>
try {<br>
Thread.sleep( 250 );<br>
} catch ( InterruptedException e ) {<br>
throw new RuntimeException( "This should never
happen" );<br>
}<br>
}<br>
}<br>
<br>
return !file.exists();<br>
<br>
}<br>
<br>
3) File writting always waits 1000ms when the file already exists
(http and linux round to nearest second) so it ensures lastmodified
is always atleast 1000ms after lastread. See code in 4)<br>
<br>
4) All file writting goes via the FileManager where I know attempt
to write any file 5 times, calling GC and wait between each
iteration.<br>
public void write(File f,<br>
String text) throws IOException {<br>
if ( f.exists() ) {<br>
// we want to make sure there is a time difference for
lastModified and lastRead checks as Linux and http often round to
seconds<br>
//
<a class="moz-txt-link-freetext" href="http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=1&t=019789">http://saloon.javaranch.com/cgi-bin/ubb/ultimatebb.cgi?ubb=get_topic&f=1&t=019789</a><br>
try {<br>
Thread.sleep( 1000 );<br>
} catch ( Exception e ) {<br>
throw new RuntimeException( "Unable to sleep" );<br>
}<br>
}<br>
<br>
// Attempt to write the file<br>
BufferedWriter output = new BufferedWriter( new FileWriter(
f ) );<br>
output.write( text );<br>
output.close();<br>
<br>
// Now check the file was written and re-attempt if it was
not <br>
// Need to do this for testing, to ensure the texts are read
the same way, otherwise sometimes you get tail \n sometimes you
don't<br>
String t1 = StringUtils.toString( new StringReader( text )
);<br>
<br>
int count = 0;<br>
while ( !t1.equals( StringUtils.toString( new
BufferedReader( new FileReader( f ) ) ) ) && count < 5 )
{<br>
// The file failed to write, try 5 times, calling GC and
sleep between each iteration<br>
// Sometimes windows takes a while to release a lock on
a file <br>
System.gc();<br>
try {<br>
Thread.sleep( 250 );<br>
} catch ( InterruptedException e ) {<br>
throw new RuntimeException( "This should never
happen" );<br>
}<br>
output = new BufferedWriter( new FileWriter( f ) );<br>
output.write( text );<br>
output.close();<br>
count++;<br>
}<br>
<br>
if ( count == 5 ) {<br>
throw new IOException( "Unable to write to file:" +
f.getCanonicalPath() );<br>
}<br>
}<br>
<br>
So far between those things the code is now more robust, it's just
taking time to move it all across :)<br>
<br>
We should probably make separate unit tests specifically for
ResourceScannerImpl, on it's own separate from the rest of hte
stack.<br>
<br>
Mark<br>
On 04/12/2010 13:26, Esteban Aliverti wrote:
<blockquote
cite="mid:AANLkTik-Q_ioTsei0EXyhBhWGAjJQyjosaKKiJx5Qdf7@mail.gmail.com"
type="cite">Mark, let me know if you need some help since this is
one of the task in my (almost eternal) TODO list. You can take a
look at KnowledgeAgentEventListenerTest.java. Using
KnowledgeAgentEventListener, you can be notified when the rule
base is recreated and there is no need to wait for x seconds
anymore.
<div>
Take a look at the waitUntilChangeSetApplied() method there.</div>
<div><br>
</div>
<div>Best,<br clear="all">
<br>
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX<br>
<br>
Esteban Aliverti<br>
- Developer @ <a moz-do-not-send="true"
href="http://www.plugtree.com" target="_blank">http://www.plugtree.com
</a><br>
- Blog @ <a moz-do-not-send="true"
href="http://ilesteban.wordpress.com" target="_blank">http://ilesteban.wordpress.com</a><br>
<br>
<br>
<div class="gmail_quote">On Sat, Dec 4, 2010 at 6:38 AM, Mark
Proctor <span dir="ltr"><<a moz-do-not-send="true"
href="mailto:mproctor@codehaus.org">mproctor@codehaus.org</a>></span>
wrote:<br>
<blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt
0.8ex; border-left: 1px solid rgb(204, 204, 204);
padding-left: 1ex;">
The KnowlegeAgent tests have been fragile for a while,
related to "wait"<br>
code and file issues, particularly on windows. Something has
changed<br>
recently and now they aren't working on the linux hudson
server either.<br>
<br>
So I'm now in the process of refactoring the tests to make
them more<br>
robust, and they will hopefully run faster too, as I'll use
call backs<br>
when something has finished instead of waiting for X seconds
in the hope<br>
that something has finished.<br>
<br>
Mark<br>
<br>
_______________________________________________<br>
rules-dev mailing list<br>
<a moz-do-not-send="true"
href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a><br>
<a moz-do-not-send="true"
href="https://lists.jboss.org/mailman/listinfo/rules-dev"
target="_blank">https://lists.jboss.org/mailman/listinfo/rules-dev</a><br>
</blockquote>
</div>
<br>
</div>
<pre wrap="">
<fieldset class="mimeAttachmentHeader"></fieldset>
_______________________________________________
rules-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:rules-dev@lists.jboss.org">rules-dev@lists.jboss.org</a>
<a class="moz-txt-link-freetext" href="https://lists.jboss.org/mailman/listinfo/rules-dev">https://lists.jboss.org/mailman/listinfo/rules-dev</a>
</pre>
</blockquote>
<br>
</body>
</html>