forked from quarkusio/quarkus
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow using a random test port within Google Cloud Function tests
Fixes quarkusio#35476
- Loading branch information
1 parent
d110270
commit 12e0afc
Showing
5 changed files
with
69 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
...-functions/src/test/java/io/quarkus/gcp/function/test/HttpFunctionRandomPortTestCase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package io.quarkus.gcp.function.test; | ||
|
||
import static io.restassured.RestAssured.when; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.google.cloud.functions.test.FunctionType; | ||
import io.quarkus.google.cloud.functions.test.WithFunction; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
@WithFunction(FunctionType.HTTP) | ||
class HttpFunctionRandomPortTestCase { | ||
|
||
@BeforeAll | ||
public static void setup() { | ||
System.setProperty("quarkus.http.test-port", "0"); | ||
} | ||
|
||
@Test | ||
public void test() { | ||
// test the function using RestAssured | ||
when() | ||
.get() | ||
.then() | ||
.statusCode(200) | ||
.body(is("Hello World!")); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
...ogle-cloud-functions/src/main/java/io/quarkus/google/cloud/functions/test/SocketUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package io.quarkus.google.cloud.functions.test; | ||
|
||
import java.io.IOException; | ||
import java.net.ServerSocket; | ||
|
||
// copied from the mailer extension: io.quarkus.mailer.runtime.SocketUtil | ||
final class SocketUtil { | ||
|
||
private SocketUtil() { | ||
} | ||
|
||
static int findAvailablePort() { | ||
ServerSocket serverSocket = null; | ||
try { | ||
serverSocket = new ServerSocket(0); | ||
return serverSocket.getLocalPort(); | ||
} catch (Exception e) { | ||
// return a default port | ||
return 25347; | ||
} finally { | ||
if (serverSocket != null) { | ||
try { | ||
serverSocket.close(); | ||
} catch (IOException e) { | ||
} | ||
} | ||
} | ||
} | ||
} |