Re: issue with exception handling in java code
Philip Aston <[email protected]>
| Newsgroups | gmane.comp.java.grinder.user |
|---|---|
| Message-ID | <[email protected]> |
JJ,
Sorry for the delay.
Thanks for the detailed report.
Yes, I agree - this is not the intended behaviour.
I have tried, in vain, to reproduce the problem. My latest attempt:
from net.grinder.script.Grinder import grinder
from net.grinder.script import Test
from grinder.test import MyClass
from grinder.test.MyClass import IOOperation
from java.net import SocketTimeoutException
test1 = Test(1, "")
c = MyClass()
# Instrument the info() method with our Test.
test1.record(c)
class Foo(IOOperation):
def run(self):
raise SocketTimeoutException()
# A TestRunner instance is created for each thread. It can be used to
# store thread-specific data.
class TestRunner:
# This method is called for every run.
def __call__(self):
grinder.logger.info("result {}",
c.topLevelExceptionHandler(Foo()))
where
public class MyClass {
public interface IOOperation {
int run() throws IOException;
}
public int topLevelExceptionHandler(final IOOperation r) {
try {
return r.run();
}
catch (final SocketTimeoutException e) {
return 1;
}
catch (final Throwable e) {
return -1;
}
finally {
}
}
}
Can you provide a standalone test case? Otherwise, I don't have any
other avenues to explore.
- Phil
On 18/02/14 16:41, JJ wrote:
> Hello, answering to myself,
> It is an issue with grinder/instrumentation of some sort ( altough it
> might be "proper" behavior but i think it is wrong )
> An easy way to fix it to use an encapsulating java class that will the
> target of the instrumentation but does nothing beyong call the real
> class/method. thus exception handling is not altered by
> instrumentation.
>
> eg :
>
> here :
> class UdpProx {
> UdpClient client = new UdpClient();
>
>
> public int createLegacyRequest("someparam" ... )
> {
> client.createLegacyRequest("someparam" ... );
> }
> }
>
> and change the jython code to
>
> from aaaa.bbbb import UdpProx
> (...)
> test1 = Test(1, "")
>
> client1 = UdpProx ()
>
> test1.record(client1)
>
> (...)
>
>
> kr.
> JJ
>
> Le 2014-02-12 14:49, JJ a écrit :
>> Hello i am not familiar with jython concept and Grinder so this may be
>> a
>> newbee issue, however the issue i am facing regarding exception
>> handling
>> is weird to me.
>>
>> I am using grinder to test load an udp server. This server, to make
>> it simple, act almost like an http server (but in private udp
>> protocol).
>>
>> i have written a java client code that work fine ; create udp request,
>> and listen for udp response and use this class from ultra basic jython
>> script that i copied from other source.
>>
>> Now it happens that the udp socket timeout when getting the expected
>> response (i know UDP is not reliable , the point is not there) and this
>> create an exception.
>>
>> My problem is this exception should be caught & processed at the java
>> level, there are all needed try catch . but it does not appear to be
>> the
>> case and the exception is only visible in the Log of grinder. ( and I
>> do
>> not print the shown stacktrace ).
>>
>> Is it due to some instrumentation that the exception bubbles up ?
>>
>> What could I be doing wrong ? ( again the exception is not abnormal,
>> it
>> only its processing that look abnormal to me )
>>
>> below are code snipets
>> Thanks for your help.
>> JJ
>>
>> --- full jython script code ------
>> # -*- coding:utf-8 -*-
>> from net.grinder.script.Grinder import grinder
>> from net.grinder.script import Test
>> from aaaa.bbbb import UdpClient
>> from java.lang import String
>> import time
>>
>> test1 = Test(1, "")
>>
>> client1 = UdpClient()
>>
>>
>> test1.record(client1)
>>
>>
>> class TestRunner:
>>
>> def __init__(self):
>> grinder.statistics.delayReports = True
>> pass
>>
>>
>> def __call__(self):
>> # this method should never throw an exception
>> result = client1.createLegacyRequest("someparam" ... )
>>
>> if result == -1:
>> grinder.statistics.forLastTest.success = 0
>> elif result == 0:
>> # this log is actually never shown - but should in the
>> expected error case
>> grinder.logger.warn("error missing more than 1 packet")
>> grinder.statistics.forLastTest.success = 0
>> else:
>> grinder.statistics.forLastTest.success = 1
>> --- full jython script code ------
>>
>> --- java code snippet ------
>> (...)
>>
>> public class UdpClient
>> {
>> /* this does not
>> declare any throw */
>> public int createLegacyRequest( ... some param )
>> {
>> int nb = -1;
>> DatagramSocket socket = null;
>> try
>> {
>> (...)
>>
>> socket = new DatagramSocket();
>> (...)
>> socket.send(packet);
>> nb = 0;
>>
>> for (int i= 0; i < expected ; i++)
>> {
>> socket.receive(rcvPacket);
>> nb++;
>> }
>> return nb;
>>
>> }catch(SocketTimeoutException ste)
>> {
>> logMe(" this is a throwable caught ", ste);
>> if (nb <= 0) return -1; // nothing received
>> if (nb < expected - 1) return 0; // we lost more than 1
>> return nb;
>> } catch (Throwable e)
>> {
>> logMe(": this is a throwable caught ", e);
>> if (nb <= 0) return -1; // nothing received
>> if (nb < expected - 1) return 0; // we lost more than 1
>> return nb;
>> }
>> finally
>> {
>> if (socket!= null)
>> {
>> try
>> {
>> socket.close();
>> } catch (Throwable e)
>> {
>>
>> }
>> }
>> }
>> }
>> --- end java code snippet ------
>> ---- grinder logs -------------
>> (...)
>> 2014-02-12 14:19:34,357 INFO The Grinder version 3.9.1
>> 2014-02-12 14:19:34,361 INFO Java(TM) SE Runtime Environment
>> 1.7.0_25-b15: Java HotSpot(TM) 64-Bit Server VM (23.25-b01, mixed mode)
>> on Linux
>> 2014-02-12 14:19:34,568 INFO worker process 0 of agent number 0
>> 2014-02-12 14:19:34,855 INFO instrumentation agents: byte code
>> transforming instrumenter for Jython 2.5; byte code transforming
>> instrumenter for Java
>> 2014-02-12 14:19:39,460 INFO running "udp.py" using Jython 2.5.3
>> (2.5:c56500f08d34+, Aug 13 2012, 14:54:35)
>> [Java HotSpot(TM) 64-Bit Server VM (Oracle Corporation)]
>> 2014-02-12 14:19:39,507 INFO starting, will run forever
>> 2014-02-12 14:19:39,508 INFO starting, will run forever
>> 2014-02-12 14:19:39,508 INFO starting, will run forever
>> 2014-02-12 14:19:39,508 INFO starting, will run forever
>> 2014-02-12 14:19:39,508 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,509 INFO starting, will run forever
>> 2014-02-12 14:19:39,510 INFO start time is 1392211179509 ms since
>> Epoch
>> 2014-02-12 14:19:48,671 ERROR Aborted run: Java exception calling
>> TestRunner
>> net.grinder.scriptengine.jython.JythonScriptExecutionException: Java
>> exception calling TestRunner
>> result = client1.createLegacyRequest( some param ... )
>> File "(...) udp.py", line 28, in __call__
>> java.net.SocketTimeoutException: Receive timed out
>> at java.net.PlainDatagramSocketImpl.receive0(Native Method)
>> ~[na:1.7.0_25]
>> at
>> java.net.AbstractPlainDatagramSocketImpl.receive(AbstractPlainDatagramSocketImpl.java:145)
>> ~[na:1.7.0_25]
>> at java.net.DatagramSocket.receive(DatagramSocket.java:786)
>> ~[na:1.7.0_25]
>> at aaaa.bbbb.UdpClient.createLegacyRequest(Unknown Source)
>> ~[udpgrinder.jar:na]
>> at sun.reflect.GeneratedMethodAccessor5.invoke(Unknown Source)
>> ~[na:na]
>> at
>> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>> ~[na:1.7.0_25]
>> at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_25]
>> at
>> org.python.core.PyReflectedFunction.__call__(PyReflectedFunction.java:186)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyObject.__call__(PyObject.java:345)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:220)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.__call__(PyMethod.java:211)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.__call__(PyMethod.java:201)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.pycode._pyx1.__call__$3((..)/udp.py:36) ~[na:na]
>> at org.python.pycode._pyx1.call_function((...)/udp.py) ~[na:na]
>> at org.python.core.PyTableCode.call(PyTableCode.java:165)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyBaseCode.call(PyBaseCode.java:301)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyBaseCode.call(PyBaseCode.java:194)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyFunction.__call__(PyFunction.java:387)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.instancemethod___call__(PyMethod.java:220)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.__call__(PyMethod.java:211)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyMethod.__call__(PyMethod.java:206)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyObject.invoke(PyObject.java:3555)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyInstance.instance___call__(PyInstance.java:351)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyInstance.__call__(PyInstance.java:342)
>> ~[jython-standalone-2.5.3.jar:na]
>> at org.python.core.PyObject.__call__(PyObject.java:371)
>> ~[jython-standalone-2.5.3.jar:na]
>> at
>> net.grinder.scriptengine.jython.JythonScriptEngine$JythonWorkerRunnable.run(JythonScriptEngine.java:250)
>> ~[grinder-core-3.9.1.jar:na]
>> at
>> net.grinder.engine.process.GrinderThread.run(GrinderThread.java:118)
>> ~[grinder-core-3.9.1.jar:na]
>> at java.lang.Thread.run(Thread.java:724) ~[na:1.7.0_25]
>> (...)
>>
>>
>>
------------------------------------------------------------------------------
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
_______________________________________________
grinder-use mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/grinder-use