You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by bu...@apache.org on 2019/04/23 11:55:09 UTC

[cxf] branch master updated: cxf-systests-tracing: stabilize OpenTracingTracingTest

This is an automated email from the ASF dual-hosted git repository.

buhhunyx pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new ee1491c  cxf-systests-tracing: stabilize OpenTracingTracingTest
ee1491c is described below

commit ee1491cad1932f4f26b7d317d892834772b8e112
Author: amarkevich <am...@talend.com>
AuthorDate: Tue Apr 23 14:54:38 2019 +0300

    cxf-systests-tracing: stabilize OpenTracingTracingTest
---
 .../org/apache/cxf/systest/jaeger/TestSender.java  | 14 ++--
 .../cxf/systest/jaxrs/tracing/BookStore.java       | 89 +++++++---------------
 .../opentracing/OpenTracingTracingTest.java        | 13 ++--
 3 files changed, 41 insertions(+), 75 deletions(-)

diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaeger/TestSender.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaeger/TestSender.java
index a7bea38..d4f49a7 100644
--- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaeger/TestSender.java
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaeger/TestSender.java
@@ -18,20 +18,20 @@
  */
 package org.apache.cxf.systest.jaeger;
 
-import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import io.jaegertracing.internal.JaegerSpan;
 import io.jaegertracing.internal.exceptions.SenderException;
 import io.jaegertracing.spi.Sender;
 
 public class TestSender implements Sender {
-    private static List<JaegerSpan> spans = Collections.synchronizedList(new ArrayList<>());
-    
+
+    private static final List<JaegerSpan> SPANS = new CopyOnWriteArrayList<>();
+
     @Override
     public int append(JaegerSpan span) throws SenderException {
-        spans.add(span);
+        SPANS.add(span);
         return 0;
     }
 
@@ -46,10 +46,10 @@ public class TestSender implements Sender {
     }
 
     public static List<JaegerSpan> getAllSpans() {
-        return spans;
+        return SPANS;
     }
 
     public static void clear() {
-        spans.clear();
+        SPANS.clear();
     }
 }
diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
index 9be6a02..f460540 100644
--- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/BookStore.java
@@ -23,10 +23,9 @@ import java.io.IOException;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.UUID;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 
 import javax.ws.rs.GET;
 import javax.ws.rs.PUT;
