Compare commits

...

4 Commits

Author SHA1 Message Date
Brandon Watson
4a4cef8dd7 Adding pagination tests 2021-03-05 16:13:25 -05:00
Brandon Watson
24fd683cb7 Adding cursor size tests 2021-03-05 14:30:25 -05:00
Brandon Watson
b61ffde4c9 WIP 2021-03-05 11:56:51 -05:00
Brandon Watson
91d0a55d5e Re-arranging files to add test project. Adding WIP cursor list 2021-03-04 12:08:53 -05:00
18 changed files with 502 additions and 10 deletions

View File

@ -0,0 +1,27 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Faker.Net" Version="1.4.108" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.7.1" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="1.3.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AuroraSignal\aurora-cradle-sharp.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,167 @@
using Xunit;
using Aurora.Services.Signal;
using Aurora.Cursor;
using Faker;
using System;
using System.Collections.Generic;
using System.Linq;
namespace AuroraSignal.test
{
public class CursorListTest
{
[Theory()]
[InlineData(SortDirection.Asc)]
[InlineData(SortDirection.Desc)]
public void CursorListSortOnStringValue(SortDirection direction)
{
CursorList<Party> list = new CursorList<Party>();
list.Add(new Party() { Name = "asdf", Id = Faker.RandomNumber.Next().ToString() });
list.Add(new Party() { Name = "bsdf", Id = Faker.RandomNumber.Next().ToString() });
list.Add(new Party() { Name = "csdf", Id = Faker.RandomNumber.Next().ToString() });
CursorResult<Party> result = new Cursor<Party>(ref list)
.WithSort(item => item.Value.Name, direction)
.GetNextPage();
if (direction == SortDirection.Desc)
{
Assert.Collection<Party>(result.Result,
item => item.Name.Equals("asdf"),
item => item.Name.Equals("bsdf"),
item => item.Name.Equals("csdf"));
}
else
{
Assert.Collection<Party>(result.Result,
item => item.Name.Equals("csdf"),
item => item.Name.Equals("bsdf"),
item => item.Name.Equals("asdf"));
}
}
[Theory()]
[InlineData(2)]
[InlineData(10)]
[InlineData(49)]
[InlineData(51)]
[InlineData(-1)]
public void CursorListSizeTest(int size)
{
int numOfItems = 50;
CursorList<Party> list = new CursorList<Party>();
// Add items to cursor list
for (int i = 0; i < numOfItems; i++)
{
list.Add(new Party()
{
Name = string.Join(" ",
Faker.Lorem.Words(2)),
Id = Faker.RandomNumber.Next().ToString()
});
}
Cursor<Party> cursor = new Cursor<Party>(ref list);
if (size < 0)
{
Assert.Throws<System.InvalidOperationException>(() =>
{
cursor.WithSize(size).GetNextPage();
});
}
else if (size > numOfItems)
{
CursorResult<Party> res = cursor.WithSize(size).GetNextPage();
Assert.Equal(res.Result.Count, numOfItems);
}
else if (size < numOfItems && size > 0)
{
CursorResult<Party> res = cursor.WithSize(size).GetNextPage();
Assert.Equal(res.Result.Count, size);
}
}
[Theory()]
[InlineData(2)]
[InlineData(10)]
[InlineData(3)]
public void CursorPaginationTest(int pageSize)
{
int numOfItems = 50;
CursorList<Party> list = new CursorList<Party>();
// Add items to cursor list
for (int i = 0; i < numOfItems; i++)
{
list.Add(new Party()
{
Name = string.Join(" ",
Faker.Lorem.Words(2)),
Id = Faker.RandomNumber.Next().ToString()
});
}
string pageToken = null;
Cursor<Party> cursor = new Cursor<Party>(ref list);
List<Party> pagedResults = new List<Party>();
while (pageToken != string.Empty)
{
CursorResult<Party> res = cursor
.WithSize(pageSize)
.WithNextPageToken(pageToken)
.GetNextPage();
pagedResults.AddRange(res.Result);
pageToken = res.NextPageToken;
}
Assert.Equal(pagedResults.Count, numOfItems);
}
[Fact()]
public void CursorPaginationWithSortTest()
{
int numOfItems = 50;
CursorList<Party> cursorList = new CursorList<Party>();
// Add items to cursor list
for (int i = 0; i < numOfItems; i++)
{
cursorList.Add(new Party()
{
Name = string.Join(" ",
Faker.Lorem.Words(2)),
Id = Faker.RandomNumber.Next().ToString()
});
}
var orderedList = cursorList.ToList().OrderBy(item => item.Value.Name).ToList().ConvertAll(item => item.Value);
string pageToken = null;
Cursor<Party> cursor = new Cursor<Party>(ref cursorList);
List<Party> pagedResults = new List<Party>();
while (pageToken != string.Empty)
{
CursorResult<Party> res = cursor
.WithSize(10)
.WithSort(item => item.Value.Name, SortDirection.Asc)
.WithNextPageToken(pageToken)
.GetNextPage();
pagedResults.AddRange(res.Result);
pageToken = res.NextPageToken;
}
var list = cursorList.ToList();
for (int i = 0; i < orderedList.Count; i++)
{
Assert.Equal(orderedList[i], pagedResults[i]);
}
}
}
}

