using System.Collections.Generic ;
using System.Linq ;
using Test.Tools ;
using System.Threading.Tasks ;
using Unity.Services.Lobbies ;
using LobbyAPIInterface = LobbyRelaySample . lobby . LobbyAPIInterface ;
namespace Test
{
private LobbyRelaySample . Auth . SubIdentity_Authentication m_auth ;
private bool m_didSigninComplete = false ;
private Dictionary < string , PlayerDataObject > m_mockUserData ; // This is handled in the LobbyAsyncRequest calls normally, but we need to supply this for the direct Lobby API calls.
LobbyUser m_LocalUser ;
[OneTimeSetUp]
public void Setup ( )
{
m_LocalUser = new LobbyUser ( true ) ;
}
[UnityTearDown]
{ LobbyAPIInterface . DeleteLobbyAsync ( m_workingLobbyId , null ) ;
{ yield return AsyncTestHelper . Await ( async ( ) = > await LobbyAsyncRequests . Instance . LeaveLobbyAsync ( m_workingLobbyId ) ) ;
m_workingLobbyId = null ;
}
yield return new WaitForSeconds ( 0.5f ) ; // We need a yield anyway, so wait long enough to probably delete the lobby. There currently (6/22/2021) aren't other tests that would have issues if this took longer.
yield return
new WaitForSeconds (
1 ) ; // To prevent a possible 429 with the upcoming Query request, in case a previous test had one; Query requests can only occur at a rate of 1 per second.
float timeout = 5 ;
Debug . Log ( "Getting Lobby List 1" ) ;
QueryResponse queryResponse = null ;
yield return AsyncTestHelper . Await ( async ( ) = > queryResponse = await LobbyAsyncRequests . Instance . RetrieveLobbyListAsync ( ) ) ;
Assert . Greater ( timeout , 0 , "Timeout check (query #0)" ) ;
timeout = 5 ;
LobbyAPIInterface . CreateLobbyAsync ( m_auth . GetContent ( "id" ) , lobbyName , 1 0 0 , false , m_mockUserData ,
( r ) = > { createResponse = r ; } ) ;
while ( createResponse = = null & & timeout > 0 )
{
yield return new WaitForSeconds ( 0.25f ) ;
timeout - = 0.25f ;
}
yield return AsyncTestHelper . Await ( async ( ) = >
createResponse = await LobbyAsyncRequests . Instance . CreateLobbyAsync (
lobbyName ,
1 0 0 ,
false ,
m_LocalUser ) ) ;
Assert . Greater ( timeout , 0 , "Timeout check (create)" ) ;
Assert . IsNotNull ( createResponse , "CreateLobbyAsync should return a non-null result." ) ;
m_workingLobbyId = createResponse . Id ;
Assert . AreEqual ( lobbyName , createResponse . Name , "Created lobby should match the provided name." ) ;
yield return AsyncTestHelper . Await ( async ( ) = > queryResponse = await LobbyAsyncRequests . Instance . RetrieveLobbyListAsync ( ) ) ;
Debug . Log ( "Got Lobby List 2" ) ;
Assert . Greater ( timeout , 0 , "Timeout check (query #1)" ) ;
Assert . IsNotNull ( queryResponse , "QueryAllLobbiesAsync should return a non-null result. (#1)" ) ;
Assert . AreEqual ( 1 + numLobbiesIni , queryResponse . Results . Count , "Queried lobbies list should contain the test lobby." ) ;
Assert . IsTrue ( queryResponse . Results . Where ( r = > r . Name = = lobbyName ) . Count ( ) = = 1 , "Checking queried lobby for name." ) ;
timeout = 5 ;
yield return AsyncTestHelper . Await ( async ( ) = > lobby = await LobbyService . Instance . GetLobbyAsync ( createResponse . Id ) ) ;
yield return AsyncTestHelper . Await ( async ( ) = > lobby = await LobbyAsyncRequests . Instance . GetLobbyAsync ( createResponse . Id ) ) ;
Assert . Greater ( timeout , 0 , "Timeout check (get)" ) ;
bool didDeleteFinish = false ;
timeout = 5 ;
LobbyAPIInterface . DeleteLobbyAsync ( m_workingLobbyId , ( ) = > { didDeleteFinish = true ; } ) ;
while ( timeout > 0 & & ! didDeleteFinish )
{ yield return new WaitForSeconds ( 0.25f ) ;
timeout - = 0.25f ;
}
Assert . Greater ( timeout , 0 , "Timeout check (delete)" ) ;
yield return AsyncTestHelper . Await ( async ( ) = > await LobbyAsyncRequests . Instance . LeaveLobbyAsync ( m_workingLobbyId ) ) ;
timeout = 5 ;
Debug . Log ( "Getting Lobby List 3" ) ;
yield return AsyncTestHelper . Await ( async ( ) = > queryResponse = await LobbyAsyncRequests . Instance . RetrieveLobbyListAsync ( ) ) ;
Assert . Greater ( timeout , 0 , "Timeout check (query #2)" ) ;
Assert . IsNotNull ( queryResponse , "QueryAllLobbiesAsync should return a non-null result. (#2)" ) ;
Assert . AreEqual ( numLobbiesIni , queryResponse . Results . Count , "Queried lobbies list should be empty." ) ;
/// <summary>
/// If the Lobby create call fails, we want to ensure we call onComplete so we can act on the failure.
/// If the Lobby create call fails, we return null
public IEnumerator OnCompletesOnFailure ( )
public IEnumerator CreateFailsWithNull ( )
{
if ( ! m_didSigninComplete )
yield return new WaitForSeconds ( 3 ) ;
bool? didComplete = null ;
LobbyAPIInterface . CreateLobbyAsync ( "ThisStringIsInvalidHere" , "lobby name" , 1 2 3 , false , m_mockUserData , ( r ) = > { didComplete = ( r = = null ) ; } ) ;
float timeout = 5 ;
while ( didComplete = = null & & timeout > 0 )
{ yield return new WaitForSeconds ( 0.25f ) ;
timeout - = 0.25f ;
}
Lobby createLobby = null ;
yield return AsyncTestHelper . Await ( async ( ) = >
createLobby = await LobbyAsyncRequests . Instance . CreateLobbyAsync (
"lobby name" ,
1 2 3 ,
false ,
m_LocalUser ) ) ;
Assert . Greater ( timeout , 0 , "Timeout check" ) ;
Assert . NotNull ( didComplete , "Should have called onComplete, even if the async request failed." ) ;
Assert . True ( didComplete , "The returned object will be null, so expect to need to handle it." ) ;
Assert . Null ( createLobby , "The returned object will be null, so expect to need to handle it." ) ;
}
}
}