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]: Bad sad error when the ballerina/test import is below the import of a module of the mocked object #42037

Closed
gayaldassanayake opened this issue Jan 24, 2024 · 4 comments
Assignees
Labels
Area/TestFramework Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug

Comments

@gayaldassanayake
Copy link
Contributor

Description

Say we have 2 modules db and user. db has a db client and it is being mocked in the tests of user. When the import of ballerina/test module is above the import of db, there is a bad sad error.

ballerina: Oh no, something really went wrong. Bad. Sad.

We appreciate it if you can report the code that broke Ballerina in
https://github.com/ballerina-platform/ballerina-lang/issues with the
log you get below and your sample code.

We thank you for helping make us better.

[2024-01-24 12:07:24,404] SEVERE {b7a.log.crash} - Cannot invoke "io.ballerina.runtime.internal.values.MapValue.containsKey(Object)" because "globalAnnotMap" is null 
java.lang.NullPointerException: Cannot invoke "io.ballerina.runtime.internal.values.MapValue.containsKey(Object)" because "globalAnnotMap" is null
        at io.ballerina.runtime.internal.AnnotationUtils.processFPValueAnnotations(AnnotationUtils.java:126)
        at gayaldassanayake.choreo_issue$0046A$test.0.tests.a_test.getMockClient(tests/a_test.bal:10)
        at gayaldassanayake.choreo_issue$0046db.0.db.getClient(db.bal)
        at gayaldassanayake.choreo_issue$0046db.0.$_init.$gen$$0046$0060init$0062(choreo_issue.db:4)
        at gayaldassanayake.choreo_issue$0046A$test.0.$_init.$moduleInit(choreo_issue.A)
        at gayaldassanayake.choreo_issue$0046A$test.0.$_init.$moduleExecute(choreo_issue.A)
        at gayaldassanayake.choreo_issue$0046A$test.0.$_init.$lambda$$moduleExecute$(choreo_issue.A)
        at io.ballerina.runtime.internal.scheduling.SchedulerItem.execute(SchedulerItem.java:54)
        at io.ballerina.runtime.internal.scheduling.Scheduler.run(Scheduler.java:306)
        at io.ballerina.runtime.internal.scheduling.Scheduler.runSafely(Scheduler.java:273)
        at java.base/java.lang.Thread.run(Thread.java:833)

Steps to Reproduce

modules/db/db.bal

import ballerina/sql;
import ballerinax/mssql;

mssql:Client dbClient = check getClient();

function getClient() returns mssql:Client|error {
    return new (
        "localhost",
        "config:database.user",
        "config:database.password",
        "billing_db",
        1443
    );
}

public function getValue() returns int {
    sql:ParameterizedQuery query = `INSERT INTO users (name, age) VALUES ("Nimal", 23)`;
    [return](sql:ExecutionResult|sql:Error) value = dbClient->execute(query);
    if (value is sql:ExecutionResult) {
        return 1;
    }
    return 0;
}

modules/user/test/testUser.bal

import choreo_issue.db as _;
import ballerina/test;
import ballerinax/mssql;

@test:Mock {
    moduleName: "choreo_issue.db",
    functionName: "getClient"
}
function getMockClient() returns mssql:Client|error {
    return test:mock(mssql:Client);
}

// Tests. removed for brevity

If we change the order of imports in testUser.bal, i.e.

import ballerina/test;
import choreo_issue.db as _;
import ballerinax/mssql;

then the error is not there.

An explanation for this issue is given in https://github.com/wso2-enterprise/internal-support-ballerina/issues/504#issuecomment-1809568898

Affected Version(s)

No response

OS, DB, other environment details and versions

No response

Related area

-> Test Framework

Related issue(s) (optional)

https://github.com/wso2-enterprise/internal-support-ballerina/issues/567
https://github.com/wso2-enterprise/internal-support-ballerina/issues/504

Suggested label(s) (optional)

No response

Suggested assignee(s) (optional)

No response

@gayaldassanayake gayaldassanayake added Type/Bug Area/TestFramework Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) labels Jan 24, 2024
@gayaldassanayake
Copy link
Contributor Author

The same issue is reported with #41706

@ShammiL ShammiL self-assigned this Jan 26, 2024
@ShammiL ShammiL moved this from BackLog to In Progress in Ballerina Team Main Board Jan 26, 2024
@ShammiL ShammiL moved this from In Progress to PR Sent in Ballerina Team Main Board Jan 26, 2024
@ShammiL ShammiL moved this from PR Sent to In Progress in Ballerina Team Main Board Jan 29, 2024
@ShammiL
Copy link
Contributor

ShammiL commented Jan 31, 2024

This failure is caused because the moduleInit execution order depends on the module imports order. Unless there are any dependencies between module imports the import order remains unchanged from the user defined order.

Hence in this case, when the package module db is imported before the ballerina/test, db source module init is loaded before loading the test module init. Calling test:mock(mssql:Client) inside the db module requires a global annotation map from the test module to be initiated but due to the order of init loading a null value is accessed instead, causing the noticed NPE.
When the import order is changed, this failure won't be noticed as the init loading happens for the test module first.

While this is a limitation of the moduleInit execution we will be working on having these issues resolved in the upcoming test mocking redesign effort.

@ShammiL ShammiL moved this from In Progress to BackLog in Ballerina Team Main Board Feb 1, 2024
@ShammiL
Copy link
Contributor

ShammiL commented Feb 22, 2024

Closing this as a workaround was provided till the new mock interface in implemented.
The progress of the mock interface design can be tracked in #42193

@ShammiL ShammiL closed this as completed Feb 22, 2024
@github-project-automation github-project-automation bot moved this from BackLog to Done in Ballerina Team Main Board Feb 22, 2024
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
Area/TestFramework Reason/EngineeringMistake The issue occurred due to a mistake made in the past. Team/DevTools Ballerina Developer Tooling ( CLI, Test FW, Package Management, OpenAPI, APIDocs ) Type/Bug
Projects
Archived in project
Development

No branches or pull requests

2 participants