Skip to content

Commit

Permalink
Fix copyrights. Add tests. Change how web client is initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
barchetta committed Mar 26, 2024
1 parent c75d268 commit a9ae9d7
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.examples.webserver.threads;


import io.helidon.logging.common.LogConfig;
import io.helidon.config.Config;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.http.HttpRouting;




/**
* The application main class.
*/
public class Main {

static WebServer webserver;
static WebClient webclient;

/**
* Cannot be instantiated.
*/
private Main() {
}


/**
* Application main entry point.
* @param args command line arguments.
Expand All @@ -36,16 +49,17 @@ public static void main(String[] args) {
Config config = Config.create();
Config.global(config);


WebServer server = WebServer.builder()
webserver = WebServer.builder()
.config(config.get("server"))
.routing(Main::routing)
.build()
.start();

webclient = WebClient.builder()
.baseUri("http://localhost:" + webserver.port() + "/thread")
.build();

System.out.println("WEB server is up! http://localhost:" + server.port() + "/thread");

System.out.println("WEB server is up! http://localhost:" + webserver.port() + "/thread");
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.examples.webserver.threads;

import java.util.ArrayList;
Expand Down Expand Up @@ -28,10 +44,6 @@ class ThreadService implements HttpService {
// Executor of virtual threads.
private static ExecutorService virtualExecutorService;

WebClient client = WebClient.builder()
.baseUri("http://localhost:8080/thread")
.build();

/**
* The config value for the key {@code greeting}.
*/
Expand Down Expand Up @@ -100,7 +112,7 @@ private void computeHandler(ServerRequest request, ServerResponse response) {
}

/**
* Sleep for a specified number of secons.
* Sleep for a specified number of seconds.
* The optional path parameter controls the number of seconds to sleep. Defaults to 1
*
* @param request server request
Expand Down Expand Up @@ -149,13 +161,14 @@ private void fanOutHandler(ServerRequest request, ServerResponse response) {
}

/**
* Simulate a remote client call be calling the sleep endpoint on ourself.
* Simulate a remote client call be calling this server's sleep endpoint
*
* @param seconds number of seconds the endpoint should sleep.
* @return
* @return string response from client
*/
private String callRemote(int seconds) {
LOGGER.log(Level.INFO, Thread.currentThread() + ": Calling remote sleep for " + seconds + "s");
WebClient client = Main.webclient;
try (HttpClientResponse response = client.get("/sleep/" + seconds).request()) {
if (response.status().equals(Status.OK_200)) {
return response.as(String.class);
Expand All @@ -168,7 +181,7 @@ private String callRemote(int seconds) {
/**
* Sleep current thread
* @param seconds number of seconds to sleep
* @return
* @return number of seconds requested to sleep
*/
private int sleep(int seconds) {
try {
Expand All @@ -182,6 +195,7 @@ private int sleep(int seconds) {
/**
* Perform a CPU intensive computation
* @param iterations: number of times to perform computation
* @return result of computation
*/
private double compute(int iterations) {
LOGGER.log(Level.INFO, Thread.currentThread() + ": Computing with " + iterations + " iterations");
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.examples.webserver.threads;
33 changes: 24 additions & 9 deletions examples/webserver/threads/src/main/resources/application.yaml
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
#
# Copyright (c) 2024 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

server:
port: 8080
host: 0.0.0.0

app:
greeting: "Hello"
application-platform-executor:
thread-name-prefix: "application-platform-executor-"
core-pool-size: 1
max-pool-size: 2
queue-capacity: 10
application-virtual-executor:
thread-name-prefix: "application-virtual-executor-"
virtual-threads: true
application-platform-executor:
thread-name-prefix: "application-platform-executor-"
core-pool-size: 1
max-pool-size: 2
queue-capacity: 10
application-virtual-executor:
thread-name-prefix: "application-virtual-executor-"
virtual-threads: true
16 changes: 16 additions & 0 deletions examples/webserver/threads/src/main/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
#
# Copyright (c) 2024 Oracle and/or its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

handlers=io.helidon.logging.jul.HelidonConsoleHandler
java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS.%1$tL %5$s%6$s%n
# Global logging level. Can be overridden by specific loggers
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,57 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.helidon.examples.webserver.threads;

import io.helidon.webserver.testing.junit5.RoutingTest;
import io.helidon.http.Status;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.http.HttpRouting;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

@RoutingTest
@ServerTest
class MainTest {
MainTest() {
private final WebClient client;

protected MainTest(WebClient client) {
this.client = client;
Main.webclient = this.client; // Needed for ThreadService to make calls
}

@SetUpRoute
static void routing(HttpRouting.Builder builder) {
Main.routing(builder);
}

@Test
void testFanOut() {
try (HttpClientResponse response = client.get("/thread/fanout/2").request()) {
assertThat(response.status(), is(Status.OK_200));
}
}

@Test
void testCompute() {
try (HttpClientResponse response = client.get("/thread/compute").request()) {
assertThat(response.status(), is(Status.OK_200));
}
}
}

0 comments on commit a9ae9d7

Please sign in to comment.