Skip to content

Commit

Permalink
Added CustomCheckSucceeded and CustomCheckFailed event publishing for…
Browse files Browse the repository at this point in the history
… external integration.
  • Loading branch information
John Simons committed Feb 4, 2015
1 parent 2d08580 commit 3a8715b
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
namespace ServiceBus.Management.AcceptanceTests.ExternalIntegrations
{
using System;
using Contexts;
using NServiceBus;
using NServiceBus.AcceptanceTesting;
using NServiceBus.Config;
using NServiceBus.Config.ConfigurationSource;
using NServiceBus.Features;
using NServiceBus.Unicast.Subscriptions;
using NServiceBus.Unicast.Subscriptions.MessageDrivenSubscriptions;
using NUnit.Framework;
using ServiceControl.Contracts;
using ServiceControl.Contracts.Operations;

[TestFixture]
public class When_a_message_has_custom_checks : AcceptanceTest
{
[Test]
public void Notification_should_be_published_on_the_bus()
{
var context = new MyContext();

Scenario.Define(context)
.WithEndpoint<ExternalIntegrationsManagementEndpoint>(b => b.Given((bus, c) => Subscriptions.OnEndpointSubscribed(s =>
{
if (s.SubscriberReturnAddress.Queue.Contains("ExternalProcessor"))
{
c.ExternalProcessorSubscribed = true;
}
}, () => c.ExternalProcessorSubscribed = true))
.When(c => c.ExternalProcessorSubscribed, bus =>
{
bus.Publish(new ServiceControl.Contracts.CustomChecks.CustomCheckSucceeded
{
Category = "Testing",
CustomCheckId = "Success custom check",
OriginatingEndpoint = new EndpointDetails
{
Host = "MyHost",
HostId = Guid.Empty,
Name = "Testing"
},
SucceededAt = DateTime.Now,
});
bus.Publish(new ServiceControl.Contracts.CustomChecks.CustomCheckFailed
{
Category = "Testing",
CustomCheckId = "Fail custom check",
OriginatingEndpoint = new EndpointDetails
{
Host = "MyHost",
HostId = Guid.Empty,
Name = "Testing"
},
FailedAt = DateTime.Now,
FailureReason = "Because I can",
});
}).AppConfig(PathToAppConfig))
.WithEndpoint<ExternalProcessor>(b => b.Given((bus, c) =>
{
bus.Subscribe<CustomCheckSucceeded>();
bus.Subscribe<CustomCheckFailed>();
}))
.Done(c => c.CustomCheckFailedReceived && c.CustomCheckFailedReceived)
.Run();

Assert.IsTrue(context.CustomCheckFailedReceived);
Assert.IsTrue(context.CustomCheckSucceededReceived);
}

[Serializable]
public class Subscriptions
{
public static Action<Action<SubscriptionEventArgs>, Action> OnEndpointSubscribed = (actionToPerformIfMessageDrivenSubscriptions, actionToPerformIfMessageDrivenSubscriptionsNotRequired) =>
{
if (Feature.IsEnabled<MessageDrivenSubscriptions>())
{
Configure.Instance.Builder.Build<MessageDrivenSubscriptionManager>().ClientSubscribed +=
(sender, args) =>
{
actionToPerformIfMessageDrivenSubscriptions(args);
};

return;
}

actionToPerformIfMessageDrivenSubscriptionsNotRequired();
};
}

public class ExternalIntegrationsManagementEndpoint : EndpointConfigurationBuilder
{
public ExternalIntegrationsManagementEndpoint()
{
EndpointSetup<ExternalIntegrationsManagementEndpointSetup>();
}
}

public class ExternalProcessor : EndpointConfigurationBuilder
{
public ExternalProcessor()
{
EndpointSetup<JSonServer>();
}

public class CustomCheckSucceededHandler : IHandleMessages<CustomCheckSucceeded>
{
public MyContext Context { get; set; }

public void Handle(CustomCheckSucceeded message)
{
Context.CustomCheckSucceededReceived = true;
}
}

public class CustomCheckFailedHandler : IHandleMessages<CustomCheckFailed>
{
public MyContext Context { get; set; }

public void Handle(CustomCheckFailed message)
{
Context.CustomCheckFailedReceived = true;
}
}

public class UnicastOverride : IProvideConfiguration<UnicastBusConfig>
{
public UnicastBusConfig GetConfiguration()
{
var config = new UnicastBusConfig();
var serviceControlMapping = new MessageEndpointMapping
{
Messages = "ServiceControl.Contracts",
Endpoint = "Particular.ServiceControl"
};
config.MessageEndpointMappings.Add(serviceControlMapping);
return config;
}
}
}

public class MyContext : ScenarioContext
{
public bool CustomCheckSucceededReceived { get; set; }
public bool CustomCheckFailedReceived { get; set; }
public bool ExternalProcessorSubscribed { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@
<Compile Include="CustomChecks\When_a_custom_check_fails.cs" />
<Compile Include="CustomChecks\When_a_periodic_custom_check_fails.cs" />
<Compile Include="DebugSessions\When_an_endpoint_is_running_with_debug_sessions_enabled.cs" />
<Compile Include="ExternalIntegrations\JSonServer.cs" />
<Compile Include="ExternalIntegrations\JsonServer.cs" />
<Compile Include="ExternalIntegrations\When_a_message_has_custom_checks.cs" />
<Compile Include="ExternalIntegrations\When_a_message_has_failed.cs" />
<Compile Include="ExternalIntegrations\When_heartbeat_is_restored.cs" />
<Compile Include="ExternalIntegrations\When_encountered_an_error.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace ServiceControl.ExternalIntegrations
{
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using CustomCheckFailed = ServiceControl.Contracts.CustomCheckFailed;


public class CustomCheckFailedPublisher : EventPublisher<Contracts.CustomChecks.CustomCheckFailed, CustomCheckFailedPublisher.DispatchContext>
{
protected override DispatchContext CreateDispatchRequest(Contracts.CustomChecks.CustomCheckFailed @event)
{
return new DispatchContext
{
EndpointHost = @event.OriginatingEndpoint.Host,
EndpointHostId = @event.OriginatingEndpoint.HostId,
EndpointName = @event.OriginatingEndpoint.Name,
FailedAt = @event.FailedAt,
Category = @event.Category,
FailureReason = @event.FailureReason,
CustomCheckId = @event.CustomCheckId,
};
}

protected override IEnumerable<object> PublishEvents(IEnumerable<DispatchContext> contexts, IDocumentSession session)
{
return contexts.Select(r => new CustomCheckFailed
{
FailedAt = r.FailedAt,
Category = r.Category,
FailureReason = r.FailureReason,
CustomCheckId = r.CustomCheckId,
Host = r.EndpointHost,
HostId = r.EndpointHostId,
EndpointName = r.EndpointName
});
}

public class DispatchContext
{
public string EndpointName { get; set; }
public Guid EndpointHostId { get; set; }
public string EndpointHost { get; set; }
public string CustomCheckId { get; set; }
public string Category { get; set; }
public string FailureReason { get; set; }
public DateTime FailedAt { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
namespace ServiceControl.ExternalIntegrations
{
using System;
using System.Collections.Generic;
using System.Linq;
using Raven.Client;
using ServiceControl.Contracts;

public class CustomCheckSucceededPublisher : EventPublisher<Contracts.CustomChecks.CustomCheckSucceeded, CustomCheckSucceededPublisher.DispatchContext>
{
protected override DispatchContext CreateDispatchRequest(Contracts.CustomChecks.CustomCheckSucceeded @event)
{
return new DispatchContext
{
EndpointHost = @event.OriginatingEndpoint.Host,
EndpointHostId = @event.OriginatingEndpoint.HostId,
EndpointName = @event.OriginatingEndpoint.Name,
SucceededAt = @event.SucceededAt,
Category = @event.Category,
CustomCheckId = @event.CustomCheckId,
};
}

protected override IEnumerable<object> PublishEvents(IEnumerable<DispatchContext> contexts, IDocumentSession session)
{
return contexts.Select(r => new CustomCheckSucceeded
{
SucceededAt = r.SucceededAt,
Category = r.Category,
CustomCheckId = r.CustomCheckId,
Host = r.EndpointHost,
HostId = r.EndpointHostId,
EndpointName = r.EndpointName
});
}

public class DispatchContext
{
public string EndpointName { get; set; }
public Guid EndpointHostId { get; set; }
public string EndpointHost { get; set; }
public string CustomCheckId { get; set; }
public string Category { get; set; }
public DateTime SucceededAt { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public void Init()
Configure.Component<MessageFailedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<HeartbeatStoppedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<HeartbeatRestoredPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<CustomCheckFailedPublisher>(DependencyLifecycle.SingleInstance);
Configure.Component<CustomCheckSucceededPublisher>(DependencyLifecycle.SingleInstance);
}
}
}
2 changes: 2 additions & 0 deletions src/ServiceControl/ServiceControl.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -250,11 +250,13 @@
<Compile Include="CustomChecks\CustomCheckNotifications.cs" />
<Compile Include="EventLog\Definitions\MessageSubmittedForRetryDefinition.cs" />
<Compile Include="CustomChecks\CustomChecksIndex.cs" />
<Compile Include="ExternalIntegrations\CustomCheckSucceededPublisher.cs" />
<Compile Include="ExternalIntegrations\EventPublisher.cs" />
<Compile Include="ExternalIntegrations\ExternalIntegrationDispatchRequest.cs" />
<Compile Include="ExternalIntegrations\EventDispatcher.cs" />
<Compile Include="ExternalIntegrations\EventMappingHandler.cs" />
<Compile Include="ExternalIntegrations\ExternalIntegrationsInitializer.cs" />
<Compile Include="ExternalIntegrations\CustomCheckFailedPublisher.cs" />
<Compile Include="ExternalIntegrations\HeartbeatRestoredPublisher.cs" />
<Compile Include="ExternalIntegrations\HeartbeatStoppedPublisher.cs" />
<Compile Include="ExternalIntegrations\IEventPublisher.cs" />
Expand Down

0 comments on commit 3a8715b

Please sign in to comment.