Skip to content

Add companion app to fully test sockets in unit tests#408

Merged
josesimoes merged 3 commits into
nanoframework:mainfrom
josesimoes:add-test-companion
Jul 10, 2026
Merged

Add companion app to fully test sockets in unit tests#408
josesimoes merged 3 commits into
nanoframework:mainfrom
josesimoes:add-test-companion

Conversation

@josesimoes

@josesimoes josesimoes commented Jul 10, 2026

Copy link
Copy Markdown
Member

Description

  • Add .NET 10 console app as companion app.
  • Add documentation for companion app.
  • Add socket round trip unit tests.

Motivation and Context

  • Need to close the loop in sockets testing. Currently there was no way to test operation that required actuall connection to a socket. This fullfils that need and opens up the path to add more unit test to increas coverage in sockets code.

How Has This Been Tested?

  • Running sockets round trip tests locally.
image

Screenshots

Types of changes

  • Improvement (non-breaking change that improves a feature, code or algorithm)
  • Bug fix (non-breaking change which fixes an issue with code or algorithm)
  • New feature (non-breaking change which adds functionality to code)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Config and build (change in the configuration and build system, has no impact on code or features)
  • Dependencies (update dependencies and changes associated, has no impact on code or features)
  • Unit Tests (add new Unit Test(s) or improved existing one(s), has no impact on code or features)
  • Documentation (changes or updates in the documentation, has no impact on code or features)

Checklist:

  • My code follows the code style of this project (only if there are changes in source code).
  • My changes require an update to the documentation (there are changes that require the docs website to be updated).
  • I have updated the documentation accordingly (the changes require an update on the docs in this repo).
  • I have read the CONTRIBUTING document.
  • I have tested everything locally and all new and existing tests passed (only if there are changes in source code).
  • I have added new tests to cover my changes.

- Add .NET 10 console app.
- Add documentation for companion app.
- Add socket round trip unit tests.

Assisted by Claude Sonnet 4.6
@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@josesimoes, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 1 minute

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: add7babc-8679-4eb7-8c9f-b3b0811489b1

📥 Commits

Reviewing files that changed from the base of the PR and between ab6ebaa and d4a09a6.

📒 Files selected for processing (5)
  • Tests/NetworkTestCompanion/FirewallHelper.cs
  • Tests/NetworkTestCompanion/Program.cs
  • Tests/README.md
  • Tests/SocketTests/SocketRoundTripTests.cs
  • Tests/SocketTests/SocketTests.nfproj
📝 Walkthrough

Walkthrough

Adds a host-side Network Test Companion with TCP/UDP echo services and firewall helpers, configures socket tests to connect to it, and introduces TCP/UDP round-trip coverage for device and emulator scenarios.

Changes

Network Test Companion

Layer / File(s) Summary
Companion runtime and firewall setup
Tests/NetworkTestCompanion/NetworkTestCompanion.csproj, Tests/NetworkTestCompanion/Program.cs, Tests/NetworkTestCompanion/FirewallHelper.cs
Adds the executable, command-line configuration, LAN IP discovery, shutdown handling, and platform-specific firewall setup/removal.
Control channel and echo services
Tests/NetworkTestCompanion/CommandServer.cs, Tests/NetworkTestCompanion/TcpEchoServer.cs, Tests/NetworkTestCompanion/UdpEchoServer.cs
Adds newline-delimited JSON control commands and cancellable TCP/UDP echo servers with lifecycle management.
Test configuration and companion client
.runsettings, Tests/SocketTests/SocketTests.nfproj, Tests/SocketTests/CompanionClient.cs, Tests/SocketTests/packages.config, Tests/SocketTests/packages.lock.json, .gitignore
Generates companion connection settings from run parameters, adds the threading reference, and provides a socket client for control commands.
Socket round-trip coverage and test guidance
Tests/SocketTests/SocketRoundTripTests.cs, Tests/SocketTests/SocketPair.cs, Tests/SocketTests/SocketTools.cs, Tests/README.md, nanoFramework.System.Net.sln
Adds TCP, UDP, and MCU-server round-trip tests, preserves existing helper behavior, and documents companion setup and execution.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant SocketRoundTripTests
  participant CompanionClient
  participant CommandServer
  participant TcpEchoServer
  SocketRoundTripTests->>CompanionClient: StartTcpEcho(port)
  CompanionClient->>CommandServer: Send JSON command
  CommandServer->>TcpEchoServer: Start echo listener
  CommandServer-->>CompanionClient: Return JSON status
  SocketRoundTripTests->>TcpEchoServer: Send test payload
  TcpEchoServer-->>SocketRoundTripTests: Echo payload
Loading

Suggested labels: Type: dependencies, Area: Config-and-Build

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Title check ✅ Passed The title clearly summarizes the companion app and socket test additions.
Description check ✅ Passed The description directly matches the companion app, docs, and round-trip socket tests in the changeset.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nfbot

nfbot commented Jul 10, 2026

Copy link
Copy Markdown
Member

⚠️ Native declaration update required!

The checksum and/or native version of nanoFramework.System.Net have changed. The native declaration in nf-interpreter must be updated before this package can be released.

Previously published This build
Native version 100.2.0.12 100.2.0.14
Checksum 0x6DFA71D6 0x8EFB9944

👉 Tracking issue: https://github.com/nanoframework/Home/issues/1816

The issue contains a link to the stubs artifact from this build and instructions for GitHub Copilot to perform the update.

@josesimoes

Copy link
Copy Markdown
Member Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 10, 2026

