2018-08-28 17:32:59 -04:00
|
|
|
|
using IW4MAdmin.Application;
|
|
|
|
|
using SharedLibraryCore;
|
2018-11-05 22:01:29 -05:00
|
|
|
|
using SharedLibraryCore.Database.Models;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using Xunit;
|
|
|
|
|
|
|
|
|
|
namespace Tests
|
|
|
|
|
{
|
|
|
|
|
[Collection("ManagerCollection")]
|
|
|
|
|
public class ManagerTests
|
|
|
|
|
{
|
|
|
|
|
readonly ApplicationManager Manager;
|
|
|
|
|
|
|
|
|
|
public ManagerTests(ManagerFixture fixture)
|
|
|
|
|
{
|
|
|
|
|
Manager = fixture.Manager;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AreCommandNamesUnique()
|
|
|
|
|
{
|
|
|
|
|
bool test = Manager.GetCommands().Count == Manager.GetCommands().Select(c => c.Name).Distinct().Count();
|
|
|
|
|
Assert.True(test, "command names are not unique");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AreCommandAliasesUnique()
|
|
|
|
|
{
|
2019-04-05 22:15:17 -04:00
|
|
|
|
var mgr = Program.ServerManager;
|
2018-08-28 17:32:59 -04:00
|
|
|
|
bool test = mgr.GetCommands().Count == mgr.GetCommands().Select(c => c.Alias).Distinct().Count();
|
|
|
|
|
|
|
|
|
|
Assert.True(test, "command aliases are not unique");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddAndRemoveClientsViaJoinShouldSucceed()
|
|
|
|
|
{
|
|
|
|
|
var server = Manager.GetServers().First();
|
2018-09-07 23:29:42 -04:00
|
|
|
|
var waiters = new Queue<GameEvent>();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
int clientStartIndex = 4;
|
|
|
|
|
int clientNum = 10;
|
|
|
|
|
|
|
|
|
|
for (int i = clientStartIndex; i < clientStartIndex + clientNum; i++)
|
|
|
|
|
{
|
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Join,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Origin = new EFClient()
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
Name = $"Player{i}",
|
|
|
|
|
NetworkId = i,
|
|
|
|
|
ClientNumber = i - 1
|
|
|
|
|
},
|
|
|
|
|
Owner = server
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server.Manager.GetEventHandler().AddEvent(e);
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Enqueue(e);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (waiters.Count > 0)
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Dequeue().OnProcessed.Wait();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(server.ClientNum == clientNum, $"client num does not match added client num [{server.ClientNum}:{clientNum}]");
|
|
|
|
|
|
|
|
|
|
for (int i = clientStartIndex; i < clientStartIndex + clientNum; i++)
|
|
|
|
|
{
|
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Disconnect,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Origin = new EFClient()
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
Name = $"Player{i}",
|
|
|
|
|
NetworkId = i,
|
|
|
|
|
ClientNumber = i - 1
|
|
|
|
|
},
|
|
|
|
|
Owner = server
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
server.Manager.GetEventHandler().AddEvent(e);
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Enqueue(e);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (waiters.Count > 0)
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Dequeue().OnProcessed.Wait();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(server.ClientNum == 0, "there are still clients connected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddAndRemoveClientsViaRconShouldSucceed()
|
|
|
|
|
{
|
|
|
|
|
var server = Manager.GetServers().First();
|
2018-09-07 23:29:42 -04:00
|
|
|
|
var waiters = new Queue<GameEvent>();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
|
|
|
|
|
int clientIndexStart = 1;
|
|
|
|
|
int clientNum = 8;
|
|
|
|
|
|
|
|
|
|
for (int i = clientIndexStart; i < clientNum + clientIndexStart; i++)
|
|
|
|
|
{
|
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Connect,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Origin = new EFClient()
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
Name = $"Player{i}",
|
|
|
|
|
NetworkId = i,
|
|
|
|
|
ClientNumber = i - 1,
|
|
|
|
|
IPAddress = i,
|
|
|
|
|
Ping = 50,
|
|
|
|
|
CurrentServer = server
|
|
|
|
|
},
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Manager.GetEventHandler().AddEvent(e);
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Enqueue(e);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (waiters.Count > 0)
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Dequeue().OnProcessed.Wait();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-11-05 22:01:29 -05:00
|
|
|
|
int actualClientNum = server.GetClientsAsList().Count(p => p.State == EFClient.ClientState.Connected);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
Assert.True(actualClientNum == clientNum, $"client connected states don't match [{actualClientNum}:{clientNum}");
|
|
|
|
|
|
|
|
|
|
for (int i = clientIndexStart; i < clientNum + clientIndexStart; i++)
|
|
|
|
|
{
|
|
|
|
|
var e = new GameEvent()
|
|
|
|
|
{
|
|
|
|
|
Type = GameEvent.EventType.Disconnect,
|
2018-11-05 22:01:29 -05:00
|
|
|
|
Origin = new EFClient()
|
2018-08-28 17:32:59 -04:00
|
|
|
|
{
|
|
|
|
|
Name = $"Player{i}",
|
|
|
|
|
NetworkId = i,
|
|
|
|
|
ClientNumber = i - 1,
|
|
|
|
|
IPAddress = i,
|
|
|
|
|
Ping = 50,
|
|
|
|
|
CurrentServer = server
|
|
|
|
|
},
|
|
|
|
|
Owner = server,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Manager.GetEventHandler().AddEvent(e);
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Enqueue(e);
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (waiters.Count > 0)
|
|
|
|
|
{
|
2018-09-07 23:29:42 -04:00
|
|
|
|
waiters.Dequeue().OnProcessed.Wait();
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
actualClientNum = server.ClientNum;
|
|
|
|
|
Assert.True(actualClientNum == 0, "there are clients still connected");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void AddClientViaLog()
|
|
|
|
|
{
|
|
|
|
|
var resetEvent = new ManualResetEventSlim();
|
|
|
|
|
resetEvent.Reset();
|
|
|
|
|
|
|
|
|
|
Manager.OnServerEvent += (sender, eventArgs) =>
|
|
|
|
|
{
|
|
|
|
|
if (eventArgs.Event.Type == GameEvent.EventType.Join)
|
|
|
|
|
{
|
|
|
|
|
eventArgs.Event.OnProcessed.Wait();
|
|
|
|
|
Assert.True(false);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
File.AppendAllText("test_mp.log", " 2:33 J;224b3d0bc64ab4f9;0;goober");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
resetEvent.Wait(5000);
|
|
|
|
|
}
|
2018-10-12 22:28:22 -04:00
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void PrintCommands()
|
|
|
|
|
{
|
|
|
|
|
var sb = new StringBuilder();
|
|
|
|
|
|
|
|
|
|
sb.AppendLine("|Name |Alias|Description |Requires Target|Syntax |Required Level|");
|
|
|
|
|
sb.AppendLine("|--------------| -----| --------------------------------------------------------| -----------------| -------------| ----------------|");
|
|
|
|
|
|
|
|
|
|
foreach (var command in Manager.GetCommands().OrderByDescending(c => c.Permission).ThenBy(c => c.Name))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine($"|{command.Name}|{command.Alias}|{command.Description}|{command.RequiresTarget}|{command.Syntax.Substring(8).EscapeMarkdown()}|{command.Permission}|");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Assert.True(false, sb.ToString());
|
|
|
|
|
}
|
2018-08-28 17:32:59 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|