|
18 | 18 | */ |
19 | 19 | package co.elastic.apm.agent.springwebclient; |
20 | 20 |
|
| 21 | +import co.elastic.apm.agent.common.JvmRuntimeInfo; |
21 | 22 | import co.elastic.apm.agent.httpclient.AbstractHttpClientInstrumentationTest; |
22 | 23 | import org.junit.runner.RunWith; |
23 | 24 | import org.junit.runners.Parameterized; |
|
30 | 31 | @RunWith(Parameterized.class) |
31 | 32 | public class WebClientInstrumentationTest extends AbstractHttpClientInstrumentationTest { |
32 | 33 |
|
33 | | - private final WebClient webClient; |
| 34 | + /** |
| 35 | + * Can't directly reference WebClient because it is compiled with java 17. |
| 36 | + */ |
| 37 | + private final Object webClient; |
34 | 38 |
|
35 | 39 | private final RequestStrategy strategy; |
36 | 40 |
|
37 | 41 | private final boolean isNetty; |
38 | 42 |
|
39 | | - public WebClientInstrumentationTest(String clientIgnored, WebClient webClient, RequestStrategy strategy, boolean isNetty) { |
| 43 | + public WebClientInstrumentationTest(String clientIgnored, Object webClient, RequestStrategy strategy, boolean isNetty) { |
40 | 44 | this.webClient = webClient; |
41 | 45 | this.strategy = strategy; |
42 | 46 | this.isNetty = isNetty; |
43 | 47 | } |
44 | 48 |
|
45 | 49 | @Parameterized.Parameters(name = "client = {0}, request strategy = {2}") |
46 | 50 | public static Object[][] testParams() { |
47 | | - return new Object[][]{ |
48 | | - {"jetty", jettyClient(), RequestStrategy.EXCHANGE, false}, |
49 | | - {"jetty", jettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, false}, |
50 | | - {"jetty", jettyClient(), RequestStrategy.EXCHANGE_TO_MONO, false}, |
51 | | - {"jetty", jettyClient(), RequestStrategy.RETRIEVE, false}, |
52 | | - {"netty", nettyClient(), RequestStrategy.EXCHANGE, true}, |
53 | | - {"netty", nettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, true}, |
54 | | - {"netty", nettyClient(), RequestStrategy.EXCHANGE_TO_MONO, true}, |
55 | | - {"netty", nettyClient(), RequestStrategy.RETRIEVE, true}, |
56 | | - {"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE, false}, |
57 | | - {"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_FLUX, false}, |
58 | | - {"hc5", reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_MONO, false}, |
59 | | - {"hc5", reactiveHttpClient5(), RequestStrategy.RETRIEVE, false} |
60 | | - }; |
| 51 | + if (JvmRuntimeInfo.ofCurrentVM().getMajorVersion() >= 17) { |
| 52 | + return new Object[][]{ |
| 53 | + {"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE, false}, |
| 54 | + {"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, false}, |
| 55 | + {"jetty", Clients.jettyClient(), RequestStrategy.EXCHANGE_TO_MONO, false}, |
| 56 | + {"jetty", Clients.jettyClient(), RequestStrategy.RETRIEVE, false}, |
| 57 | + {"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE, true}, |
| 58 | + {"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE_TO_FLUX, true}, |
| 59 | + {"netty", Clients.nettyClient(), RequestStrategy.EXCHANGE_TO_MONO, true}, |
| 60 | + {"netty", Clients.nettyClient(), RequestStrategy.RETRIEVE, true}, |
| 61 | + {"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE, false}, |
| 62 | + {"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_FLUX, false}, |
| 63 | + {"hc5", Clients.reactiveHttpClient5(), RequestStrategy.EXCHANGE_TO_MONO, false}, |
| 64 | + {"hc5", Clients.reactiveHttpClient5(), RequestStrategy.RETRIEVE, false} |
| 65 | + }; |
| 66 | + } else { |
| 67 | + return new Object[0][0]; |
| 68 | + } |
61 | 69 | } |
62 | 70 |
|
63 | 71 | @Override |
@@ -86,55 +94,58 @@ protected enum RequestStrategy { |
86 | 94 | EXCHANGE { |
87 | 95 | @Override |
88 | 96 | @SuppressWarnings("deprecation") |
89 | | - void execute(WebClient client, String uri) { |
90 | | - client.get().uri(uri).exchange() // deprecated API |
| 97 | + void execute(Object client, String uri) { |
| 98 | + ((WebClient) client).get().uri(uri).exchange() // deprecated API |
91 | 99 | .block(); |
92 | 100 | } |
93 | 101 | }, |
94 | 102 | EXCHANGE_TO_FLUX { |
95 | 103 | @Override |
96 | | - void execute(WebClient client, String uri) { |
97 | | - client.get().uri(uri).exchangeToFlux(response -> response.bodyToFlux(String.class)).blockLast(); |
| 104 | + void execute(Object client, String uri) { |
| 105 | + ((WebClient) client).get().uri(uri).exchangeToFlux(response -> response.bodyToFlux(String.class)).blockLast(); |
98 | 106 | } |
99 | 107 | }, |
100 | 108 | EXCHANGE_TO_MONO { |
101 | 109 | // TODO |
102 | 110 | @Override |
103 | | - void execute(WebClient client, String uri) { |
104 | | - client.get().uri(uri).exchangeToMono(response -> response.bodyToMono(String.class)).block(); |
| 111 | + void execute(Object client, String uri) { |
| 112 | + ((WebClient) client).get().uri(uri).exchangeToMono(response -> response.bodyToMono(String.class)).block(); |
105 | 113 | } |
106 | 114 | }, |
107 | 115 | RETRIEVE { |
108 | 116 | @Override |
109 | | - void execute(WebClient client, String uri) { |
110 | | - client.get().uri(uri).retrieve().bodyToMono(String.class).block(); |
| 117 | + void execute(Object client, String uri) { |
| 118 | + ((WebClient) client).get().uri(uri).retrieve().bodyToMono(String.class).block(); |
111 | 119 | } |
112 | 120 | }; |
113 | 121 |
|
114 | | - abstract void execute(WebClient client, String uri); |
115 | | - } |
116 | | - |
117 | | - private static WebClient jettyClient() { |
118 | | - return WebClient.builder() |
119 | | - .clientConnector(new JettyClientHttpConnector()) |
120 | | - .build(); |
121 | | - } |
122 | | - |
123 | | - private static WebClient nettyClient() { |
124 | | - HttpClient httpClient = HttpClient.create() |
125 | | - // followRedirect(boolean) only enables redirect for 30[1278], not 303 |
126 | | - .followRedirect((req, res) -> res.status().code() == 303); |
127 | | - |
128 | | - // crete netty reactor client |
129 | | - return WebClient.builder() |
130 | | - .clientConnector(new ReactorClientHttpConnector(httpClient)) |
131 | | - .build(); |
| 122 | + abstract void execute(Object client, String uri); |
132 | 123 | } |
133 | 124 |
|
134 | | - public static WebClient reactiveHttpClient5() { |
135 | | - return WebClient.builder() |
136 | | - .clientConnector(new HttpComponentsClientHttpConnector()) |
137 | | - .build(); |
| 125 | + public static class Clients { |
| 126 | + |
| 127 | + private static Object jettyClient() { |
| 128 | + return WebClient.builder() |
| 129 | + .clientConnector(new JettyClientHttpConnector()) |
| 130 | + .build(); |
| 131 | + } |
| 132 | + |
| 133 | + private static Object nettyClient() { |
| 134 | + HttpClient httpClient = HttpClient.create() |
| 135 | + // followRedirect(boolean) only enables redirect for 30[1278], not 303 |
| 136 | + .followRedirect((req, res) -> res.status().code() == 303); |
| 137 | + |
| 138 | + // crete netty reactor client |
| 139 | + return WebClient.builder() |
| 140 | + .clientConnector(new ReactorClientHttpConnector(httpClient)) |
| 141 | + .build(); |
| 142 | + } |
| 143 | + |
| 144 | + public static Object reactiveHttpClient5() { |
| 145 | + return WebClient.builder() |
| 146 | + .clientConnector(new HttpComponentsClientHttpConnector()) |
| 147 | + .build(); |
| 148 | + } |
138 | 149 | } |
139 | 150 |
|
140 | 151 | } |
0 commit comments