From df2a6b4bfcb1d6da98fc46e1efb62c1321fc1dc0 Mon Sep 17 00:00:00 2001 From: watsonb8 Date: Mon, 20 Jan 2020 22:53:33 -0500 Subject: [PATCH] Successful unit test setup and tear down --- .../ControllerTests/MembersControllerTest.cs | 60 +++++++++++++++++++ .../PartyControllerTest.cs} | 26 ++++---- Aurora.test/Models/CallContext.cs | 39 ++++++++++++ Aurora/Proto/party.v2.proto | 4 +- .../Server/Controllers/MemberController.cs | 18 ++++-- .../Server/Controllers/PartyController.cs | 7 ++- Aurora/Services/Server/ServerService.cs | 30 ++++++---- Aurora/Utils/HashUtil.cs | 16 +++-- 8 files changed, 159 insertions(+), 41 deletions(-) create mode 100644 Aurora.test/ControllerTests/MembersControllerTest.cs rename Aurora.test/{UnitTest1.cs => ControllerTests/PartyControllerTest.cs} (55%) create mode 100644 Aurora.test/Models/CallContext.cs diff --git a/Aurora.test/ControllerTests/MembersControllerTest.cs b/Aurora.test/ControllerTests/MembersControllerTest.cs new file mode 100644 index 0000000..9be17eb --- /dev/null +++ b/Aurora.test/ControllerTests/MembersControllerTest.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/Aurora.test/UnitTest1.cs b/Aurora.test/ControllerTests/PartyControllerTest.cs similarity index 55% rename from Aurora.test/UnitTest1.cs rename to Aurora.test/ControllerTests/PartyControllerTest.cs index 48f8c42..0f4c5df 100644 --- a/Aurora.test/UnitTest1.cs +++ b/Aurora.test/ControllerTests/PartyControllerTest.cs @@ -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"); } } } \ No newline at end of file diff --git a/Aurora.test/Models/CallContext.cs b/Aurora.test/Models/CallContext.cs new file mode 100644 index 0000000..420c245 --- /dev/null +++ b/Aurora.test/Models/CallContext.cs @@ -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(); + } + } +} \ No newline at end of file diff --git a/Aurora/Proto/party.v2.proto b/Aurora/Proto/party.v2.proto index 61c71b3..52efeee 100644 --- a/Aurora/Proto/party.v2.proto +++ b/Aurora/Proto/party.v2.proto @@ -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; diff --git a/Aurora/Services/Server/Controllers/MemberController.cs b/Aurora/Services/Server/Controllers/MemberController.cs index 8e95955..2dbc70c 100644 --- a/Aurora/Services/Server/Controllers/MemberController.cs +++ b/Aurora/Services/Server/Controllers/MemberController.cs @@ -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 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 DeleteMember(DeleteMemberRequest request, Grpc.Core.ServerCallContext context) + public override Task 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); } } } \ No newline at end of file diff --git a/Aurora/Services/Server/Controllers/PartyController.cs b/Aurora/Services/Server/Controllers/PartyController.cs index 28b28c7..1ee14ea 100644 --- a/Aurora/Services/Server/Controllers/PartyController.cs +++ b/Aurora/Services/Server/Controllers/PartyController.cs @@ -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 /// public RemotePartyController(string partyName, string description) { - this._startDateTime = DateTime.Now; + this._startDateTime = DateTime.UtcNow; this._displayName = partyName; this._description = description; this._memberList = new SortedList(); @@ -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(), diff --git a/Aurora/Services/Server/ServerService.cs b/Aurora/Services/Server/ServerService.cs index 4cc8e37..2f21af0 100644 --- a/Aurora/Services/Server/ServerService.cs +++ b/Aurora/Services/Server/ServerService.cs @@ -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 /// Task 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() diff --git a/Aurora/Utils/HashUtil.cs b/Aurora/Utils/HashUtil.cs index 7d0e2fb..0644870 100644 --- a/Aurora/Utils/HashUtil.cs +++ b/Aurora/Utils/HashUtil.cs @@ -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); } } }