Class OtlpTestCollector

java.lang.Object
com.codename1.backend.test.OtlpTestCollector

public final class OtlpTestCollector extends Object

An OTLP/HTTP receiver for tests: what a test points the exporters of a BackendTest.Feature.TELEMETRY application at, to see that they send and that they flush when the application stops.

It accepts every POST, answers 200 with an empty body, and keeps each request's path and body. It decodes nothing -- the exporters' encoding has its own tests -- so a test asserts on what arrived where, and how much.

static final OtlpTestCollector COLLECTOR = OtlpTestCollector.start();

@BackendTest(features = BackendTest.Feature.TELEMETRY,
        properties = {"cn1.otel.endpoint=http://127.0.0.1:4318",
                      "cn1.otel.metrics.intervalMillis=200"})

A fixed endpoint in properties needs a fixed port: start(int) takes one. Runs on the JVM and compiled alike; it is an ordinary HttpServer.

  • Method Details

    • start

      public static OtlpTestCollector start() throws IOException
      A collector on a free loopback port.
      Throws:
      IOException
    • start

      public static OtlpTestCollector start(int port) throws IOException
      A collector on port, for an endpoint a test names in its properties.
      Throws:
      IOException
    • getPort

      public int getPort()
      The port it listens on.
    • getEndpoint

      public String getEndpoint()
      The base URL for cn1.otel.endpoint, http://127.0.0.1:<port>.
    • count

      public int count(String path)
      How many requests arrived at path, /v1/traces or /v1/metrics.
    • bodies

      public List<byte[]> bodies(String path)
      The bodies that arrived at path, oldest first.
    • await

      public boolean await(String path, int count, long timeoutMillis) throws InterruptedException
      Waits until count requests have arrived at path; false when timeoutMillis passed first.
      Throws:
      InterruptedException
    • clear

      public void clear()
      Forgets what arrived so far.
    • stop

      public void stop()
      Stops listening.