📄 tailsourcetest.java
字号:
package de.spieleck.app.jacson.source;
import junit.framework.*;
import java.io.*;
import java.util.*;
import org.xml.sax.InputSource;
import de.spieleck.config.Config;
import de.spieleck.config.ConfigNode;
import de.spieleck.util.ThreadUtil;
import de.spieleck.app.jacson.*;
import de.spieleck.app.jacson.report.TestReport;
/**
*/
public class TailSourceTest
extends TestCase
implements NamesForTesting, JacsonNames
{
public final static String CONFIG =
"<?xml version='1.0'?>"
+"<application>"
+ "<jacson>"
+ "<eval se:this='LoggingEvaluator'/>"
+ "<report se:this='ValuePrintingReport'>"
//+ "<debug>true</debug>"
+ "<section>log/chunk</section>"
+ "</report>"
+ "</jacson>"
+"</application>"
;
protected JacsonState state;
public TailSourceTest(String testName)
{
super(testName);
}
public void setUp()
{
state = new JacsonState();
}
public void tearDown()
{
state = null; // Not really essential
}
public void test_5_2_1_1000()
throws Exception
{
internalTest(5,2,1, 1000);
}
/** Was 6_3_0 but it turned out that my notebook didn't have
* filesystem fast enough to cope with "No delay".
*/
public void test_6_3_1_10000()
throws Exception
{
internalTest(6,3,1, 10000);
}
protected void internalTest(int sources, int threads, int delay, int count)
throws Exception
{
ConfigNode config = null;
JacsonState rootState = new JacsonState();
InputSource is = new InputSource(
new StringBufferInputStream(CONFIG));
config = Config.parse(is, rootState);
config = config.node(JS_MAIN_CFG);
if ( config == null )
fail("missing "+JS_MAIN_CFG+" section in config.");
Jacson stat = null;
stat = new Jacson(config, rootState);
TestReport tr = new TestReport();
stat.addReport(tr);
TailSource ts = new TailSource();
ts.registerState(rootState);
ts.setWorkers(threads);
File[] files = new File[sources];
PrintWriter[] wrs = new PrintWriter[sources];
int i;
for(i = 0; i < sources; i++)
{
files[i] = File.createTempFile("tmp", "tailsource");
wrs[i] = new PrintWriter(new FileOutputStream(files[i]),true);
ts.addWatch(Integer.toString(i), files[i].getAbsolutePath());
}
Thread th = new Thread(new StatRunner(stat, ts));
th.start();
for(int j = 0; j < count; j++)
{
int ch = (int) (Math.random() * sources);
String line = "This is line <"+j+">!";
wrs[ch].println(line);
wrs[ch].flush();
ThreadUtil.sleep(delay);
}
for(i = 0; i < sources; i++)
{
wrs[i].flush();
wrs[i].close();
ThreadUtil.sleep(delay);
}
// The problem is, that Java is so much faster, than an Filesystem,
// the one join could still observer "nothing to do any more" and
// we are still not done!
// One could argue this is a bug of the system, but I rather consider
// it a flaw of the test. Maybe we should have taken pipes...
ThreadUtil.sleep(1000);
ts.join();
assertEquals("ValueCount: ", 2*count, tr.getValueCount());
}
private static class StatRunner
implements Runnable
{
private JacsonChunkSource ts;
private Jacson stat;
public StatRunner(Jacson stat, JacsonChunkSource ts)
{
this.ts = ts;
this.stat = stat;
}
public void run()
{
try
{
stat.run(ts);
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
public static Test suite() {
TestSuite suite = new TestSuite(TailSourceTest.class);
return suite;
}
public static void main(java.lang.String[] args) {
junit.textui.TestRunner.run(suite());
}
}
//
// Jacson - Text Filtering with Java.
// Copyright (C) 2002 Frank S. Nestel (nestefan -at- users.sourceforge.net)
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -