aboutsummaryrefslogtreecommitdiffstats
path: root/clustercontroller-utils/src/test/java/com/yahoo/vespa/clustercontroller/utils/test/TestTransport.java
blob: dc53985639d9c1b807b1363caf59d99cbc82bc01 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.clustercontroller.utils.test;

import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperation;
import com.yahoo.vespa.clustercontroller.utils.communication.async.AsyncOperationImpl;
import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequest;
import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpRequestHandler;
import com.yahoo.vespa.clustercontroller.utils.communication.http.HttpResult;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
 * This class is a utility for unit tests.. You can register HttpRequestHandler instances in it, and then
 * you can extract an AsyncHttpClient<HttpResult> instance from it, which you can use to talk to the
 * registered servers. Thus you can do end to end testing of components talking over HTTP without actually
 * going through HTTP if you are using the HTTP abstraction layer in communication.http package.
 */
public class TestTransport {

    private static final Logger log = Logger.getLogger(TestTransport.class.getName());
    private static class Handler {
        final HttpRequestHandler handler;
        final String pathPrefix;
        Handler(HttpRequestHandler h, String prefix) { this.handler = h; this.pathPrefix = prefix; }
    }
    private static class Socket {
        public final String hostname;
        public final int port;

        Socket(String hostname, int port) {
            this.hostname = hostname;
            this.port = port;
        }
        @Override
        public boolean equals(Object o) {
            if (!(o instanceof Socket other)) return false;
            return (hostname.equals(other.hostname) && port == other.port);
        }
        @Override
        public int hashCode() {
            return hostname.hashCode() * port;
        }
    }
    private static class Request {
        public final HttpRequest request;
        public final AsyncOperationImpl<HttpResult> result;

        Request(HttpRequest r, AsyncOperationImpl<HttpResult> rr) {
            this.request = r;
            this.result = rr;
        }
    }
    private final Map<Socket, List<Handler>> handlers = new HashMap<>();
    private final LinkedList<Request> requests = new LinkedList<>();
    private final AsyncHttpClient<HttpResult> client = new AsyncHttpClient<>() {
        @Override
        public AsyncOperation<HttpResult> execute(HttpRequest r) {
            log.fine("Queueing request " + r);
            if (r.getHttpOperation() == null) {
                r = r.clone();
                r.setHttpOperation(r.getPostContent() == null ? HttpRequest.HttpOp.GET : HttpRequest.HttpOp.POST);
            }
            r.verifyComplete();
            AsyncOperationImpl<HttpResult> op = new AsyncOperationImpl<>(r.toString());
            synchronized (requests) {
                requests.addLast(new Request(r, op));
            }
            return op;
        }

        @Override
        public void close() { TestTransport.this.close(); }
    };
    private boolean running = true;
    private final Thread workerThread = new Thread() {
        @Override
        public void run() {
            while (running) {
                synchronized (requests) {
                    if (requests.isEmpty()) {
                        try {
                            requests.wait(100);
                        } catch (InterruptedException e) { return; }
                    } else {
                        Request request = requests.removeFirst();
                        HttpRequest r = request.request;
                        log.fine("Processing request " + r);
                        HttpRequestHandler handler = getHandler(r);
                        if (handler == null) {
                            if (log.isLoggable(Level.FINE)) {
                                log.fine("Failed to find target for request " + r.toString(true));
                                log.fine("Existing handlers:");
                                for (Socket socket : handlers.keySet()) {
                                    log.fine("  Socket " + socket.hostname + ":" + socket.port);
                                    for (Handler h : handlers.get(socket)) {
                                        log.fine("    " + h.pathPrefix);
                                    }
                                }
                            }
                            request.result.setResult(new HttpResult().setHttpCode(
                                    404, "No such server socket with suitable path prefix found open"));
                        } else {
                            try{
                                request.result.setResult(handler.handleRequest(r));
                            } catch (Exception e) {
                                HttpResult result = new HttpResult().setHttpCode(500, e.getMessage());
                                StringWriter sw = new StringWriter();
                                e.printStackTrace(new PrintWriter(sw));
                                result.setContent(sw.toString());
                                request.result.setResult(result);
                            }
                        }
                        //log.fine("Request " + r.toString(true) + " created result " + request.getSecond().getResult().toString(true));
                    }
                }
            }
        }
    };

    public TestTransport() {
        workerThread.start();
    }

    public void close() {
        if (!running) return;
        running = false;
        synchronized (requests) { requests.notifyAll(); }
        try {
            workerThread.join();
        } catch (InterruptedException e) {}
    }

    /** Get an HTTP client that talks to this test transport layer. */
    public AsyncHttpClient<HttpResult> getClient() { return client; }

    private HttpRequestHandler getHandler(HttpRequest r) {
        Socket socket = new Socket(r.getHost(), r.getPort());
        synchronized (this) {
            List<Handler> handlerList = handlers.get(socket);
            if (handlerList == null) {
                log.fine("No socket match");
                return null;
            }
            log.fine("Socket found");
            for (Handler h : handlers.get(socket)) {
                if (r.getPath().length() >= h.pathPrefix.length() && r.getPath().startsWith(h.pathPrefix)) {
                    return h.handler;
                }
            }
            log.fine("No path prefix match");
        }
        return null;
    }

    public void addServer(HttpRequestHandler server, String hostname, int port, String pathPrefix) {
        Socket socket = new Socket(hostname, port);
        synchronized (this) {
            List<Handler> shandlers = handlers.get(socket);
            if (shandlers == null) {
                shandlers = new LinkedList<>();
                handlers.put(socket, shandlers);
            }
            shandlers.add(new Handler(server, pathPrefix));
        }
    }

}