Skip to content

[Communication] PR9: Added Mailbox tests#221

Open
Joseph0120 wants to merge 1 commit intojoseph/communication_delay_PR8from
joseph/communication_delay_PR9
Open

[Communication] PR9: Added Mailbox tests#221
Joseph0120 wants to merge 1 commit intojoseph/communication_delay_PR8from
joseph/communication_delay_PR9

Conversation

@Joseph0120
Copy link
Copy Markdown
Collaborator

Files Added:
MailboxTest.cs

Added simple test cases for Mailbox.cs.

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 1, 2026

Warning

Rate limit exceeded

@Joseph0120 has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 23 minutes and 6 seconds before requesting another review.

Your organization is not enrolled in usage-based pricing. Contact your admin to enable usage-based pricing to continue reviews beyond the rate limit, or try again in 23 minutes and 6 seconds.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: af3c15c2-9570-4ed8-be47-e288c4313e9d

📥 Commits

Reviewing files that changed from the base of the PR and between dbe07cf and e16e944.

📒 Files selected for processing (1)
  • Assets/Tests/EditMode/MailboxTests.cs
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch joseph/communication_delay_PR9

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 and usage tips.

@stacklane-pr-stack-visualizer
Copy link
Copy Markdown

stacklane-pr-stack-visualizer Bot commented Apr 1, 2026

🧱 Stack PR · Branch end (9 PRs in stack)

Stack Structure:

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request adds a comprehensive test suite for the Mailbox system, covering message delivery, latency scheduling, and receiver lifecycle management. Feedback suggests optimizing the tests by removing redundant initialization calls and cleanup logic, replacing brittle reflection with internal access, refactoring duplicated setup code, and ensuring test isolation by avoiding modifications to the global random state.

Comment on lines +262 to +263
Mailbox mailbox = mailboxObject.AddComponent<Mailbox>();
InvokePrivateMethod(mailbox, "Awake");
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

high

In Unity, AddComponent<T>() automatically calls the Awake method of the component if the GameObject is active. Manually invoking Awake via reflection here results in it being called a second time, which can lead to unintended side effects like double-initialization or multiple event subscriptions.

    Mailbox mailbox = mailboxObject.AddComponent<Mailbox>();

Comment on lines +12 to +22
foreach (GameObject gameObject in _createdObjects) {
if (gameObject != null) {
Object.DestroyImmediate(gameObject);
}
}

foreach (Mailbox mailbox in Object.FindObjectsByType<Mailbox>(FindObjectsSortMode.None)) {
if (mailbox != null) {
Object.DestroyImmediate(mailbox.gameObject);
}
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The second loop in TearDown is redundant because all Mailbox objects created via the CreateMailbox helper are already tracked in the _createdObjects list and destroyed in the first loop. Additionally, it is good practice to call _createdObjects.Clear() after destruction to ensure the list is empty for subsequent tests if the test class instance is reused.

    foreach (GameObject gameObject in _createdObjects) {
      if (gameObject != null) {
        Object.DestroyImmediate(gameObject);
      }
    }
    _createdObjects.Clear();

Comment on lines +204 to +205
SetPrivateField(mailbox, "_latencyJitterStdSeconds", -1f);
SetPrivateField(mailbox, "_uniformLatency", -2f);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Using hardcoded strings for private field names in reflection is brittle. If these fields are renamed in the Mailbox class, the tests will fail at runtime without compile-time errors. Consider making these fields internal and using [InternalsVisibleTo] to allow for safer, type-checked access in tests.

Comment on lines +284 to +305
private TestMailboxAgent CreateMailboxAgent(string name) {
GameObject agentObject = new(name);
_createdObjects.Add(agentObject);
agentObject.AddComponent<Rigidbody>();
TestMailboxAgent agent = agentObject.AddComponent<TestMailboxAgent>();
InvokePrivateMethod(agent, "Awake");
return agent;
}

private TestMailboxInterceptor CreateMailboxInterceptor(string name) {
GameObject interceptorObject = new(name);
_createdObjects.Add(interceptorObject);
interceptorObject.AddComponent<Rigidbody>();
return interceptorObject.AddComponent<TestMailboxInterceptor>();
}

private TestMailboxCarrier CreateMailboxCarrier(string name) {
GameObject carrierObject = new(name);
_createdObjects.Add(carrierObject);
carrierObject.AddComponent<Rigidbody>();
return carrierObject.AddComponent<TestMailboxCarrier>();
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The creation logic for different agent types is duplicated. Refactoring this into a generic helper method improves maintainability. This also removes the redundant manual Awake call in CreateMailboxAgent, consistent with the behavior of AddComponent on active GameObjects.

  private T CreateTestAgent<T>(string name) where T : Component {
    GameObject agentObject = new(name);
    _createdObjects.Add(agentObject);
    agentObject.AddComponent<Rigidbody>();
    return agentObject.AddComponent<T>();
  }

  private TestMailboxAgent CreateMailboxAgent(string name) => CreateTestAgent<TestMailboxAgent>(name);
  private TestMailboxInterceptor CreateMailboxInterceptor(string name) => CreateTestAgent<TestMailboxInterceptor>(name);
  private TestMailboxCarrier CreateMailboxCarrier(string name) => CreateTestAgent<TestMailboxCarrier>(name);

}

private static float ComputeExpectedLatency(float baseLatency, float jitterStdSeconds, int randomSeed) {
Random.InitState(randomSeed);
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

Calling UnityEngine.Random.InitState modifies the global random state of the application. Doing this inside a helper method used for assertions can cause side effects in other tests. If possible, consider using a local System.Random instance for calculating expected values to ensure test isolation, provided the production code's logic can be matched.

@Joseph0120 Joseph0120 force-pushed the joseph/communication_delay_PR8 branch from 0fe4232 to dbe07cf Compare April 1, 2026 06:09
@Joseph0120 Joseph0120 force-pushed the joseph/communication_delay_PR9 branch from 4dd5d2e to e16e944 Compare April 1, 2026 06:11
@Joseph0120 Joseph0120 requested a review from tryuan99 April 1, 2026 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant