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

[Bug]: bal test does not exit when a module has a listener declaration and a continuously running worker/asynchronous function #41213

Closed
Nadeeshan96 opened this issue Aug 16, 2023 · 3 comments · Fixed by #41210
Assignees
Labels
Priority/High Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Milestone

Comments

@Nadeeshan96
Copy link
Contributor

Description

$title

Steps to Reproduce

Create a Ballerina package and add the following code to a module (e.g. default module).

import ballerina/http;
import ballerina/lang.runtime;

listener http:Listener httpListener = new(8080);

function init() returns error? {
    _ = start infiniteLoop();
}

public isolated function infiniteLoop() {
    while true {
        runtime:sleep(1);
    }
}

public function intAdd(int a, int b) returns (int) {
    return a + b;
}

service / on httpListener {
    resource function get greeting() returns string { 
        return "Hello, World!"; 
    }
}

and add the following Ballerina test (or another Ballerina test) in the tests folder.

import ballerina/test;

@test:Config {}
function intAddTest() {
    test:assertEquals(intAdd(1, 3), 4);
}

Running bal test hangs giving the below output.

Running Tests

        baltestissue


                1 passing
                0 failing
                0 skipped

Affected Version(s)

2201.7.0

OS, DB, other environment details and versions

No response

Related area

-> Runtime

Related issue(s) (optional)

No response

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@Nadeeshan96 Nadeeshan96 added Type/Bug Priority/High Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime labels Aug 16, 2023
@Nadeeshan96 Nadeeshan96 added this to the 2201.8.0 milestone Aug 16, 2023
@Nadeeshan96 Nadeeshan96 self-assigned this Aug 16, 2023
@Nadeeshan96
Copy link
Contributor Author

Nadeeshan96 commented Aug 16, 2023

As a workaround we can do the following by handling the listener dynamically by ourselves.

Edit the module code as follows.

import ballerina/http;
import ballerina/lang.runtime;

// remove the listener keyword here
http:Listener httpListener = check new(8080);

// add the service as an service object declared in the module
http:Service serviceObj = service object {
    resource function get greeting() returns string { 
        return "Hello, World!"; 
    }
};

function detachAndStopListener() returns error? {
    check httpListener.detach(serviceObj);
    check httpListener.gracefulStop();
    runtime:deregisterListener(httpListener);
}

function init() returns error? {
    _ = start infiniteLoop();
    // add the below code to handle the listener dynamically by the developer
    runtime:onGracefulStop(detachAndStopListener);
    check httpListener.attach(serviceObj);
    check httpListener.'start();
    // need to register the listener with the module to activate the listening phase
    runtime:registerListener(httpListener);
}

public isolated function infiniteLoop() {
    while true {
        runtime:sleep(1);
    }
}

public function intAdd(int a, int b) returns (int) {
    return a + b;
}

Call the detachAndStopListener() function in the AfterSuite function in the Ballerina test.

import ballerina/test;

@test:Config {}
function intAddTest() {
    test:assertEquals(intAdd(1, 3), 4);
}

@test:AfterSuite {}
function stopListener() returns error? {
    check detachAndStopListener();
}

Now since we are deregistering the listener and stopping it by ourselves, it does not hang.

@Nadeeshan96
Copy link
Contributor Author

bal test hangs when there is a continuously running worker inside the init function and a test failing. Needs to fix that too.

@github-project-automation github-project-automation bot moved this from In Progress to Done in Ballerina Team Main Board Aug 20, 2023
@Nadeeshan96 Nadeeshan96 added the Reason/EngineeringMistake The issue occurred due to a mistake made in the past. label Aug 20, 2023
@github-actions
Copy link

This issue is NOT closed with a proper Reason/ label. Make sure to add proper reason label before closing. Please add or leave a comment with the proper reason label now.

      - Reason/EngineeringMistake - The issue occurred due to a mistake made in the past.
      - Reason/Regression - The issue has introduced a regression.
      - Reason/MultipleComponentInteraction - Issue occured due to interactions in multiple components.
      - Reason/Complex - Issue occurred due to complex scenario.
      - Reason/Invalid - Issue is invalid.
      - Reason/Other - None of the above cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority/High Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/jBallerina All the issues related to BIR, JVM backend code generation and runtime Type/Bug
Projects
Archived in project
3 participants