Skip to content

Commit

Permalink
Merge branch 'release/v0.4.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
jirenius committed Mar 10, 2021
2 parents f4ccab4 + bf7ca36 commit 94df05d
Show file tree
Hide file tree
Showing 9 changed files with 336 additions and 47 deletions.
16 changes: 15 additions & 1 deletion ResgateIO.Service.UnitTests/BaseHandlerTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

Expand Down Expand Up @@ -600,5 +599,20 @@ public void AuthMethod_DuplicateMethods_ThrowsInvalidOperationException()
Assert.Throws<InvalidOperationException>(() => new AuthMethod_DuplicateMethods_ThrowsInvalidOperationException_Class());
}
#endregion

#region OnRegister
class OnRegister_ServiceAndPattern_IsExposed_Class : BaseHandler { }
[Fact]
public void OnRegister_ServiceAndPattern_IsExposed()
{
var handler = new OnRegister_ServiceAndPattern_IsExposed_Class();
var service = new ResService("");
Assert.Null(handler.Service);
Assert.Null(handler.FullPattern);
handler.OnRegister(service, "test.model");
Assert.Equal(service, handler.Service);
Assert.Equal("test.model", handler.FullPattern);
}
#endregion
}
}
15 changes: 15 additions & 0 deletions ResgateIO.Service.UnitTests/DynamicHandlerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -522,5 +522,20 @@ public void EnabledHandlers_WithMultipleHandlers_FlagsAreSet()
.Get(r => { });
Assert.Equal(HandlerTypes.Access | HandlerTypes.Get, handler.EnabledHandlers);
}

#region OnRegister
class OnRegister_ServiceAndPattern_IsExposed_Class : BaseHandler { }
[Fact]
public void OnRegister_ServiceAndPattern_IsExposed()
{
var handler = new OnRegister_ServiceAndPattern_IsExposed_Class();
var service = new ResService("");
Assert.Null(handler.Service);
Assert.Null(handler.FullPattern);
handler.OnRegister(service, "test.model");
Assert.Equal(service, handler.Service);
Assert.Equal("test.model", handler.FullPattern);
}
#endregion
}
}
81 changes: 79 additions & 2 deletions ResgateIO.Service.UnitTests/RouterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,26 @@ public static IEnumerable<object[]> GetMountToSubpathTestData()
yield return new object[] { "test", null, "sub", "$id", "test.sub.foo" };
yield return new object[] { "test", "sub", null, ">", "test.sub.foo.bar" };
}

public static IEnumerable<object[]> GetRegisterTestData()
{
yield return new object[] { "", "model", "sub.model" };
yield return new object[] { "", "model.foo", "sub.model.foo" };
yield return new object[] { "", "model.$id", "sub.model.$id" };
yield return new object[] { "", "model.>", "sub.model.>" };
yield return new object[] { "", "$id", "sub.$id" };
yield return new object[] { "", ">", "sub.>" };
yield return new object[] { "", "$id.>", "sub.$id.>" };
yield return new object[] { "", "$id.$foo", "sub.$id.$foo" };
yield return new object[] { "test", "model", "test.sub.model" };
yield return new object[] { "test", "model.foo", "test.sub.model.foo" };
yield return new object[] { "test", "model.$id", "test.sub.model.$id" };
yield return new object[] { "test", "model.>", "test.sub.model.>" };
yield return new object[] { "test", "$id", "test.sub.$id" };
yield return new object[] { "test", ">", "test.sub.>" };
yield return new object[] { "test", "$id.>", "test.sub.$id.>" };
yield return new object[] { "test", "$id.$foo", "test.sub.$id.$foo" };
}
#endregion

#region AddHandler
Expand Down Expand Up @@ -805,7 +825,7 @@ public void ValidateEventListeners_AddEventListenerWithoutAddHandler_ThrowsInval
{
Router r = new Router(pattern);
r.AddEventListener(path, (sender, ev) => { });
Assert.Throws<InvalidOperationException>(() => r.ValidateEventListeners());
Assert.Throws<AggregateException>(() => r.ValidateEventListeners());
}

