Mirror icon indicating copy to clipboard operation
Mirror copied to clipboard

MirrorTest base class client authority

Open DumDumin opened this issue 3 years ago • 3 comments

Please explain the suggested feature in detail. I am using the MirrorTest class as a base for some of my unit tests. It contains the method CreateNetworkedAndSpawn, which takes an optional parameter ownerConnection. The summary states, that the ownConnection should only be NetworkServer.localConnection and the same is asserted at the end of the method. I am unsure why it could not be a connection to a none hostclient. I removed the assert in my code and use it with a client connection to run my tests (only thing i have to do, is set isClient on the clientComponent). I try to test, that client code is only executed on the client and not on the server.

Could you please explain why this should not be done? If there is a valid reason you should also consider to replace the ownerConnection parameter with a bool, to indicate if you want to use the NetworkServer.localConnection or not as the owner.

    // create GameObject + NetworkIdentity + NetworkBehaviour & SPAWN
    // => ownerConnection can be NetworkServer.localConnection if needed.
    // => returns objects from client and from server.
    //    will be same in host mode.

    protected void CreateNetworkedAndSpawn<T>(
        out GameObject serverGO, out NetworkIdentity serverIdentity, out T serverComponent,
        out GameObject clientGO, out NetworkIdentity clientIdentity, out T clientComponent,
        NetworkConnection ownerConnection = null)
        where T : NetworkBehaviour {

        // server & client need to be active before spawning
        Debug.Assert(NetworkClient.active, "NetworkClient needs to be active before spawning.");
        Debug.Assert(NetworkServer.active, "NetworkServer needs to be active before spawning.");

        // create one on server, one on client
        // (spawning has to find it on client, it doesn't create it)
        CreateNetworked(out serverGO, out serverIdentity, out serverComponent);
        CreateNetworked(out clientGO, out clientIdentity, out clientComponent);

        // give both a scene id and register it on client for spawnables
        clientIdentity.sceneId = serverIdentity.sceneId = (ulong)serverGO.GetHashCode();
        NetworkClient.spawnableObjects[clientIdentity.sceneId] = clientIdentity;

        // spawn
        NetworkServer.Spawn(serverGO, ownerConnection);
        ProcessMessages();

        // double check that we have authority if we passed an owner connection
        if (ownerConnection != null)
            Debug.Assert(serverComponent.hasAuthority == true, $"Behaviour Had Wrong Authority when spawned, This means that the test is broken and will give the wrong results");

        // make sure the client really spawned it.
        Assert.That(NetworkClient.spawned.ContainsKey(serverIdentity.netId));
    }

DumDumin avatar Feb 01 '22 13:02 DumDumin

where in the code does it require it to be the host connection? localConnection is only mentioned in the comment? which line did you remove?

miwarnec avatar Feb 14 '22 04:02 miwarnec

Thanks for reaching out on this topic!

The following snippet requires, that the serverComponent has the authority, if an ownerConnection is passed to the method.

// double check that we have authority if we passed an owner connection
if (ownerConnection != null)
    Debug.Assert(serverComponent.hasAuthority == true, $"Behaviour Had Wrong Authority when spawned, This means that the test is broken and will give the wrong results");

This fails, if I pass a NetworkConnectionToClient instance for a none host client, created as shown in the following snippet. It only works, if I use the NetworkServer.localConnection instead.

NetworkServer.Listen(1);
ConnectClientBlockingAuthenticatedAndReady(out NetworkConnectionToClient clientConnection);

To get my test working I removed the check shown in the first snippet, because the clientComponent has the authority in case of passing a none host client connection.

DumDumin avatar Feb 14 '22 11:02 DumDumin

hey @DumDumin , checking your issue again. you mentioned:

and the same is asserted at the end of the method

so what is the issue exactly?

  • is it that you can't use it without NetworkServer.localConnection? it should be usable by the connection that is returned from ConnectClientBlockingAuthenticatedAndReady(out NetworkConnectionToClient clientConnection). you should find some examples in the code for that too.
  • or is it that you try to spawn an object with non default authority?

miwarnec avatar Mar 19 '22 12:03 miwarnec

Closing for lack of response...reopen if questions are answered / new information becomes available.

MrGadget1024 avatar Nov 24 '22 12:11 MrGadget1024