Skip to content
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

feat: add support to run keploy v2 with unit tests #154

Merged
merged 2 commits into from
Sep 4, 2023
Merged

Conversation

gouravkrosx
Copy link
Member

@gouravkrosx gouravkrosx commented Aug 19, 2023

Related Issue

Describe the changes you've made

  • Added KeployCLI class which contains global functions to run keploy tests along with junit tests.

  • To run keploy tests along with exisiting junit tests, you just have to add this Test function in your test file.

 @Test
    @Order(Integer.MAX_VALUE)
    public void TestKeploy() throws InterruptedException {
        System.out.println("Running keploy tests along with unit tests");
        long pid = ProcessHandle.current().pid();
        System.out.println("Sending app pid to keploy:" + pid);

        Boolean testResult = true;
        try {
            // Start your keploy server
            Thread keployThread = new Thread(() -> {
                try {
                    System.out.println("Starting keploy server");
                    RunKeployServer(pid, 20, "./", 0);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });

            keployThread.setDaemon(true);
            keployThread.start();
            // wait for the keploy to load hooks
            Thread.sleep(10000);

            System.out.println("Trying to fetch test sets");
            String[] testSets = FetchTestSets();
            if (testSets == null) {
                return;
            }

            System.out.println("TestSets: " + Arrays.asList(testSets));

            System.out.println("starting user application");

            boolean result = true;
            for (String testset : testSets) {

               String testRunId = RunTestSet(testset);

                // Start User application
                ExecutorService executor = Executors.newFixedThreadPool(1);

                // Submit tasks to the executor
                executor.submit(() -> {
                    try {
                        System.out.println("now starting");
                        SamplesJavaApplication.main(new String[]{""});
                    } catch (Exception e) {
                        System.out.println("got error while starting user application");
                        throw new RuntimeException(e);
                    }
                });

                KeployCLI.TestRunStatus testRunStatus;

               while (true) {

                   // check status every 2 sec
                   Thread.sleep(2000);
                   testRunStatus = FetchTestSetStatus(testRunId);
                   if (testRunStatus == TestRunStatus.RUNNING) {
                       System.out.println("testRun still in progress");
                       continue;
                   }
                   break;
               }

                
                // Shutdown the executor
                executor.shutdown();
                try {
                    if (!executor.awaitTermination(5, TimeUnit.SECONDS)) {
                        executor.shutdownNow();
                    }
                } catch (InterruptedException e) {
                    executor.shutdownNow();
                }
                Thread.sleep(5);
                // Stop User Application
                StopApplication();

                if (testRunStatus == TestRunStatus.FAILED) {
                    System.out.println("testrun failed");
                    result = false;
                } else if (testRunStatus == TestRunStatus.PASSED) {
                    System.out.println("testrun passed");
                    result = true;
                }

                System.out.println("TestResult of [" + testset + "]:" + result);
                testResult = testResult && result;

            }
        } catch (Exception e) {
            System.err.println("failed to execute keploy tests:" + e);
        } finally {
            System.out.println("Testing done with status:" + testResult);
        }

        System.out.println("Asserting on testResult:" + testResult);

        Thread stopKeploy = new Thread(() -> {
            System.out.println("Stopping keploy server");
            StopKeployServer();
        });
        stopKeploy.start();
        stopKeploy.join();
        assertTrue(testResult, "Keploy Test Result");
    }

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Code style update (formatting, local variables)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Please describe the tests(if any). Provide instructions how its affecting the coverage.

Describe if there is any unusual behaviour of your code(Write NA if there isn't)

NA

Checklist:

  • My code follows the style guidelines of this project.
  • I have performed a self-review of my own code.
  • I have commented my code, particularly in hard-to-understand areas and used java doc.
  • I have made corresponding changes to the documentation.
  • My changes generate no new warnings.
  • I have added tests that prove my fix is effective or that my feature works.
  • New and existing unit tests pass locally with my changes.

Screenshots (if any)

Original Updated
original screenshot updated screenshot

Signed-off-by: gouravkrosx <gouravgreatkr@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

checkstyle

v2/src/main/java/io.keploy.cli/KeployCLI.java|52 col 9| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|55| Line is longer than 80 characters (found 87).
v2/src/main/java/io.keploy.cli/KeployCLI.java|60| Line is longer than 80 characters (found 129).
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 24| Name 'RunKeployServer' must match pattern '^[a-z][a-zA-Z0-9]$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 40| Parameter pid should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 50| Parameter delay should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 61| Parameter testPath should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|60 col 78| Parameter port should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|66 col 30| '1000' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|74| Line is longer than 80 characters (found 93).
v2/src/main/java/io.keploy.cli/KeployCLI.java|75| Line is longer than 80 characters (found 167).
v2/src/main/java/io.keploy.cli/KeployCLI.java|112| Line is longer than 80 characters (found 112).
v2/src/main/java/io.keploy.cli/KeployCLI.java|114| Line is longer than 80 characters (found 82).
v2/src/main/java/io.keploy.cli/KeployCLI.java|140 col 33| '15000' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|141 col 36| '10000' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|143| Line is longer than 80 characters (found 87).
v2/src/main/java/io.keploy.cli/KeployCLI.java|155 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|155 col 28| Name 'FetchTestSets' must match pattern '^[a-z][a-zA-Z0-9]
$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|178| Line is longer than 80 characters (found 89).
v2/src/main/java/io.keploy.cli/KeployCLI.java|180| Line is longer than 80 characters (found 85).
v2/src/main/java/io.keploy.cli/KeployCLI.java|191 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|191 col 33| Name 'FetchTestSetStatus' must match pattern '^[a-z][a-zA-Z0-9]$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|191 col 52| Parameter testRunId should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|200| Line is longer than 80 characters (found 93).
v2/src/main/java/io.keploy.cli/KeployCLI.java|218| Line is longer than 80 characters (found 89).
v2/src/main/java/io.keploy.cli/KeployCLI.java|239 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|239 col 26| Name 'RunTestSet' must match pattern '^[a-z][a-zA-Z0-9]
$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|239 col 37| Parameter testSetName should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|247| Line is longer than 80 characters (found 116).
v2/src/main/java/io.keploy.cli/KeployCLI.java|265| Line is longer than 80 characters (found 89).
v2/src/main/java/io.keploy.cli/KeployCLI.java|278| Line is longer than 80 characters (found 92).
v2/src/main/java/io.keploy.cli/KeployCLI.java|278 col 49| Parameter conn should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|280 col 33| '200' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|280 col 55| '300' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|283| Line is longer than 80 characters (found 94).
v2/src/main/java/io.keploy.cli/KeployCLI.java|283 col 51| Parameter conn should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|284| Line is longer than 80 characters (found 93).
v2/src/main/java/io.keploy.cli/KeployCLI.java|296 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|296 col 24| Name 'StopKeployServer' must match pattern '^[a-z][a-zA-Z0-9]*$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|301 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|301 col 42| Parameter port should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|302 col 65| ',' is not followed by whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|307| Line is longer than 80 characters (found 104).
v2/src/main/java/io.keploy.cli/KeployCLI.java|317 col 47| Parameter pid should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|319 col 36| '+' is not followed by whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|319 col 36| '+' is not preceded with whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|320 col 42| ',' is not followed by whitespace.

v2/pom.xml Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
v2/src/main/java/io.keploy.cli/KeployCLI.java Show resolved Hide resolved
Signed-off-by: gouravkrosx <gouravgreatkr@gmail.com>
Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remaining comments which cannot be posted as a review comment to avoid GitHub Rate Limit

checkstyle

v2/src/main/java/io.keploy.cli/KeployCLI.java|265| Line is longer than 80 characters (found 89).
v2/src/main/java/io.keploy.cli/KeployCLI.java|278| Line is longer than 80 characters (found 92).
v2/src/main/java/io.keploy.cli/KeployCLI.java|278 col 49| Parameter conn should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|280 col 33| '200' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|280 col 55| '300' is a magic number.
v2/src/main/java/io.keploy.cli/KeployCLI.java|283| Line is longer than 80 characters (found 94).
v2/src/main/java/io.keploy.cli/KeployCLI.java|283 col 51| Parameter conn should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|284| Line is longer than 80 characters (found 93).
v2/src/main/java/io.keploy.cli/KeployCLI.java|296 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|296 col 24| Name 'StopKeployServer' must match pattern '^[a-z][a-zA-Z0-9]*$'.
v2/src/main/java/io.keploy.cli/KeployCLI.java|301 col 5| Missing a Javadoc comment.
v2/src/main/java/io.keploy.cli/KeployCLI.java|301 col 42| Parameter port should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|302 col 65| ',' is not followed by whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|307| Line is longer than 80 characters (found 104).
v2/src/main/java/io.keploy.cli/KeployCLI.java|317 col 47| Parameter pid should be final.
v2/src/main/java/io.keploy.cli/KeployCLI.java|319 col 36| '+' is not followed by whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|319 col 36| '+' is not preceded with whitespace.
v2/src/main/java/io.keploy.cli/KeployCLI.java|320 col 42| ',' is not followed by whitespace.

public enum TestRunStatus {
RUNNING,
PASSED,
FAILED

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocVariableCheck> reported by reviewdog 🐶
Missing a Javadoc comment.

FAILED
}

// public static void main(String[] args) throws IOException, InterruptedException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck> reported by reviewdog 🐶
Line is longer than 80 characters (found 87).



// Run Keploy server
public static void RunKeployServer(long pid, int delay, String testPath, int port) throws InterruptedException, IOException {

This comment was marked as resolved.



// Run Keploy server
public static void RunKeployServer(long pid, int delay, String testPath, int port) throws InterruptedException, IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck> reported by reviewdog 🐶
Missing a Javadoc comment.



// Run Keploy server
public static void RunKeployServer(long pid, int delay, String testPath, int port) throws InterruptedException, IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck> reported by reviewdog 🐶
Name 'RunKeployServer' must match pattern '^[a-z][a-zA-Z0-9]*$'.

logger.debug("response body received: {}", resBody);
// Parse the response body using Gson
Gson gson = new Gson();
GraphQLResponse response = gson.fromJson(resBody, GraphQLResponse.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck> reported by reviewdog 🐶
Line is longer than 80 characters (found 89).



// Run a particular testSet
public static String RunTestSet(String testSetName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.javadoc.MissingJavadocMethodCheck> reported by reviewdog 🐶
Missing a Javadoc comment.



// Run a particular testSet
public static String RunTestSet(String testSetName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.naming.MethodNameCheck> reported by reviewdog 🐶
Name 'RunTestSet' must match pattern '^[a-z][a-zA-Z0-9]*$'.



// Run a particular testSet
public static String RunTestSet(String testSetName) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.FinalParametersCheck> reported by reviewdog 🐶
Parameter testSetName should be final.

}

String payload = String.format(
"{ \"query\": \"mutation { runTestSet(testSet: \\\"%s\\\") { success testRunId message } }\" }",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚫 [checkstyle] <com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck> reported by reviewdog 🐶
Line is longer than 80 characters (found 116).

Copy link
Member

@Sarthak160 Sarthak160 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@Sarthak160 Sarthak160 merged commit 10656e5 into main Sep 4, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants