Successful unit test setup and tear down

This commit is contained in:
watsonb8 2020-01-20 22:53:33 -05:00
parent 28afcf12e4
commit df2a6b4bfc
8 changed files with 159 additions and 41 deletions

View File

@ -0,0 +1,60 @@
using NUnit.Framework;
using Aurora.Proto.PartyV2;
using Aurora.Services.Server;
using Grpc.Core;
using System.Threading.Tasks;
namespace Aurora.test.ControllerTests
{
public class MemberControllerTests
{
private RemotePartyService.RemotePartyServiceClient _remotePartyService;
private Channel _channel;
[SetUp]
public void Setup()
{
ServerService.Instance.Start("testParty", "asdf");
_channel = new Channel(string.Format("{0}:{1}", ServerService.GetLocalIPAddress(), 8080), ChannelCredentials.Insecure);
_remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel);
}
[TearDown]
public async Task TearDown()
{
await ServerService.Instance.Stop();
await _channel.ShutdownAsync();
}
[Test]
public void DefaultTest()
{
ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest()
{
Parent = "party1",
PageSize = 10,
});
Assert.NotNull(resp);
Assert.GreaterOrEqual(resp.Members.Count, 1);
}
[Test]
[TestCase("Alex")]
[TestCase("Alex Goldberg")]
[TestCase("Alex/goldberg")]
[TestCase("alex@welcome.com")]
public void CreateMemberTest(string value)
{
Member member = _remotePartyService.CreateMember(new CreateMemberRequest()
{
Parent = "party1",
Member = new Member()
{
UserName = value,
IpAddress = ServerService.GetLocalIPAddress(),
}
});
Assert.NotNull(member);
}
}
}

View File

@ -1,11 +1,12 @@
using System.Threading.Tasks;
using NUnit.Framework;
using Aurora.Proto.PartyV2;
using Aurora.Services.Server;
using Grpc.Core;
namespace Aurora.test
namespace Aurora.test.ControllerTests
{
public class Tests
public class PartyControllerTests
{
private RemotePartyService.RemotePartyServiceClient _remotePartyService;
private Channel _channel;
@ -17,15 +18,20 @@ namespace Aurora.test
_remotePartyService = new RemotePartyService.RemotePartyServiceClient(_channel);
}
[Test]
public void Test2()
[TearDown]
public async Task TearDown()
{
ListMembersResponse resp = _remotePartyService.ListMembers(new ListMembersRequest()
{
Parent = "party1",
PageSize = 10,
});
Assert.NotNull(resp);
await ServerService.Instance.Stop();
await _channel.ShutdownAsync();
}
[Test]
public void DefaultTest()
{
Party party = _remotePartyService.GetParty(new Proto.General.Empty());
Assert.NotNull(party);
Assert.AreEqual(party.Name, "party/party1");
}
}
}

View File

@ -0,0 +1,39 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Grpc.Core;
namespace Aurora.test.Models
{
public class CallContext : ServerCallContext
{
protected override string MethodCore => throw new NotImplementedException();
protected override string HostCore => throw new NotImplementedException();
protected override string PeerCore => throw new NotImplementedException();
protected override DateTime DeadlineCore => throw new NotImplementedException();
protected override Metadata RequestHeadersCore => throw new NotImplementedException();
protected override CancellationToken CancellationTokenCore => throw new NotImplementedException();
protected override Metadata ResponseTrailersCore => throw new NotImplementedException();
protected override Status StatusCore { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
protected override WriteOptions WriteOptionsCore { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
protected override AuthContext AuthContextCore => throw new NotImplementedException();
protected override ContextPropagationToken CreatePropagationTokenCore(ContextPropagationOptions options)
{
throw new NotImplementedException();
}
protected override Task WriteResponseHeadersAsyncCore(Metadata responseHeaders)
{
throw new NotImplementedException();
}
}
}

View File

@ -94,10 +94,11 @@ message LeavePartyResponse {
}
message Member {
//Resource name of the party member to be returned
//Resource name of the party member to be returned (Added by server)
string name = 1;
string userName = 2;
string ipAddress = 3;
//Added by server
google.protobuf.Timestamp addedOn = 4;
}
@ -112,7 +113,6 @@ message ListMembersResponse {
repeated Member members = 1;
string nextPageToken = 2;
}
message GetMemberRequest {
//Resource name of the member to be returned
string name = 1;

View File

@ -1,11 +1,11 @@
using System;
using System.Threading.Tasks;
using System.Collections.Generic;
using System.Collections;
using Aurora.Proto.PartyV2;
using Aurora.Proto.General;
using Aurora.Utils;
using Grpc.Core;
using Google.Protobuf.WellKnownTypes;
namespace Aurora.Services.Server.Controllers
{
@ -62,8 +62,8 @@ namespace Aurora.Services.Server.Controllers
public override Task<Member> CreateMember(CreateMemberRequest request, Grpc.Core.ServerCallContext context)
{
//Generate Guid
string memberNameGuid = HashUtil.GetHashGuid(new string[] { context.Peer, request.Member.UserName }).ToString();
string resourceName = string.Format("{0}/members/{1}", request.Parent, memberNameGuid);
string resourceName = GetNewMemberResourceName(request.Parent, context.Peer, request.Member.UserName);
//Check if already added
if (_memberList.ContainsKey(resourceName))
{
@ -71,6 +71,8 @@ namespace Aurora.Services.Server.Controllers
}
request.Member.Name = resourceName;
request.Member.AddedOn = Timestamp.FromDateTime(DateTime.UtcNow);
request.Member.IpAddress = context.Host;
_memberList.Add(resourceName, request.Member);
@ -89,7 +91,7 @@ namespace Aurora.Services.Server.Controllers
return Task.FromResult(request.Member);
}
public override Task<Empty> DeleteMember(DeleteMemberRequest request, Grpc.Core.ServerCallContext context)
public override Task<Aurora.Proto.General.Empty> DeleteMember(DeleteMemberRequest request, Grpc.Core.ServerCallContext context)
{
string memberResourceName = request.Name;
//Check if member exists
@ -113,7 +115,13 @@ namespace Aurora.Services.Server.Controllers
_eventManager.RemoveAllSubscriptions(memberResourceName);
_eventManager.CancelEventStream(memberResourceName);
return Task.FromResult(new Empty());
return Task.FromResult(new Aurora.Proto.General.Empty());
}
private string GetNewMemberResourceName(string parent, string contextPeer, string userName)
{
string memberNameGuid = HashUtil.GetHash(new string[] { contextPeer, userName }).ToString();
return string.Format("{0}/members/{1}", parent, memberNameGuid);
}
}
}

View File

@ -9,6 +9,7 @@ namespace Aurora.Services.Server.Controllers
{
public partial class RemotePartyController : RemotePartyService.RemotePartyServiceBase
{
private string _partyResourceName = "party/party1";
private string _displayName;
private string _description;
private Member _hostMember;
@ -21,7 +22,7 @@ namespace Aurora.Services.Server.Controllers
/// </summary>
public RemotePartyController(string partyName, string description)
{
this._startDateTime = DateTime.Now;
this._startDateTime = DateTime.UtcNow;
this._displayName = partyName;
this._description = description;
this._memberList = new SortedList<string, Member>();
@ -32,7 +33,7 @@ namespace Aurora.Services.Server.Controllers
this._hostMember = new Member()
{
Name = userName,
Name = GetNewMemberResourceName(_partyResourceName, ServerService.GetLocalIPAddress(), userName),
UserName = userName,
IpAddress = ServerService.GetLocalIPAddress(),
};
@ -44,7 +45,7 @@ namespace Aurora.Services.Server.Controllers
{
Party party = new Party()
{
Name = "party/party1",
Name = _partyResourceName,
DisplayName = this._displayName,
Description = this._description,
HostIp = ServerService.GetLocalIPAddress(),

View File

@ -31,11 +31,6 @@ namespace Aurora.Services.Server
}
_hostname = host;
_server = new Grpc.Core.Server
{
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
};
}
public int Port
@ -68,15 +63,18 @@ namespace Aurora.Services.Server
Console.WriteLine(string.Format("Starting gRPC server at hostname: {0}, port: {1}", _hostname, _port));
if (!Initialized)
_server = new Grpc.Core.Server
{
//Construct implementations
_remotePartyController = new RemotePartyController(partyName, description);
Ports = { new ServerPort(_hostname, _port, ServerCredentials.Insecure) }
};
//Construct implementations
_remotePartyController = new RemotePartyController(partyName, description);
// Register grpc RemoteService with singleton server service
RegisterService(RemotePartyService.BindService(_remotePartyController));
// Register grpc RemoteService with singleton server service
RegisterService(RemotePartyService.BindService(_remotePartyController));
}
_server.Start();
}
catch (Exception ex)
@ -91,7 +89,15 @@ namespace Aurora.Services.Server
/// <returns>Task</returns>
public async Task Stop()
{
await _server.ShutdownAsync();
try
{
await _server.ShutdownAsync();
await _server.ShutdownTask;
}
catch (Exception ex)
{
Console.WriteLine(string.Format("Error stopping gRPC server: {0}", ex.Message));
}
}
public async Task Reset()

View File

@ -6,7 +6,7 @@ namespace Aurora.Utils
{
public class HashUtil
{
public static Guid GetHashGuid(string[] inputs)
public static Guid GetHash(string[] inputs)
{
string input = "";
foreach (string str in inputs)
@ -14,14 +14,12 @@ namespace Aurora.Utils
input += str;
}
Guid result;
using (SHA256 sha = SHA256.Create())
{
byte[] hash = sha.ComputeHash(Encoding.Default.GetBytes(input));
result = new Guid(hash);
}
return result;
byte[] stringbytes = Encoding.UTF8.GetBytes(input);
byte[] hashedBytes = new System.Security.Cryptography
.SHA1CryptoServiceProvider()
.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
return new Guid(hashedBytes);
}
}
}