Skip to content

Callback attributes not enforced for NetworkBehaviours created through test scripts #3227

@ghost

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.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions