You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@cxf.apache.org by GitBox <gi...@apache.org> on 2019/01/03 23:28:47 UTC

[GitHub] amarkevich closed pull request #490: cxf-rt-transports-udp: minor code improvements to improve test stability

amarkevich closed pull request #490: cxf-rt-transports-udp: minor code improvements to improve test stability
URL: https://github.com/apache/cxf/pull/490
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java
index edeacd2822c..3805c37f47b 100644
--- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java
+++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPConduit.java
@@ -114,11 +114,7 @@ private void dataReceived(Message message, IoBuffer buf, boolean async, boolean
                     if (queue == null) {
                         queue = queuem.getAutomaticWorkQueue();
                     }
-                    queue.execute(new Runnable() {
-                        public void run() {
-                            incomingObserver.onMessage(inMessage);
-                        }
-                    });
+                    queue.execute(() -> incomingObserver.onMessage(inMessage));
                 } else {
                     incomingObserver.onMessage(inMessage);
                     if (!message.getExchange().isSynchronous() || multi) {
@@ -194,17 +190,13 @@ public void prepare(final Message message) throws IOException {
                 int port = Integer.parseInt(s);
                 sendViaBroadcast(message, null, port);
             } else {
-                InetSocketAddress isa = null;
-                String hp = "";
-
-                isa = new InetSocketAddress(uri.getHost(), uri.getPort());
-                hp = uri.getHost() + ":" + uri.getPort();
-
+                final InetSocketAddress isa = new InetSocketAddress(uri.getHost(), uri.getPort());
                 if (isa.getAddress().isMulticastAddress()) {
                     sendViaBroadcast(message, isa, isa.getPort());
                     return;
                 }
 
+                final String hp = uri.getHost() + ':' + uri.getPort();
                 Queue<ConnectFuture> q = connections.get(hp);
                 ConnectFuture connFuture = null;
                 if (q != null) {
@@ -217,9 +209,9 @@ public void prepare(final Message message) throws IOException {
                     ((DatagramSessionConfig)connFuture.getSession().getConfig()).setReceiveBufferSize(64 * 1024);
                 }
                 connFuture.getSession().setAttribute(CXF_MESSAGE_ATTR, message);
-                message.setContent(OutputStream.class, new UDPConduitOutputStream(connector, connFuture, message));
+                message.setContent(OutputStream.class, new UDPConduitOutputStream(connFuture));
                 message.getExchange().put(ConnectFuture.class, connFuture);
-                message.getExchange().put(HOST_PORT, uri.getHost() + ":" + uri.getPort());
+                message.getExchange().put(HOST_PORT, hp);
             }
         } catch (Exception ex) {
             throw new IOException(ex);
@@ -340,19 +332,13 @@ public void flush() throws IOException {
         }
     }
 
-    public class UDPConduitOutputStream extends OutputStream {
+    static class UDPConduitOutputStream extends OutputStream {
         final ConnectFuture future;
-        final NioDatagramConnector connector;
-        final Message message;
         IoBuffer buffer = IoBuffer.allocate(64 * 1024 - 42); //max size
         boolean closed;
 
-        public UDPConduitOutputStream(NioDatagramConnector connector,
-                                      ConnectFuture connFuture,
-                                      Message m) {
-            this.connector = connector;
+        UDPConduitOutputStream(ConnectFuture connFuture) {
             this.future = connFuture;
-            this.message = m;
         }
 
         public void write(int b) throws IOException {
diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java
index c5b8996fca2..d3e22ac4a65 100644
--- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java
+++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPDestination.java
@@ -98,22 +98,14 @@ public void close() throws IOException {
                         }
                     };
 
-                    UDPConnectionInfo info = new UDPConnectionInfo(null,
-                                                                   out,
-                                                                   new ByteArrayInputStream(bytes, 0, p.getLength()));
-
                     final MessageImpl m = new MessageImpl();
                     final Exchange exchange = new ExchangeImpl();
                     exchange.setDestination(UDPDestination.this);
                     m.setDestination(UDPDestination.this);
                     exchange.setInMessage(m);
-                    m.setContent(InputStream.class, info.in);
-                    m.put(UDPConnectionInfo.class, info);
-                    queue.execute(new Runnable() {
-                        public void run() {
-                            getMessageObserver().onMessage(m);
-                        }
-                    });
+                    m.setContent(InputStream.class, new ByteArrayInputStream(bytes, 0, p.getLength()));
+                    m.put(OutputStream.class, out);
+                    queue.execute(() -> getMessageObserver().onMessage(m));
                 } catch (IOException ex) {
                     ex.printStackTrace();
                 }
@@ -124,14 +116,13 @@ public void run() {
 
     /** {@inheritDoc}*/
     @Override
-    protected Conduit getInbuiltBackChannel(Message inMessage) {
+    protected Conduit getInbuiltBackChannel(final Message inMessage) {
         if (inMessage.getExchange().isOneWay()) {
             return null;
         }
-        final UDPConnectionInfo info = inMessage.get(UDPConnectionInfo.class);
         return new AbstractBackChannelConduit() {
             public void prepare(Message message) throws IOException {
-                message.setContent(OutputStream.class, info.out);
+                message.setContent(OutputStream.class, inMessage.get(OutputStream.class));
             }
         };
     }
@@ -190,8 +181,9 @@ protected void activate() {
                 dcfg.setReuseAddress(true);
                 acceptor.bind();
             }
+        } catch (RuntimeException e) {
+            throw e;
         } catch (Exception ex) {
-            ex.printStackTrace();
             throw new RuntimeException(ex);
         }
     }
@@ -235,22 +227,8 @@ protected void deactivate() {
         }
     }
 
-    static class UDPConnectionInfo {
-        final IoSession session;
-        final OutputStream out;
-        final InputStream in;
-
-        UDPConnectionInfo(IoSession io, OutputStream o, InputStream i) {
-            session = io;
-            out = o;
-            in = i;
-        }
-    }
-
-
     class UDPIOHandler extends StreamIoHandler {
 
-
         @Override
         public void sessionOpened(IoSession session) {
             // Set timeouts
@@ -273,12 +251,8 @@ protected void processStreamIo(IoSession session, InputStream in, OutputStream o
             exchange.setInMessage(m);
             m.setContent(InputStream.class, in);
             out = new UDPDestinationOutputStream(out);
-            m.put(UDPConnectionInfo.class, new UDPConnectionInfo(session, out, in));
-            queue.execute(new Runnable() {
-                public void run() {
-                    getMessageObserver().onMessage(m);
-                }
-            });
+            m.put(OutputStream.class, out);
+            queue.execute(() -> getMessageObserver().onMessage(m));
         }
 
         public void sessionClosed(IoSession session) throws Exception {
@@ -329,12 +303,12 @@ public void sessionIdle(IoSession session, IdleStatus status) {
         }
     }
 
-    public class UDPDestinationOutputStream extends OutputStream {
+    static class UDPDestinationOutputStream extends OutputStream {
         final OutputStream out;
         IoBuffer buffer = IoBuffer.allocate(64 * 1024 - 42); //max size
         boolean closed;
 
-        public UDPDestinationOutputStream(OutputStream out) {
+        UDPDestinationOutputStream(OutputStream out) {
             this.out = out;
         }
 
diff --git a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPTransportFactory.java b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPTransportFactory.java
index 5703105001e..3303715a218 100644
--- a/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPTransportFactory.java
+++ b/rt/transports/udp/src/main/java/org/apache/cxf/transport/udp/UDPTransportFactory.java
@@ -20,7 +20,6 @@
 package org.apache.cxf.transport.udp;
 
 import java.io.IOException;
-import java.util.Arrays;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.List;
@@ -45,14 +44,10 @@
     implements DestinationFactory, ConduitInitiator {
 
     public static final String TRANSPORT_ID = "http://cxf.apache.org/transports/udp";
-    public static final List<String> DEFAULT_NAMESPACES
-        = Collections.unmodifiableList(Arrays.asList(TRANSPORT_ID));
+    public static final List<String> DEFAULT_NAMESPACES = Collections.singletonList(TRANSPORT_ID);
 
     private static final Logger LOG = LogUtils.getL7dLogger(UDPTransportFactory.class);
-    private static final Set<String> URI_PREFIXES = new HashSet<>();
-    static {
-        URI_PREFIXES.add("udp://");
-    }
+    private static final Set<String> URI_PREFIXES = Collections.singleton("udp://");
 
     private Set<String> uriPrefixes = new HashSet<>(URI_PREFIXES);
 
@@ -94,7 +89,7 @@ public Conduit getConduit(EndpointInfo ei, EndpointReferenceType target, Bus bus
     public void setUriPrefixes(Set<String> s) {
         uriPrefixes = s;
     }
-    EndpointReferenceType createReference(EndpointInfo ei) {
+    static EndpointReferenceType createReference(EndpointInfo ei) {
         EndpointReferenceType epr = new EndpointReferenceType();
         AttributedURIType address = new AttributedURIType();
         address.setValue(ei.getAddress());
diff --git a/rt/transports/udp/src/test/java/org/apache/cxf/transport/udp/UDPTransportTest.java b/rt/transports/udp/src/test/java/org/apache/cxf/transport/udp/UDPTransportTest.java
index fd37f27fc0f..2c393a54d9a 100644
--- a/rt/transports/udp/src/test/java/org/apache/cxf/transport/udp/UDPTransportTest.java
+++ b/rt/transports/udp/src/test/java/org/apache/cxf/transport/udp/UDPTransportTest.java
@@ -22,12 +22,10 @@
 import java.net.NetworkInterface;
 import java.util.Enumeration;
 
-import javax.jws.WebService;
-
 import org.apache.cxf.endpoint.Server;
 import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
 import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
-import org.apache.cxf.testutil.common.AbstractBusClientServerTestBase;
+import org.apache.cxf.testutil.common.TestUtil;
 import org.apache.hello_world.Greeter;
 
 import org.junit.AfterClass;
@@ -35,21 +33,18 @@
 import org.junit.Test;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 /**
  *
  */
-public class UDPTransportTest extends AbstractBusClientServerTestBase {
-    static final String PORT = allocatePort(UDPTransportTest.class);
+public class UDPTransportTest {
+    private static final String PORT = TestUtil.getPortNumber(UDPTransportTest.class);
     private static Server server;
 
-    @WebService(serviceName = "SOAPService",
-        endpointInterface = "org.apache.hello_world.Greeter",
-        targetNamespace = "http://apache.org/hello_world")
     static class GreeterImpl implements Greeter {
         private String myName = "defaultGreeter";
-        GreeterImpl() {
-        }
+
         public String greetMe(String me) {
             return "Hello " + me;
         }
@@ -57,17 +52,18 @@ public String sayHi() {
             return "Bonjour from " + myName;
         }
         public void pingMe() {
+            throw new UnsupportedOperationException();
         }
     }
 
 
     @BeforeClass
     public static void setUpBeforeClass() throws Exception {
-        createStaticBus();
         JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
-        factory.setBus(getStaticBus());
         factory.setAddress("udp://:" + PORT);
+        factory.setServiceClass(Greeter.class);
         factory.setServiceBean(new GreeterImpl());
+        // factory.setFeatures(Collections.singletonList(new LoggingFeature()));
         server = factory.create();
     }
 
@@ -84,11 +80,13 @@ public void testSimpleUDP() throws Exception {
         fact.setAddress("udp://localhost:" + PORT);
         Greeter g = fact.create(Greeter.class);
         for (int x = 0; x < 5; x++) {
-            assertEquals("Hello World", g.greetMe("World"));
+            final String message = Integer.toString(x);
+            assertTrue(g.greetMe(message).endsWith(message));
         }
 
         ((java.io.Closeable)g).close();
     }
+
     @Test
     public void testBroadcastUDP() throws Exception {
         // Disable the test on Redhat Enterprise Linux which doesn't enable the UDP broadcast by default
@@ -117,6 +115,7 @@ public void testBroadcastUDP() throws Exception {
         fact.setAddress("udp://:" + PORT + "/foo");
         Greeter g = fact.create(Greeter.class);
         assertEquals("Hello World", g.greetMe("World"));
+
         ((java.io.Closeable)g).close();
     }
 
@@ -133,4 +132,12 @@ public void testLargeRequest() throws Exception {
 
         ((java.io.Closeable)g).close();
     }
+
+    @Test(expected = javax.xml.ws.soap.SOAPFaultException.class)
+    public void testFailure() throws Exception {
+        JaxWsProxyFactoryBean fact = new JaxWsProxyFactoryBean();
+        fact.setAddress("udp://localhost:" + PORT);
+        Greeter g = fact.create(Greeter.class);
+        g.pingMe();
+    }
 }


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services