View File

@ -0,0 +1,9 @@
using Aurora.Cursor;
namespace Aurora.Services.Signal
{
public partial class Party : ICursorObject
{
}
}

View File

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Linq;
#nullable enable
namespace Aurora.Cursor
{
public enum SortDirection
{
Asc,
Desc,
}
public class Cursor<T> where T : ICursorObject
{
private CursorList<T> _list;
private string? _previousPageToken;
private string? _nextPageToken;
private int _pageSize;
private Func<KeyValuePair<string, T>, string> _sortDelgate;
private SortDirection _direction;
public Cursor(ref CursorList<T> list)
{
this._list = list;
this._previousPageToken = string.Empty;
this._nextPageToken = string.Empty;
this._pageSize = 10;
this._sortDelgate = delegate (KeyValuePair<string, T> item)
{
return item.Key;
};
}
public CursorResult<T> GetNextPage()
{
if (this._pageSize < 0)
{
throw new InvalidOperationException("Page Size must be greater than zero");
}
List<KeyValuePair<string, T>> tmpList;
// Sort reference list
if (this._direction == SortDirection.Desc)
{
tmpList = this._list.OrderByDescending(this._sortDelgate).ToList();
}
else
{
tmpList = this._list.OrderBy(this._sortDelgate).ToList();
}
if (tmpList == null)
{
throw new System.NullReferenceException();
}
int startIdx = 0;
if (!string.IsNullOrEmpty(this._nextPageToken))
{
// TODO find another way to index into the list that's not a regular array search
startIdx = tmpList.FindIndex(item => item.Key == this._nextPageToken) + 1;
}
int adjustedSize = this._pageSize;
if (startIdx + this._pageSize > tmpList.Count)
{
adjustedSize = this._pageSize - ((startIdx + _pageSize) - tmpList.Count);
}
List<KeyValuePair<string, T>> selection = new List<KeyValuePair<string, T>>();
if (adjustedSize != 0)
{
selection = tmpList.GetRange(startIdx, adjustedSize);
}
return new CursorResult<T>
{
NextPageToken = this._pageSize == selection.Count ? selection[selection.Count - 1].Key : string.Empty,
PrevPageToken = string.Empty,
Count = this._list.Count,
Result = selection.ConvertAll(item => item.Value)
};
}
public CursorResult<T> GetPreviousPage()
{
throw new NotImplementedException();
}
public Cursor<T> WithNextPageToken(string nextPageToken)
{
this._nextPageToken = nextPageToken;
return this;
}
public Cursor<T> WithPreviousPageToken(string prevPageToken)
{
this._previousPageToken = prevPageToken;
return this;
}
public Cursor<T> WithSort(Func<KeyValuePair<string, T>, string> sortDelegate, SortDirection direction)
{
this._sortDelgate = sortDelegate;
this._direction = direction;
return this;
}
public Cursor<T> WithSize(int size)
{
this._pageSize = size;
return this;
}
}
}

