|
| 1 | +/* |
| 2 | + * Copyright 2021 Google |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.cloud.opentelemetry.endtoend; |
| 17 | + |
| 18 | +import com.google.api.core.ApiFuture; |
| 19 | +import com.google.api.core.ApiFutureCallback; |
| 20 | +import com.google.api.core.ApiFutures; |
| 21 | +import com.google.cloud.pubsub.v1.AckReplyConsumer; |
| 22 | +import com.google.cloud.pubsub.v1.Publisher; |
| 23 | +import com.google.cloud.pubsub.v1.Subscriber; |
| 24 | +import com.google.common.util.concurrent.MoreExecutors; |
| 25 | +import com.google.pubsub.v1.PubsubMessage; |
| 26 | + |
| 27 | +/** |
| 28 | + * Server implements the "integration test driver" for this language. |
| 29 | + * |
| 30 | + * <p>It is responsible for the following: |
| 31 | + * |
| 32 | + * <ul> |
| 33 | + * <li>Setting up a subscriber queue for inbound "RPC Request" messages |
| 34 | + * <li>Converting incoming pub sub messages to {@link Request} |
| 35 | + * <li>Setting up a publisher queue for outbound "RPC Response" messages |
| 36 | + * <li>Converting from outbound {@link Response} to pubsub messages. |
| 37 | + * <li>Handling any/all failures escaping the test scenario. |
| 38 | + * </ul> |
| 39 | + * |
| 40 | + * <p>This class includes a main method which runs the integration test driver using locally |
| 41 | + * available credentials to acccess pubsub channels. |
| 42 | + */ |
| 43 | +public class Server implements AutoCloseable { |
| 44 | + private final ScenarioHandlerManager scenarioHandlers = new ScenarioHandlerManager(); |
| 45 | + private final Publisher publisher; |
| 46 | + private final Subscriber subscriber; |
| 47 | + |
| 48 | + public Server() throws Exception { |
| 49 | + this.publisher = Publisher.newBuilder(Constants.getResponseTopic()).build(); |
| 50 | + this.subscriber = |
| 51 | + Subscriber.newBuilder(Constants.getRequestSubscription(), this::handleMessage).build(); |
| 52 | + subscriber.addListener( |
| 53 | + new Subscriber.Listener() { |
| 54 | + @Override |
| 55 | + public void failed(Subscriber.State from, Throwable failure) { |
| 56 | + // Handle failure. This is called when the Subscriber encountered a fatal error and is |
| 57 | + // shutting down. |
| 58 | + System.err.println(failure); |
| 59 | + } |
| 60 | + }, |
| 61 | + MoreExecutors.directExecutor()); |
| 62 | + } |
| 63 | + |
| 64 | + /** Starts the subcriber pulling requests. */ |
| 65 | + public void start() { |
| 66 | + subscriber.startAsync().awaitRunning(); |
| 67 | + } |
| 68 | + |
| 69 | + /** Closes our subscriptions. */ |
| 70 | + public void close() { |
| 71 | + if (subscriber != null) { |
| 72 | + subscriber.stopAsync(); |
| 73 | + subscriber.awaitTerminated(); |
| 74 | + } |
| 75 | + if (publisher != null) { |
| 76 | + publisher.shutdown(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** This method converts from {@link Response} to pubsub and sends out the publisher channel. */ |
| 81 | + public void respond(final String testId, final Response response) { |
| 82 | + final PubsubMessage message = |
| 83 | + PubsubMessage.newBuilder() |
| 84 | + .putAttributes(Constants.TEST_ID, testId) |
| 85 | + .putAttributes(Constants.STATUS_CODE, Integer.toString(response.statusCode().ordinal())) |
| 86 | + .setData(response.data()) |
| 87 | + .build(); |
| 88 | + ApiFuture<String> messageIdFuture = publisher.publish(message); |
| 89 | + ApiFutures.addCallback( |
| 90 | + messageIdFuture, |
| 91 | + new ApiFutureCallback<String>() { |
| 92 | + public void onSuccess(String messageId) {} |
| 93 | + |
| 94 | + public void onFailure(Throwable t) { |
| 95 | + System.out.println("failed to publish response to test: " + testId); |
| 96 | + t.printStackTrace(); |
| 97 | + } |
| 98 | + }, |
| 99 | + MoreExecutors.directExecutor()); |
| 100 | + } |
| 101 | + |
| 102 | + /** Execute a scenario based on the incoming message from the test runner. */ |
| 103 | + public void handleMessage(PubsubMessage message, AckReplyConsumer consumer) { |
| 104 | + if (!message.containsAttributes(Constants.TEST_ID)) { |
| 105 | + consumer.nack(); |
| 106 | + return; |
| 107 | + } |
| 108 | + String testId = message.getAttributesOrDefault(Constants.TEST_ID, ""); |
| 109 | + if (!message.containsAttributes(Constants.SCENARIO)) { |
| 110 | + respond( |
| 111 | + testId, |
| 112 | + Response.invalidArgument( |
| 113 | + String.format("Expected attribute \"%s\" is missing", Constants.SCENARIO))); |
| 114 | + consumer.ack(); |
| 115 | + return; |
| 116 | + } |
| 117 | + String scenario = message.getAttributesOrDefault(Constants.SCENARIO, ""); |
| 118 | + Request request = Request.make(testId, message.getAttributesMap(), message.getData()); |
| 119 | + |
| 120 | + // Run the given request/response cycle through a handler and respond with results. |
| 121 | + Response response = Response.EMPTY; |
| 122 | + try { |
| 123 | + response = scenarioHandlers.handleScenario(scenario, request); |
| 124 | + } catch (Throwable e) { |
| 125 | + response = Response.internalError(e); |
| 126 | + } finally { |
| 127 | + respond(testId, response); |
| 128 | + consumer.ack(); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** Runs our server. */ |
| 133 | + public static void main(String[] args) throws Exception { |
| 134 | + try (Server server = new Server()) { |
| 135 | + server.start(); |
| 136 | + // Docs for Subscriber recommend doing this to block main thread while daemon thread consumes |
| 137 | + // stuff. |
| 138 | + for (; ; ) { |
| 139 | + Thread.sleep(Long.MAX_VALUE); |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | +} |
0 commit comments