@@ -46,17 +45,14 @@ import org.apache.cxf.tracing.TracerContext;
 @Path("/bookstore/")
 public class BookStore<T extends Closeable> {
     @Context private TracerContext tracer;
-    private ExecutorService executor = Executors.newSingleThreadExecutor();
+    private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
 
     @GET
     @Path("/books")
     @Produces(MediaType.APPLICATION_JSON)
-    public Collection< Book > getBooks() throws IOException {
+    public Collection<Book> getBooks() throws IOException {
         try (T span = tracer.startSpan("Get Books")) {
-            return Arrays.asList(
-                new Book("Apache CXF in Action", UUID.randomUUID().toString()),
-                new Book("Mastering Apache CXF", UUID.randomUUID().toString())
-            );
+            return books();
         }
     }
 
@@ -67,25 +63,15 @@ public class BookStore<T extends Closeable> {
         tracer.continueSpan(new Traceable<Void>() {
             @Override
             public Void call(final TracerContext context) throws Exception {
-                executor.submit(
+                executor.schedule(
                     tracer.wrap("Processing books", new Traceable<Void>() {
                         @Override
                         public Void call(final TracerContext context) throws Exception {
-                            // Simulate some running job
-                            Thread.sleep(200);
-
-                            response.resume(
-                                Arrays.asList(
-                                    new Book("Apache CXF in Action", UUID.randomUUID().toString()),
-                                    new Book("Mastering Apache CXF", UUID.randomUUID().toString())
-                                )
-                            );
-
+                            response.resume(books());
                             return null;
                         }
                     }
-                ));
-
+                ), 200L, TimeUnit.MILLISECONDS); // Simulate some running job
                 return null;
             }
         });
@@ -94,25 +80,9 @@ public class BookStore<T extends Closeable> {
     @GET
     @Path("/books/async/notrace")
     @Produces(MediaType.APPLICATION_JSON)
-    public void getBooksAsyncNoTrace(@Suspended final AsyncResponse response) throws Exception {
-        executor.submit(
-            new Callable<Void>() {
-                @Override
-                public Void call() throws Exception {
-                    // Simulate some running job
-                    Thread.sleep(200);
-
-                    response.resume(
-                        Arrays.asList(
-                            new Book("Apache CXF in Action", UUID.randomUUID().toString()),
-                            new Book("Mastering Apache CXF", UUID.randomUUID().toString())
-                        )
-                    );
-
-                    return null;
-                }
-            }
-        );
+    public void getBooksAsyncNoTrace(@Suspended final AsyncResponse response) {
+        executor.schedule(() -> response.resume(books()),
+                200L, TimeUnit.MILLISECONDS); // Simulate some running job
     }
 
     @GET
@@ -125,10 +95,7 @@ public class BookStore<T extends Closeable> {
                 return tracer.wrap("Processing books", new Traceable<Collection<Book>>() {
                     @Override
                     public Collection<Book> call(final TracerContext context) throws Exception {
-                        return Arrays.asList(
-                            new Book("Apache CXF in Action", UUID.randomUUID().toString()),
-                            new Book("Mastering Apache CXF", UUID.randomUUID().toString())
-                        );
+                        return books();
                     }
                 }).call();
             }
@@ -146,22 +113,22 @@ public class BookStore<T extends Closeable> {
     @PUT
     @Path("/process")
     @Produces(MediaType.APPLICATION_JSON)
-    public Response processBooks() throws InterruptedException {
-        final Future<Void> future = executor.submit(
-            tracer.wrap("Processing books", new Traceable<Void>() {
-                @Override
-                public Void call(final TracerContext context) throws Exception {
-                    context.timeline("Processing started");
-                    return null;
-                }
-            })
-        );
-        
-        if (!future.isDone()) {
-            // Just give it some time to have trace finished 
-            Thread.sleep(20);
-        }
-
+    public Response processBooks() throws Exception {
+        tracer.wrap("Processing books", new Traceable<Void>() {
+            @Override
+            public Void call(final TracerContext context) throws Exception {
+                context.timeline("Processing started");
+                return null;
+            }
+        }).call();
         return Response.ok().build();
     }
+
+    private static Collection<Book> books() {
+        return Arrays.asList(
+                new Book("Apache CXF in Action", UUID.randomUUID().toString()),
+                new Book("Mastering Apache CXF", UUID.randomUUID().toString())
+            );
+    }
+
 }
diff --git a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/OpenTracingTracingTest.java b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/OpenTracingTracingTest.java
index b0da034..953241f 100644
--- a/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/OpenTracingTracingTest.java
+++ b/systests/tracing/src/test/java/org/apache/cxf/systest/jaxrs/tracing/opentracing/OpenTracingTracingTest.java
@@ -82,7 +82,7 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
 
     private Tracer tracer;
     private OpenTracingClientProvider openTracingClientProvider;
-    private Random random;
+    private final Random random = new Random();
 
     @Ignore
     public static class Server extends AbstractBusTestServerBase {
@@ -134,7 +134,6 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
             .getTracer();
 
         openTracingClientProvider = new OpenTracingClientProvider(tracer);
-        random = new Random();
     }
 
     @Test
@@ -201,8 +200,8 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
         assertEquals(Status.OK.getStatusCode(), r.getStatus());
 
         assertThat(TestSender.getAllSpans().size(), equalTo(2));
-        assertThat(TestSender.getAllSpans().get(0).getOperationName(), equalTo("Processing books"));
-        assertThat(TestSender.getAllSpans().get(1).getOperationName(), equalTo("GET /bookstore/books/async"));
+        assertEquals("Processing books", TestSender.getAllSpans().get(0).getOperationName());
+        assertEquals("GET /bookstore/books/async", TestSender.getAllSpans().get(1).getOperationName());
         assertThat(TestSender.getAllSpans().get(1).getReferences(), not(empty()));
         assertThat(TestSender.getAllSpans().get(1).getReferences().get(0).getSpanContext().getSpanId(),
             equalTo(spanId.getSpanId()));
@@ -289,7 +288,7 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
             assertEquals(Status.OK.getStatusCode(), r.getStatus());
         }
 
-        assertThat(TestSender.getAllSpans().size(), equalTo(12));
+        assertEquals(TestSender.getAllSpans().toString(), 12, TestSender.getAllSpans().size());
 
         IntStream
             .range(0, 4)
@@ -312,7 +311,7 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
             final Response r = client.get();
             assertEquals(Status.OK.getStatusCode(), r.getStatus());
 
-            assertThat(TestSender.getAllSpans().size(), equalTo(3));
+            assertEquals(TestSender.getAllSpans().toString(), 3, TestSender.getAllSpans().size());
             assertThat(TestSender.getAllSpans().get(0).getOperationName(), equalTo("Get Books"));
             assertThat(TestSender.getAllSpans().get(0).getReferences(), not(empty()));
             assertThat(TestSender.getAllSpans().get(1).getReferences(), not(empty()));
@@ -394,7 +393,7 @@ public class OpenTracingTracingTest extends AbstractBusClientServerTestBase {
 
     private<T> T get(final Future<T> future) {
         try {
-            return future.get(1, TimeUnit.HOURS);
+            return future.get(1L, TimeUnit.HOURS);
         } catch (InterruptedException | TimeoutException | ExecutionException ex) {
             throw new RuntimeException(ex);
         }