View File

@ -0,0 +1,32 @@
using System.Collections.Generic;
using System.Collections;
using System;
using System.Linq;
using Aurora.Utils;
#nullable enable
namespace Aurora.Cursor
{
public class CursorList<T> : SortedList<string, T> where T : ICursorObject
{
public CursorList()
{
}
public CursorList<T> Add(T item)
{
string itemHashId = HashUtil.GetHash(new string[] { item.Id, item.GetHashCode().ToString() }).ToString();
bool res = this.TryAdd(itemHashId, item);
if (res == false)
{
throw new System.Exception("Failed to add item to cursor list");
}
return this;
}
}
}

View File

@ -0,0 +1,9 @@
using System.Collections.Generic;
namespace Aurora.Cursor
{
public interface ICursorObject
{
string Id { get; set; }
}
}

View File

@ -0,0 +1,25 @@
using System.Collections.Generic;
namespace Aurora.Cursor
{
public class CursorResult<T>
{
public CursorResult()
{
Result = new List<T>();
}
public CursorResult(CursorResult<T> cpy)
{
NextPageToken = cpy.NextPageToken;
PrevPageToken = cpy.PrevPageToken;
Result = cpy.Result;
Count = cpy.Count;
}
public string NextPageToken { get; set; }
public string PrevPageToken { get; set; }
public List<T> Result { get; set; }
public int Count { get; set; }
}
}

View File

