Closed pujagani closed 2 days ago
⏱️ Estimated effort to review [1-5] | 3 |
🧪 Relevant tests | No |
🔒 Security concerns | No |
⚡ Key issues to review |
Return Type Change: The method addListener in BiDi.java and Connection.java has been changed to return a long instead of void . This change affects the method signature and any existing code that uses these methods. Ensure that this change is documented and that all usages of these methods in the codebase are updated to handle the new return type. |
Data Structure Change: In Connection.java , the storage structure for event callbacks has been changed from a List to a Map . This change could affect how callbacks are managed and invoked. Review the logic to ensure that callbacks are correctly managed and that there are no memory leaks or errors in callback invocation. |
Category | Suggestion | Score |
Enhancement |
✅ Enhance the removeListener method to clean up empty maps to prevent memory leaks___Suggestion Impact:The commit implemented the suggestion to clean up empty maps from the eventCallbacks map after removing an id to prevent memory leaks. code diff: ```diff + eventCallbacks.forEach( + (k, v) -> { + v.remove(id); + if (v.isEmpty()) { + eventCallbacks.remove(k); + } + }); ```eventCallbacks map to prevent memory leaks. This can be done by checking if the map is empty after removing an id and, if so, removing the map from eventCallbacks .**
[java/src/org/openqa/selenium/bidi/Connection.java [219]](https://github.com/SeleniumHQ/selenium/pull/14215/files#diff-fa4b58e0bb2f950d2559090a4e1f36eb7126c32eeee7d611844d93c0b785670bR219-R219)
```diff
-eventCallbacks.forEach((k, v) -> v.remove(id));
+eventCallbacks.forEach((k, v) -> {
+ v.remove(id);
+ if (v.isEmpty()) {
+ eventCallbacks.remove(k);
+ }
+});
```
- [ ] **Apply this suggestion**
Suggestion importance[1-10]: 10Why: Cleaning up empty maps after removing a listener is essential to prevent memory leaks. This suggestion correctly identifies and addresses a potential issue in the new code, making it highly valuable. | 10 |
Add exception handling around the event subscription command to ensure robustness___ **Ensure that theaddListener method in the BiDi class properly handles exceptions that might occur during the subscription process. This can be achieved by adding a try-catch block around the send method call, which sends the subscription command to the server. This will prevent the method from failing silently if an error occurs during the command transmission.** [java/src/org/openqa/selenium/bidi/BiDi.java [58-60]](https://github.com/SeleniumHQ/selenium/pull/14215/files#diff-f0ed6d655b8c0ff47563631639d01d8f7a5f8e83a40db48456a2e9137e8ab0e6R58-R60) ```diff -send( - new Command<>( - "session.subscribe", Map.of("events", Collections.singletonList(event.getMethod())))); +try { + send( + new Command<>( + "session.subscribe", Map.of("events", Collections.singletonList(event.getMethod())))); +} catch (Exception e) { + // Handle exception appropriately + throw new RuntimeException("Failed to subscribe to event", e); +} ``` - [ ] **Apply this suggestion** Suggestion importance[1-10]: 8Why: Adding exception handling around the event subscription command is a good practice to prevent silent failures and improve robustness. This suggestion correctly addresses a potential issue in the new code. | 8 | |
Possible bug |
Improve the event handler registration logic to support multiple handlers for the same event___ **Modify theaddListener method to ensure that the event handler is added to an existing map if the event is already registered. This avoids overwriting existing handlers for the same event. Use Map.computeIfPresent to add the handler to the existing map instead of creating a new one every time.** [java/src/org/openqa/selenium/bidi/Connection.java [192-198]](https://github.com/SeleniumHQ/selenium/pull/14215/files#diff-fa4b58e0bb2f950d2559090a4e1f36eb7126c32eeee7d611844d93c0b785670bR192-R198) ```diff -eventCallbacks.computeIfAbsent( - event, - key -> { - HashMap Suggestion importance[1-10]: 9Why: This suggestion ensures that multiple handlers can be registered for the same event without overwriting existing handlers, which is crucial for the correct functioning of the event system. The improved code is accurate and enhances the existing functionality. | 9 |
Possible issue |
Ensure that
___
**Refactor the | 7 |
User description
This is the base for implementing the high-level BiDi API
Thanks for contributing to Selenium! A PR well described will help maintainers to quickly review and merge it
Before submitting your PR, please check our contributing guidelines. Avoid large PRs, help reviewers by making them as simple and short as possible.
Description
Motivation and Context
Types of changes
Checklist
PR Type
Enhancement
Description
addListener
methods inBiDi
,Connection
, andLogInspector
classes to return a long ID, enabling better listener management.removeListener
method inBiDi
andConnection
classes to allow removal of listeners by their ID.Connection
to store listeners with their corresponding IDs.Changes walkthrough 📝
BiDi.java
Modify listener methods to return IDs and add removal method
java/src/org/openqa/selenium/bidi/BiDi.java
addListener
methods to return a long ID.removeListener
method to remove listeners by ID.Connection.java
Enhance listener management with ID-based tracking
java/src/org/openqa/selenium/bidi/Connection.java
addListener
method to return a long ID.removeListener
method to remove listeners by ID.LogInspector.java
Update LogInspector to use ID-based listener management
java/src/org/openqa/selenium/bidi/module/LogInspector.java