[Theory]
Expand All @@ -816,7 +836,7 @@ public void ValidateEventListeners_AddEventListenerWithFewerAddHandler_ThrowsInv
r.AddEventListener(path, (sender, ev) => { });
r.AddHandler("completely.different", new DynamicHandler());
r.AddEventListener("completely.different", (sender, ev) => { });
Assert.Throws<InvalidOperationException>(() => r.ValidateEventListeners());
Assert.Throws<AggregateException>(() => r.ValidateEventListeners());
}
#endregion

Expand Down Expand Up @@ -936,5 +956,62 @@ public void EventListenerAttribute_MultiplePublicMethods_AreAdded()
Assert.Equal(1, h.Called2);
}
#endregion

#region Register

[Theory]
[InlineData("", "model", "model")]
[InlineData("", "model.foo", "model.foo")]
[InlineData("", "model.$id", "model.$id")]
[InlineData("", "model.>", "model.>")]
[InlineData("", "$id", "$id")]
[InlineData("", ">", ">")]
[InlineData("", "$id.>", "$id.>")]
[InlineData("", "$id.$foo", "$id.$foo")]
[InlineData("test", "model", "test.model")]
[InlineData("test", "model.foo", "test.model.foo")]
[InlineData("test", "model.$id", "test.model.$id")]
[InlineData("test", "model.>", "test.model.>")]
[InlineData("test", "$id", "test.$id")]
[InlineData("test", ">", "test.>")]
[InlineData("test", "$id.>", "test.$id.>")]
[InlineData("test", "$id.$foo", "test.$id.$foo")]
public void Register_WithService_CallsOnRegister(string pattern, string subpattern, string expectedFullPattern)
{
var service = new ResService(pattern);
var handler = new DynamicHandler().SetType(ResourceType.Model);
service.AddHandler(subpattern, handler);
Assert.Equal(service, handler.Service);
Assert.Equal(expectedFullPattern, handler.FullPattern);
}

[Theory]
[MemberData(nameof(GetRegisterTestData))]
public void Register_AddHandlerToRouterBeforeMountingToService_CallsOnRegister(string pattern, string subpattern, string expectedFullPattern)
{
var service = new ResService(pattern);
var router = new Router();
var handler = new DynamicHandler().SetType(ResourceType.Model);
router.AddHandler(subpattern, handler);
Assert.Null(handler.Service);
Assert.Null(handler.FullPattern);
service.Mount("sub", router);
Assert.Equal(service, handler.Service);
Assert.Equal(expectedFullPattern, handler.FullPattern);
}

[Theory]
[MemberData(nameof(GetRegisterTestData))]
public void Register_AddHandlerToRouterAfterMountingToService_CallsOnRegister(string pattern, string subpattern, string expectedFullPattern)
{
var service = new ResService(pattern);
var router = new Router();
var handler = new DynamicHandler().SetType(ResourceType.Model);
service.Mount("sub", router);
router.AddHandler(subpattern, handler);
Assert.Equal(service, handler.Service);
Assert.Equal(expectedFullPattern, handler.FullPattern);
}
#endregion
}
}
9 changes: 8 additions & 1 deletion ResgateIO.Service/IAsyncHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ public interface IAsyncHandler
/// <returns>A task that represents the asynchronous handling.</returns>
Task Handle(IRequest request);

/// <summary>
/// Called when the handler is registered to a service.
/// </summary>
/// <param name="service">Service which the handler is registered to.</param>
/// <param name="pattern">Full resource id pattern being handled.</param>
void OnRegister(ResService service, String pattern);

/// <summary>
/// Applies modifying events onto the resource.
/// </summary>
Expand Down Expand Up @@ -62,4 +69,4 @@ public interface IAsyncHandler
/// <returns>A task that represents the asynchronous handling.</returns>
Task Apply(IResourceContext resource, EventArgs ev);
}
}
}
34 changes: 19 additions & 15 deletions ResgateIO.Service/ResService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private enum State { Stopped, Starting, Started, Stopping };
public ResService() : base()
{
Log = new ConsoleLogger();
Register(this);
}

/// <summary>
Expand All @@ -105,6 +106,7 @@ public ResService() : base()
public ResService(string name) : base(name)
{
Log = new ConsoleLogger();
Register(this);
}

/// <summary>
Expand Down Expand Up @@ -143,14 +145,7 @@ public ResService SetQueryDuration(TimeSpan duration)
public ResService SetLogger(ILogger logger)
{
assertStopped();
if (logger == null)
{
logger = new VoidLogger();
}
else
{
Log = logger;
}
Log = logger ?? new VoidLogger();
return this;
}

