MirrorNetworking / MirrorNetworking/Mirror

Callback attributes not enforced for NetworkBehaviours created through test scripts

Open
#3,227 2 comments 0 reactions 1 assignee View on GitHub

@miwarnec is already working on this.

Since Nov 24, 2022.

Dominant language
C#
Stars
6.3k
Forks
870
PR merge metrics
No merged PRs in 30d

Description

Bug Description
[ServerCallback] and [ClientCallback] stop methods on NetworkBehaviours from being called on the wrong end of the network. However, these attributes have false negatives when called during unit tests. This makes it hard to test code that relies on these attributes, because the test environment and the build environment are different.

Minimum Reproduction
Test

using Mirror;
using Mirror.Tests;
using NUnit.Framework;

public class TestAttributes : MirrorEditModeTest
{
    NetworkConnectionToClient connectionToClient;
    TestPlayer playerServer;
    TestPlayer playerClient;

    [SetUp]
    public override void SetUp()
    {
        base.SetUp();

        // start server/client
        NetworkServer.Listen(1);
        ConnectClientBlockingAuthenticatedAndReady(out connectionToClient);

        CreateNetworkedAndSpawn(out _, out _, out playerServer,
        out _, out _, out playerClient,
        connectionToClient);
    }

    [Test]
    public void TestServerCallbackAttributes()
    {
        playerClient.CMDFoo();
        ProcessMessages();
    }

    [Test]
    public void TestClientCallbackAttributes()
    {
        playerClient.Foo();
    }
}

Buttons

using UnityEngine;

public class UIManager : MonoBehaviour
{
    public TestPlayer player;

    public void CallFoo()
    {
        player.Foo();
    }

    public void CallCommandFoo()
    {
        player.CMDFoo();
    }
}

Player

using Mirror;
using UnityEngine;

public class TestPlayer : NetworkBehaviour
{
    public override void OnStartLocalPlayer()
    {
        base.OnStartLocalPlayer();
        FindObjectOfType<UIManager>().player = this;
    }

    public void Foo()
    {
        ServerFoo();
        ClientFoo();
    }

    [Command]
    public void CMDFoo()
    {
        ServerFoo();
        ClientFoo();
    }

    [ServerCallback]
    private void ServerFoo()
    {
        Debug.LogError("Server Called");
    }

    [ClientCallback]
    private void ClientFoo()
    {
        Debug.LogError("Client Called");
    }
}

Build (editor as server)
image
Build (editor as client)
image
All buttons were pressed for these examples, but only one ever logged anything (as expected)

Test (TestServerCallbackAttributes)
image
Test (TestClientCallbackAttributes)
image

Expected behavior
I would expect that the client is only called on Foo for both test and build
I would expect that the server is only called on CMDFoo for both test and build

Desktop:

  • OS: Windows 10
  • Build target: NA
  • Unity version: 2021.3.11f1 (Current LTS)
  • Mirror branch: Master

Additional Notes
Obviously this is not a problem when using host mode, as one would expect to have both be called.
Here is a link to a minimum reproduction project. Hope not having to deal with Assembly definitions helps.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.