@ -6,7 +6,7 @@ using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
namespace aurora_cradle_sharp namespace Aurora
{ {
public class Program public class Program
{ {

View File

@ -1,6 +1,6 @@
syntax = "proto3"; syntax = "proto3";
option csharp_namespace = "aurora_cradle_sharp"; option csharp_namespace = "Aurora.Services.Signal";
package signal; package signal;
@ -9,28 +9,38 @@ import "google/protobuf/timestamp.proto";
import "google/protobuf/field_mask.proto"; import "google/protobuf/field_mask.proto";
import "google/protobuf/empty.proto"; import "google/protobuf/empty.proto";
service Signal{
service Signal {
//************** //**************
//Party Resource //Party Resource
//************** //**************
//Get Party //Get Party
rpc ListParties(ListPartiesRequest) returns (ListPartiesResponse); rpc ListParties(ListPartiesRequest) returns (ListPartiesResponse);
rpc GetParty(GetPartyRequest) returns (Party); rpc GetParty(GetPartyRequest) returns (Party);
rpc UpdateParty(UpdatePartyRequest) returns (Party); rpc UpdateParty(UpdatePartyRequest) returns (Party);
rpc CreateParty(CreatePartyRequest) returns (Party); rpc CreateParty(CreatePartyRequest) returns (Party);
rpc DeleteParty(DeletePartyRequest) returns (google.protobuf.Empty); rpc DeleteParty(DeletePartyRequest) returns (google.protobuf.Empty);
//*************************** //***************************
//EventSubscriptions Resource //EventSubscriptions Resource
//*************************** //***************************
//List //List
rpc ListEventSubscriptions(ListEventSubscriptionsRequest) returns (ListEventSubscriptionsResponse); rpc ListEventSubscriptions(ListEventSubscriptionsRequest) returns (ListEventSubscriptionsResponse);
//Create //Create
rpc SubscribeToEvent(EventSubscriptionRequest) returns (EventSubscription); rpc SubscribeToEvent(EventSubscriptionRequest) returns (EventSubscription);
//Delete //Delete
rpc DeleteEventSubscription(DeleteEventSubscriptionRequest) returns (google.protobuf.Empty); rpc DeleteEventSubscription(DeleteEventSubscriptionRequest) returns (google.protobuf.Empty);
//CUSTOM: Create EventSubscription List //CUSTOM: Create EventSubscription List
rpc SubscribeToEvents(EventSubscriptionListRequest) returns (EventSubscriptionListResponse); rpc SubscribeToEvents(EventSubscriptionListRequest) returns (EventSubscriptionListResponse);
//CUSTOM: Delete all //CUSTOM: Delete all
rpc DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest) returns (google.protobuf.Empty); rpc DeleteAllEventSubscriptions(DeleteAllEventSubscriptionsRequest) returns (google.protobuf.Empty);
@ -40,47 +50,58 @@ service Signal{
//Get //Get
rpc GetEventStream(GetEventsRequest) returns (stream BaseEvent) {}; rpc GetEventStream(GetEventsRequest) returns (stream BaseEvent) {};
} }
message Party { message Party {
//The resource name of the party //The resource name of the party
string name = 1; string name = 1;
string party_id = 2; string id = 2;
string display_name = 3; string display_name = 3;
string description = 4; string description = 4;
string host_ip = 5; string host_ip = 5;
google.protobuf.Timestamp created_on = 6; google.protobuf.Timestamp created_on = 6;
} }
message PartyListItem { message PartyListItem {
string name = 1; string name = 1;
string party_id = 2; string id = 2;
} }
message ListPartiesRequest { message ListPartiesRequest {
int32 page_size = 1; int32 page_size = 1;
string page_token = 2; string page_token = 2;
} }
message ListPartiesResponse { message ListPartiesResponse {
repeated PartyListItem parties = 1; repeated PartyListItem parties = 1;
string next_page_token = 2; string next_page_token = 2;
} }
message GetPartyRequest { message GetPartyRequest {
string party_id = 1; string party_id = 1;
} }
message CreatePartyRequest { message CreatePartyRequest {
string party_id = 1; string party_id = 1;
Party party = 2; Party party = 2;
} }
message DeletePartyRequest { message DeletePartyRequest {
string party_id = 1; string party_id = 1;
} }
message UpdatePartyRequest { message UpdatePartyRequest {
Party party = 1; Party party = 1;
google.protobuf.FieldMask update_mask = 2; google.protobuf.FieldMask update_mask = 2;
} }
/* Event Types */ /* Event Types */
enum EventType { enum EventType {
NewPartiesAvailable = 0; NewPartiesAvailable = 0;
} }
message NewPartiesAvailableEvent { message NewPartiesAvailableEvent {
} }
message BaseEvent { message BaseEvent {
//Resource name of the event ? //Resource name of the event ?
string name = 1; string name = 1;
@ -89,42 +110,51 @@ message BaseEvent {
NewPartiesAvailableEvent new_parties_available_event = 3; NewPartiesAvailableEvent new_parties_available_event = 3;
} }
} }
message EventSubscription { message EventSubscription {
EventType type = 2; EventType type = 2;
} }
message ListEventSubscriptionsRequest { message ListEventSubscriptionsRequest {
//Resource name of parent to the subscription list (The member) //Resource name of parent to the subscription list (The member)
string parent = 1; string parent = 1;
int32 page_size = 2; int32 page_size = 2;
string page_token = 3; string page_token = 3;
} }
message ListEventSubscriptionsResponse { message ListEventSubscriptionsResponse {
repeated EventSubscription subscriptions = 1; repeated EventSubscription subscriptions = 1;
string next_page_token = 2; string next_page_token = 2;
} }
message EventSubscriptionRequest { message EventSubscriptionRequest {
//Resource name of the parent to the subscription list (The member) //Resource name of the parent to the subscription list (The member)
string parent = 1; string parent = 1;
EventSubscription event_subscription = 2; EventSubscription event_subscription = 2;
} }
message DeleteEventSubscriptionRequest { message DeleteEventSubscriptionRequest {
//Resource name of the subscription to delete //Resource name of the subscription to delete
string parent = 1; string parent = 1;
EventType type = 2; EventType type = 2;
} }
message EventSubscriptionListRequest { message EventSubscriptionListRequest {
//Resource name of the parent to the subscription list (The member) //Resource name of the parent to the subscription list (The member)
string parent = 1; string parent = 1;
repeated EventSubscription event_subscriptions = 2; repeated EventSubscription event_subscriptions = 2;
} }
message EventSubscriptionListResponse { message EventSubscriptionListResponse {
repeated EventSubscription event_subscriptions = 1; repeated EventSubscription event_subscriptions = 1;
} }
message DeleteAllEventSubscriptionsRequest { message DeleteAllEventSubscriptionsRequest {
//Resource name of the parent to the subscription list (the member) //Resource name of the parent to the subscription list (the member)
string parent = 1; string parent = 1;
} }
message GetEventsRequest { message GetEventsRequest {
//Resource name of the parent to the event stream (the member) //Resource name of the parent to the event stream (the member)
string parent = 1; string parent = 1;
} }

View File

@ -0,0 +1,7 @@
namespace Aurora.Services.Signal
{
public partial class SignalService : Signal.SignalBase
{
}
}

View File

@ -0,0 +1,34 @@
using System.Threading.Tasks;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;
namespace Aurora.Services.Signal
{
public partial class SignalService : Signal.SignalBase
{
public override Task<Party> CreateParty(CreatePartyRequest request, ServerCallContext context)
{
return base.CreateParty(request, context);
}
public override Task<Empty> DeleteParty(DeletePartyRequest request, ServerCallContext context)
{
return base.DeleteParty(request, context);
}
public override Task<ListPartiesResponse> ListParties(ListPartiesRequest request, ServerCallContext context)
{
return base.ListParties(request, context);
}
public override Task<Party> GetParty(GetPartyRequest request, ServerCallContext context)
{
return base.GetParty(request, context);
}
public override Task<Party> UpdateParty(UpdatePartyRequest request, ServerCallContext context)
{
return base.UpdateParty(request, context);
}
}
}

View File

@ -4,15 +4,19 @@ using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Grpc.Core; using Grpc.Core;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Aurora.Cursor;
namespace aurora_cradle_sharp namespace Aurora.Services.Signal
{ {
public class SignalService : Signal.SignalBase public partial class SignalService : Signal.SignalBase
{ {
private readonly ILogger<SignalService> _logger; private readonly ILogger<SignalService> _logger;
private CursorList<Party> _partyList;
public SignalService(ILogger<SignalService> logger) public SignalService(ILogger<SignalService> logger)
{ {
_logger = logger; _logger = logger;
this._partyList = new CursorList<Party>();
} }
} }

View File

@ -7,8 +7,9 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Aurora.Services.Signal;
namespace aurora_cradle_sharp namespace Aurora
{ {
public class Startup public class Startup
{ {

View File

@ -0,0 +1,25 @@
using System.Security.Cryptography;
using System.Text;
using System;
namespace Aurora.Utils
{
public class HashUtil
{
public static Guid GetHash(string[] inputs)
{
string input = "";
foreach (string str in inputs)
{
input += str;
}
byte[] stringbytes = Encoding.UTF8.GetBytes(input);
byte[] hashedBytes = new System.Security.Cryptography
.SHA1CryptoServiceProvider()
.ComputeHash(stringbytes);
Array.Resize(ref hashedBytes, 16);
return new Guid(hashedBytes);
}
}
}

View File

@ -7,6 +7,9 @@
"settings": { "settings": {
"files.exclude": { "files.exclude": {
"**/obj": true "**/obj": true
} },
"dotnet-test-explorer.testProjectPath": "./AuroraSignal.test",
"editor.formatOnSave": true,
"editor.defaultFormatter": "ms-dotnettools.csharp"
} }
} }