Copy link
Copy Markdown
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 10

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@Tests/NetworkTestCompanion/FirewallHelper.cs`:
- Around line 80-81: Update RemoveWindows to accept the TCP and UDP port lists
used by SetupWindows, then delete each rule using the complete
nF-TestCompanion-TCP-{port} and nF-TestCompanion-UDP-{port} names instead of
only RulePrefix; keep the success message only after all deletions complete.
- Around line 115-142: IsUfwActive() can incorrectly return false when ufw
status requires elevated permissions, suppressing necessary Linux port-opening
guidance. Update the Linux guidance logic around the caller of IsUfwActive() to
print the ufw instructions unconditionally, or replace the check with a
permission-safe detection method; avoid using IsUfwActive() as a gate for
displaying those commands.

In `@Tests/README.md`:
- Around line 84-88: Update the firewall examples in Tests/README.md to use the
actual round-trip test ports 7001–7004 instead of port 7, including both the ufw
and iptables/nftables sections; alternatively, clearly label port 7 as an
example and identify the test command’s ports as the required values.
- Line 13: The Tests README table incorrectly labels round-trip tests as future
work. Update the SocketTests row to state that round-trip tests are present, and
mention their companion requirement as described, keeping it consistent with
SocketRoundTripTests.

In `@Tests/SocketTests/CompanionClient.cs`:
- Around line 26-29: The companion socket initialization in the CompanionClient
constructor should configure a short ReceiveTimeout before connecting, so
stalled reads fail promptly instead of waiting for the session timeout.
Optionally set SendTimeout as well to guard writes, using the existing _socket
setup.
- Around line 75-78: The response-reading logic in the companion client assumes
one Socket.Receive call returns the complete message and uses an unnecessary
fixed sleep. Update the code around the Receive call to repeatedly read into the
buffer, accumulate bytes, and stop when a newline is received or the socket
reaches EOF; remove the Thread.Sleep dependency while preserving correct JSON
response handling.

In `@Tests/SocketTests/SocketRoundTripTests.cs`:
- Around line 201-212: Update GetLocalIp() to skip loopback addresses,
disconnected interfaces, and virtual adapters before selecting an IPv4 address;
retain the existing fallback to "0.0.0.0" when no suitable interface is found.
- Around line 69-84: Ensure socket resources are cleaned up on both success and
failure paths: wrap each echo test’s `companion.Stop(echoPort)` in a finally
block surrounding the client/send/receive assertions, and update the
server/accepted-socket test to use using declarations or try/finally so both are
disposed even when assertions or setup throw. Apply this to the test methods
covering the referenced round-trip scenarios and the server/accepted socket
flow.
- Around line 146-156: Configure a finite ReceiveTimeout on the sockets used by
the round-trip tests before any blocking reads. Set it on the TCP client sockets
before their receive loops and on the UDP client in the test using ReceiveFrom,
such as the socket created in the UDP setup, so lost echoes or stalled peers
raise SocketException instead of blocking indefinitely.

In `@Tests/SocketTests/SocketTests.nfproj`:
- Around line 65-111: Update the comment above GenerateTestConfiguration to
accurately state that TestConfiguration.cs is generated during the build and is
not committed to source, rather than claiming it is committed. Clarify that a
fresh checkout receives the file when the target runs.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository: nanoframework/coderabbit/.coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 8e93dc5a-3547-4a32-8686-4bd2f10acffd

📥 Commits

Reviewing files that changed from the base of the PR and between 923b423 and ab6ebaa.

📒 Files selected for processing (17)
  • .gitignore
  • .runsettings
  • Tests/NetworkTestCompanion/CommandServer.cs
  • Tests/NetworkTestCompanion/FirewallHelper.cs
  • Tests/NetworkTestCompanion/NetworkTestCompanion.csproj
  • Tests/NetworkTestCompanion/Program.cs
  • Tests/NetworkTestCompanion/TcpEchoServer.cs
  • Tests/NetworkTestCompanion/UdpEchoServer.cs
  • Tests/README.md
  • Tests/SocketTests/CompanionClient.cs
  • Tests/SocketTests/SocketPair.cs
  • Tests/SocketTests/SocketRoundTripTests.cs
  • Tests/SocketTests/SocketTests.nfproj
  • Tests/SocketTests/SocketTools.cs
  • Tests/SocketTests/packages.config
  • Tests/SocketTests/packages.lock.json
  • nanoFramework.System.Net.sln

Comment thread Tests/NetworkTestCompanion/FirewallHelper.cs Outdated
Comment thread Tests/NetworkTestCompanion/FirewallHelper.cs Outdated
Comment thread Tests/README.md Outdated
Comment thread Tests/README.md
Comment thread Tests/SocketTests/CompanionClient.cs
Comment thread Tests/SocketTests/CompanionClient.cs
Comment thread Tests/SocketTests/SocketRoundTripTests.cs Outdated
Comment thread Tests/SocketTests/SocketRoundTripTests.cs Outdated
Comment thread Tests/SocketTests/SocketRoundTripTests.cs
Comment thread Tests/SocketTests/SocketTests.nfproj
@josesimoes
josesimoes merged commit 25d2755 into nanoframework:main Jul 10, 2026
8 checks passed
@josesimoes
josesimoes deleted the add-test-companion branch July 10, 2026 15:38
josesimoes added a commit that referenced this pull request Jul 13, 2026
@coderabbitai coderabbitai Bot mentioned this pull request Jul 17, 2026
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants