-
Notifications
You must be signed in to change notification settings - Fork 230
#26 Integretion Testing Initial Example #74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
57b4aed
#26 Add Hello World Integration Testing working on Windows, need work…
JuanJose-Herrera e1e18b0
#26 Add new Integration Test to test DAPR state functionality
JuanJose-Herrera 5ffcf7a
#26 Add Hello World Integration Testing working on Windows, need work…
JuanJose-Herrera 8988766
Update Integration Testing getting free ports automatically
JuanJose-Herrera c17ae3b
Merge branch 'java_sdk_wip' into java_sdk_wip
youngbupark 3d083d0
Merge branch 'java_sdk_wip' into java_sdk_wip
artursouza d82821d
#26 Refractor to use a base class for all the integration tests
JuanJose-Herrera 3fe5eb4
Merge remote-tracking branch 'origin/java_sdk_wip' into java_sdk_wip
JuanJose-Herrera b6d4afd
#26 Make StateOptions as optional in order to not throw a null pointe…
JuanJose-Herrera ae66c4e
#26 Remove empty lines and correct the ident
JuanJose-Herrera 01e3068
Merge branch 'java_sdk_wip' into java_sdk_wip
artursouza cfcca1d
Adding license to DaprIntegrationTestingRunner
artursouza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,81 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.it; | ||
|
|
||
| import org.junit.AfterClass; | ||
| import org.junit.BeforeClass; | ||
| import org.junit.ClassRule; | ||
| import org.junit.contrib.java.lang.system.EnvironmentVariables; | ||
|
|
||
| import java.util.Optional; | ||
|
|
||
| import static io.dapr.it.DaprIntegrationTestingRunner.DAPR_FREEPORTS; | ||
|
|
||
| public class BaseIT { | ||
|
|
||
| protected static DaprIntegrationTestingRunner daprIntegrationTestingRunner; | ||
|
|
||
|
|
||
| @ClassRule | ||
| public static final EnvironmentVariables environmentVariables = new EnvironmentVariables(); | ||
|
|
||
| @BeforeClass | ||
| public static void setEnvironmentVariables(){ | ||
| environmentVariables.set("DAPR_HTTP_PORT", String.valueOf(DAPR_FREEPORTS.getHttpPort())); | ||
| } | ||
|
|
||
| public static DaprIntegrationTestingRunner createDaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { | ||
| return new DaprIntegrationTestingRunner(successMessage, serviceClass, useAppPort, sleepTime); | ||
| } | ||
|
|
||
| @AfterClass | ||
| public static void cleanUp() { | ||
| Optional.ofNullable(daprIntegrationTestingRunner).ifPresent(daprRunner -> daprRunner.destroyDapr()); | ||
| } | ||
|
|
||
| public static class MyData { | ||
|
|
||
| /// Gets or sets the value for PropertyA. | ||
| private String propertyA; | ||
|
|
||
| /// Gets or sets the value for PropertyB. | ||
| private String propertyB; | ||
|
|
||
| private MyData myData; | ||
|
|
||
| public String getPropertyB() { | ||
| return propertyB; | ||
| } | ||
|
|
||
| public void setPropertyB(String propertyB) { | ||
| this.propertyB = propertyB; | ||
| } | ||
|
|
||
| public String getPropertyA() { | ||
| return propertyA; | ||
| } | ||
|
|
||
| public void setPropertyA(String propertyA) { | ||
| this.propertyA = propertyA; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "MyData{" + | ||
| "propertyA='" + propertyA + '\'' + | ||
| ", propertyB='" + propertyB + '\'' + | ||
| '}'; | ||
| } | ||
|
|
||
| public MyData getMyData() { | ||
| return myData; | ||
| } | ||
|
|
||
| public void setMyData(MyData myData) { | ||
| this.myData = myData; | ||
| } | ||
| } | ||
| } |
146 changes: 146 additions & 0 deletions
146
sdk/src/test/java/io/dapr/it/DaprIntegrationTestingRunner.java
This file contains hidden or 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,146 @@ | ||
| /* | ||
| * Copyright (c) Microsoft Corporation. | ||
| * Licensed under the MIT License. | ||
| */ | ||
|
|
||
| package io.dapr.it; | ||
|
|
||
| import org.junit.Assert; | ||
|
|
||
| import java.io.*; | ||
| import java.net.ServerSocket; | ||
| import java.util.Optional; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.*; | ||
|
|
||
|
|
||
| public class DaprIntegrationTestingRunner { | ||
|
|
||
| public static DaprIntegrationTestingRunner.DaprFreePorts DAPR_FREEPORTS; | ||
|
|
||
| static { | ||
| try { | ||
| DAPR_FREEPORTS = new DaprIntegrationTestingRunner.DaprFreePorts().initPorts(); | ||
| } catch (Exception e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| private Runtime rt = Runtime.getRuntime(); | ||
| private Process proc; | ||
|
|
||
| private String successMessage; | ||
| private Class serviceClass; | ||
| private Boolean useAppPort; | ||
| private int sleepTime; | ||
| private String appName; | ||
|
|
||
| DaprIntegrationTestingRunner(String successMessage, Class serviceClass, Boolean useAppPort, int sleepTime) { | ||
| this.successMessage = successMessage; | ||
| this.serviceClass = serviceClass; | ||
| this.useAppPort = useAppPort; | ||
| this.sleepTime = sleepTime; | ||
| this.generateAppName(); | ||
| } | ||
|
|
||
| public DaprFreePorts initializeDapr() throws Exception { | ||
| String daprCommand=this.buildDaprCommand(); | ||
| System.out.println(daprCommand); | ||
| proc= rt.exec(daprCommand); | ||
|
|
||
| final Runnable stuffToDo = new Thread(() -> { | ||
| try { | ||
| try (InputStream stdin = proc.getInputStream()) { | ||
| try(InputStreamReader isr = new InputStreamReader(stdin)) { | ||
| try (BufferedReader br = new BufferedReader(isr)){ | ||
| String line; | ||
| while ((line = br.readLine()) != null) { | ||
| System.out.println(line); | ||
| if (line.contains(successMessage)) { | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } | ||
| } catch (IOException ex) { | ||
| Assert.fail(ex.getMessage()); | ||
| } | ||
| }); | ||
|
|
||
| final ExecutorService executor = Executors.newSingleThreadExecutor(); | ||
| final Future future = executor.submit(stuffToDo); | ||
| executor.shutdown(); // This does not cancel the already-scheduled task. | ||
| future.get(1, TimeUnit.MINUTES); | ||
| Thread.sleep(sleepTime); | ||
| return DAPR_FREEPORTS; | ||
| } | ||
|
|
||
| private static final String DAPR_RUN = "dapr run --app-id %s "; | ||
| private static final String DAPR_COMMAND = " -- mvn exec:java -D exec.mainClass=%s -D exec.classpathScope=\"test\" -Dexec.args=\"-p %d -grpcPort %d -httpPort %d\""; | ||
|
|
||
| private String buildDaprCommand(){ | ||
| StringBuilder stringBuilder= new StringBuilder(String.format(DAPR_RUN, this.appName)) | ||
| .append(this.useAppPort ? "--app-port " + this.DAPR_FREEPORTS.appPort : "") | ||
| .append(" --grpc-port ") | ||
| .append(this.DAPR_FREEPORTS.grpcPort) | ||
| .append(" --port ") | ||
| .append(this.DAPR_FREEPORTS.httpPort) | ||
| .append(String.format(DAPR_COMMAND, this.serviceClass.getCanonicalName(),this.DAPR_FREEPORTS.appPort, this.DAPR_FREEPORTS.grpcPort, this.DAPR_FREEPORTS.httpPort)); | ||
| return stringBuilder.toString(); | ||
| } | ||
|
|
||
| private void generateAppName(){ | ||
|
|
||
| this.appName=UUID.randomUUID().toString(); | ||
| } | ||
|
|
||
| private static Integer findRandomOpenPortOnAllLocalInterfaces() throws Exception { | ||
| try ( | ||
| ServerSocket socket = new ServerSocket(0) | ||
| ) { | ||
| return socket.getLocalPort(); | ||
|
|
||
| } | ||
| } | ||
|
|
||
| public void destroyDapr() { | ||
| Optional.ofNullable(rt).ifPresent( runtime -> { | ||
| try { | ||
| runtime.exec("dapr stop --app-id " + this.appName); | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
| Optional.ofNullable(proc).ifPresent(process -> process.destroy()); | ||
| } | ||
|
|
||
| public static class DaprFreePorts | ||
| { | ||
| public DaprFreePorts initPorts() throws Exception { | ||
| this.appPort= findRandomOpenPortOnAllLocalInterfaces(); | ||
| this.grpcPort= findRandomOpenPortOnAllLocalInterfaces(); | ||
| this.httpPort= findRandomOpenPortOnAllLocalInterfaces(); | ||
| return this; | ||
| } | ||
|
|
||
| private int grpcPort; | ||
|
|
||
| public int getGrpcPort() { | ||
| return grpcPort; | ||
| } | ||
|
|
||
| public int getHttpPort() { | ||
| return httpPort; | ||
| } | ||
|
|
||
| public int getAppPort() { | ||
| return appPort; | ||
| } | ||
|
|
||
| private int httpPort; | ||
|
|
||
| private int appPort; | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep single spaces.