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

Close streams fetched from data-providers #18552

Merged
merged 6 commits into from
Jan 30, 2024

Conversation

heruan
Copy link
Member

@heruan heruan commented Jan 26, 2024

This is an attempt to fix #18551 for the DataCommunicator API by handling the streams fetched from data-providers inside try-with-resources blocks.

Unclosed streams might still leak from the DataView API, that should be fixed in each component using that API.

@heruan heruan self-assigned this Jan 26, 2024
Copy link

github-actions bot commented Jan 26, 2024

Test Results

1 092 files  ± 0  1 092 suites  ±0   1h 25m 14s ⏱️ + 2m 40s
6 828 tests + 7  6 782 ✅ + 7  46 💤 ±0  0 ❌ ±0 
7 157 runs   - 13  7 099 ✅  - 13  58 💤 ±0  0 ❌ ±0 

Results for commit c513d13. ± Comparison against base commit dc68b28.

♻️ This comment has been updated with latest results.

@mcollovati mcollovati added the Contribution PRs coming from the community or external to the team label Jan 29, 2024
@vaadin-bot vaadin-bot added +0.0.1 and removed +1.0.0 labels Jan 29, 2024
@mcollovati
Copy link
Collaborator

The change looks good to me 👍
Two points we should probably consider:

  • Updating DataView javadoc to suggest the user to use the returned stream in combination with try-with-resource to prevent leaking resources (as noted in the PR description). Possibly adding a simple code example.
  • Make HierarchicalDataCommunicator work consistently, by applying try-with-resource where possible also in HierarchyMapper (e.g. getFlatChildrenStream()) and HierarchicalCommunicationController (e.g. activate method).

@heruan
Copy link
Member Author

heruan commented Jan 29, 2024

  • Updating DataView javadoc to suggest the user to use the returned stream in combination with try-with-resource to prevent leaking resources (as noted in the PR description). Possibly adding a simple code example.

Updated the JavaDoc with the recommendation and code example.

  • Make HierarchicalDataCommunicator work consistently, by applying try-with-resource where possible also in HierarchyMapper (e.g. getFlatChildrenStream()) and HierarchicalCommunicationController (e.g. activate method).

Closing streams also for hierarchical communicators and mapper now. I'm just having a hard time understanding how unit tests work for the hierarchical API, I added one covering HierarchicalCommunicationController::activate and HierarchyMapper::getChildrenStream but I couldn't hit HierarchyMapper::getFlatChildrenStream.

@mcollovati
Copy link
Collaborator

@heruan great work!
I think we can test HierarchyMapper::getFlatChildrenStream indirectly in HierarchyMapperWithDataTest by calling HierarchyMapper.fetchHierarchyItems()

Something like

    @Test
    public void fetchHierarchyItems_streamIsClosed() {
        AtomicBoolean streamIsClosed = new AtomicBoolean();
        mapper = new HierarchyMapper<>(new TreeDataProvider<>(data) {
            @Override
            public Stream<Node> fetchChildren(HierarchicalQuery<Node, SerializablePredicate<Node>> query) {
                return super.fetchChildren(query)
                        .onClose(() -> streamIsClosed.set(true));
            }
        });
        Node rootNode = testData.get(0);
        mapper.expand(rootNode);
        mapper.fetchHierarchyItems(rootNode, Range.between(0, 10)).count();

        Assert.assertTrue(streamIsClosed.get());
    }

@heruan
Copy link
Member Author

heruan commented Jan 30, 2024

@heruan great work! I think we can test HierarchyMapper::getFlatChildrenStream indirectly in HierarchyMapperWithDataTest by calling HierarchyMapper.fetchHierarchyItems()

Oh there it is, thanks! I added two tests to HierarchyMapperWithDataTest for consistency, one for getFlatChildrenStream and one for getChildrenStream.

Copy link

sonarcloud bot commented Jan 30, 2024

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@mcollovati mcollovati merged commit ec47e68 into main Jan 30, 2024
25 of 26 checks passed
@mcollovati mcollovati deleted the fix/close-data-provider-streams branch January 30, 2024 09:34
vaadin-bot pushed a commit that referenced this pull request Jan 30, 2024
Fixes DataCommunicator API by handling the streams fetched from data-providers inside try-with-resources blocks.
Unclosed streams might still leak from the DataView API, that should be fixed in each component using that API.

Fixes #18551
vaadin-bot added a commit that referenced this pull request Jan 30, 2024
Fixes DataCommunicator API by handling the streams fetched from data-providers inside try-with-resources blocks.
Unclosed streams might still leak from the DataView API, that should be fixed in each component using that API.

Fixes #18551

Co-authored-by: Giovanni Lovato <giovanni@vaadin.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cherry-picked-24.3 Contribution PRs coming from the community or external to the team target/24.3 +0.0.1
Projects
Development

Successfully merging this pull request may close these issues.

Streams fetched by data-providers are never closed
3 participants