Expand Down Expand Up @@ -183,7 +178,8 @@ public ResService SetOwnedResources(string[] resources, string[] access)
/// it calls the appropriate handler method.
/// </summary>
/// <param name="conn">Connection to NATS Server</param>
public void Serve(IConnection conn) {
public void Serve(IConnection conn)
{
lock (stateLock)
{
assertStopped();
Expand All @@ -207,7 +203,8 @@ public void Serve(IConnection conn) {
/// or until successfully reconnecting, upon which Reset will be called.
/// </summary>
/// <param name="url">URL to NATS Server.</param>
public void Serve(string url) {
public void Serve(string url)
{
lock (stateLock)
{
assertStopped();
Expand Down Expand Up @@ -536,7 +533,8 @@ private void handleMessage(object sender, MsgHandlerEventArgs e)

// Get request type
Int32 idx = subj.IndexOf('.');
if (idx < 0) {
if (idx < 0)
{
// Shouldn't be possible unless NATS is really acting up
OnError("Invalid request subject: {0}", subj);
return;
Expand Down Expand Up @@ -616,6 +614,7 @@ public void ResetAll()
/// Sends a raw data message to NATS server on a given subject,
/// logging any exception.
/// </summary>
/// <remarks>This is a low level method, only to be used if you are familiar with the RES protocol.</remarks>
/// <param name="subject">Message subject</param>
/// <param name="data">Message JSON encoded data</param>
internal void RawSend(string subject, byte[] data)
Expand All @@ -631,9 +630,10 @@ internal void RawSend(string subject, byte[] data)
}

/// <summary>
/// Sends a message to NATS server on a given subject,
/// logging any exception.
/// Sends a JSON encoded message to NATS server on a given subject,
/// trace logging the message, and logging any exception.
/// </summary>
/// <remarks>This is a low level method, only to be used if you are familiar with the RES protocol.</remarks>
/// <param name="subject">Message subject</param>
/// <param name="payload">Message payload</param>
internal void Send(string subject, object payload)
Expand All @@ -659,6 +659,10 @@ internal void Send(string subject, object payload)
}
}

/// <summary>
/// Adds a query
/// </summary>
/// <param name="queryEvent"></param>
internal void AddQueryEvent(QueryEvent queryEvent)
{
if (queryEvent.Start())
Expand Down Expand Up @@ -752,7 +756,7 @@ private async Task processWork(Work work)
{
await task();
}
catch(Exception ex)
catch (Exception ex)
{
OnError("Exception encountered running resource task:\n{0}", ex.ToString());
}
Expand Down Expand Up @@ -798,7 +802,7 @@ private Task processRequest(Msg msg, String rtype, String rname, String method,
reqInput.URI,
reqInput.Query);
}
catch(Exception ex)
catch (Exception ex)
{
OnError("Error deserializing incoming request: {0}", msg.Data == null ? "<null>" : Encoding.UTF8.GetString(msg.Data));
req = new Request(this, msg);
Expand Down
4 changes: 2 additions & 2 deletions ResgateIO.Service/ResgateIO.Service.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.6;netstandard2.0;net45</TargetFrameworks>
<PackageId>ResgateIO.Service</PackageId>
<Version>0.4.3</Version>
<Version>0.4.4</Version>
<Title>RES Service .NET library</Title>
<Authors>Samuel Jirenius</Authors>
<Owners>Samuel Jirenius</Owners>
<Company>Resgate.io</Company>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageProjectUrl>https://resgate.io/</PackageProjectUrl>
<Description>RES service library for building next gen realtime APIs with synchronized clients using Resgate.</Description>
<PackageReleaseNotes>https://github.com/jirenius/csharp-res/releases/tag/v0.4.3</PackageReleaseNotes>
<PackageReleaseNotes>https://github.com/jirenius/csharp-res/releases/tag/v0.4.4</PackageReleaseNotes>
<Copyright>Copyright 2019 Samuel Jirenius</Copyright>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/jirenius/csharp-res</RepositoryUrl>
Expand Down
Loading

0 comments on commit 94df05d

Please sign in to comment.