public void Test_Ctor() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { Assert.That(context, Has.Property("ContextType").EqualTo(ContextType.New)); } }
public void Test_AttachedContext_Does_Not_Flush_On_Dispose() { TestMessage msg; TestMessage msgAttached = new TestMessage(); DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext(); DeferredChannelMessageContext attachedContext = new DeferredChannelMessageContext(ContextType.Attached); attachedContext.AddOrUpdateMessage("Attached", msgAttached, null); Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.True, "Attached context missing msg"); Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msgAttached); // Disposing inner context should not flush the messages attachedContext.Dispose(); Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.True, "Attached context missing msg"); Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msgAttached); // Disposing outer context should flush the messages outerContext.Dispose(); Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.False, "Attached context contains msg"); Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.False, "Outer context contains msg"); }
public void Test_Non_Transactional_Database_Context_Does_Not_FlushesMessages() { TestMessage msg; TestMessage msg1 = new TestMessage(); TestMessage msg2 = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { outerContext.AddOrUpdateMessage("Channel1", msg1, null); outerContext.AddOrUpdateMessage("Channel2", msg2, null); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg1); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg2); using (DatabaseContext context = DatabaseContext.GetContext()) { } Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg"); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg"); } }
public void Test_AddOrUpdateMessage_MultipleLevels() { TestMessage msg; TestMessage msgOuter = new TestMessage(); TestMessage msgInner = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { outerContext.AddOrUpdateMessage("OuterChannel", msgOuter, null); Assert.That(outerContext.TryGetMessage("OuterChannel", out msg), Is.True, "Outer context missing msg"); using (DeferredChannelMessageContext innerContext = new DeferredChannelMessageContext()) { innerContext.AddOrUpdateMessage("InnerChannel", msgInner, null); Assert.That(innerContext.TryGetMessage("InnerChannel", out msg), Is.True, "Inner context missing msg"); Assert.That(innerContext.TryGetMessage("OuterChannel", out msg), Is.False, "Inner context contains msg"); Assert.That(outerContext.TryGetMessage("OuterChannel", out msg), Is.True, "Outer context missing msg"); Assert.That(outerContext.TryGetMessage("InnerChannel", out msg), Is.False, "Outer context contains msg"); } Assert.That(outerContext.TryGetMessage("OuterChannel", out msg), Is.True, "Outer context missing msg"); Assert.That(outerContext.TryGetMessage("InnerChannel", out msg), Is.False, "Outer context contains msg"); } }
/// <summary> /// Entry point to the thread running background workflows. Used by monitoring software. /// </summary> /// <param name="o"> /// The <see cref="Action"/> to run. This cannot be null. /// </param> /// <exception cref="ArgumentException"> /// <paramref name="o"/> cannot be null and must be an <see cref="Action"/>. /// </exception> private static void StartThread(object o) { using (EntryPointContext.SetEntryPoint("WorkflowThread")) { ProcessMonitorWriter.Instance.Write("WorkflowThread"); try { Action action; action = o as Action; if (action == null) { throw new ArgumentException("Null or not an Action", "o"); } using (DeferredChannelMessageContext deferredMsgContext = new DeferredChannelMessageContext()) using (DatabaseContext.GetContext()) { action(); } } catch (Exception ex) { EventLog.Application.WriteError("WorkflowRunContext.StartThread: Unexpected exception thrown: {0}", ex); } } }
public void Test_Committing_Transaction_FlushesMessages() { TestMessage msg; TestMessage msg1 = new TestMessage(); TestMessage msg2 = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { outerContext.AddOrUpdateMessage("Channel1", msg1, null); outerContext.AddOrUpdateMessage("Channel2", msg2, null); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg1); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg2); using (DatabaseContext context = DatabaseContext.GetContext(true)) { context.CommitTransaction(); } Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.False, "Outer context contains msg"); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.False, "Outer context contains msg"); } }
public void Test_GetContext_NoContext() { DeferredChannelMessageContext context; context = DeferredChannelMessageContext.GetContext(); Assert.That(context, Has.Property("ContextType").EqualTo(ContextType.Detached)); }
public void Test_FlushMessages() { TestMessage msg; TestMessage msg1 = new TestMessage(); TestMessage msg2 = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { outerContext.AddOrUpdateMessage("Channel1", msg1, null); outerContext.AddOrUpdateMessage("Channel2", msg2, null); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg1); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg2); outerContext.FlushMessages(); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.False, "Outer context contains msg"); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.False, "Outer context contains msg"); } }
public void Test_Dispose_DifferentThread() { TestMessage msg; TestMessage msg1 = new TestMessage(); TestMessage msg2 = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { outerContext.AddOrUpdateMessage("Channel1", msg1, null); outerContext.AddOrUpdateMessage("Channel2", msg2, null); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg1); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msg2); // Call the dispose on a different thread. // This tests the Begin Request/End Request creation and disposal Thread tSetup = new Thread(new ThreadStart(() => { outerContext.Dispose(); })); tSetup.Start(); tSetup.Join(); Assert.That(outerContext.TryGetMessage("Channel1", out msg), Is.False, "Outer context contains msg"); Assert.That(outerContext.TryGetMessage("Channel2", out msg), Is.False, "Outer context contains msg"); } }
public void Teardown() { if (_deferredChannelMessageContext != null) { _deferredChannelMessageContext.Dispose(); _deferredChannelMessageContext = null; } }
public void Test_TryGetMessage_Null_Channel() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { TestMessage msg; Assert.Throws <ArgumentNullException>(() => context.TryGetMessage(null, out msg)); } }
public void Test_AddOrUpdateMessage_Null_Channel() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { TestMessage msg = new TestMessage(); msg.Data.Add(1); Assert.Throws <ArgumentNullException>(() => context.AddOrUpdateMessage(null, msg, null)); } }
public void Test_IsSet_OneLevel() { Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "Set beforehand"); using (new DeferredChannelMessageContext()) { Assert.That(DeferredChannelMessageContext.IsSet(), Is.True, "Not set"); } Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "Set afterwards"); }
public void Test_AddOrUpdateMessage_Null_Message() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { context.AddOrUpdateMessage <TestMessage>("TestChannel", null, null); TestMessage msg; Assert.That(context.TryGetMessage("TestChannel", out msg), Is.False, "TryGetMessage failed"); } }
public void TestBulkPublisher( ) { const string channelName = "myChannel"; using (var domain = new TestAppDomain( )) { Func <MessageEventArgs <TestMessage>, bool> predicate = m => m.Message.Action == PublisherActions.Remove && m.Message.Items.Count == 2 && m.Message.Items.Contains("a,:") && m.Message.Items.Contains("b,:"); var instance = domain.InjectType <SubscriberRefObject <TestMessage> >(channelName, RunAsDefaultTenant.DefaultTenantName, predicate); using (IDistributedMemoryManager manager = new RedisManager( )) { manager.Connect( ); using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { using (IChannel <TestMessage> channel1 = manager.GetChannel <TestMessage>(channelName)) { var message1 = new TestMessage { Action = PublisherActions.Remove }; message1.Items.Add("a,:"); channel1.Publish(message1, PublishOptions.None, false, (e, n) => e.Items.AddRange(n.Items)); using (IChannel <TestMessage> channel2 = manager.GetChannel <TestMessage>(channelName)) { var message2 = new TestMessage { Action = PublisherActions.Remove }; message2.Items.Add("b,:"); channel2.Publish(message2, PublishOptions.None, false, (e, n) => e.Items.AddRange(n.Items)); } } } bool waitOne = instance.MessageReceived.WaitOne(DefaultTimeout); Assert.IsTrue(waitOne, "No message received in " + DefaultTimeout + "ms."); Assert.GreaterOrEqual(instance.ReceivedMessages.Count, 1, "Received message count is invalid."); MessageEventArgs <TestMessage> args = instance.ReceivedMessages.FirstOrDefault(predicate); Assert.IsNotNull(args, "Received message is invalid."); } } }
/// <summary> /// Flush any deferred messages. /// </summary> private void FlushDeferredMessageContextMessages() { if (!DeferredChannelMessageContext.IsSet()) { return; } using (DeferredChannelMessageContext messageContext = DeferredChannelMessageContext.GetContext()) { messageContext.FlushMessages(); } }
public void Test_IsSet_TwoLevels() { Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "Set beforehand"); using (new DeferredChannelMessageContext()) { Assert.That(DeferredChannelMessageContext.IsSet(), Is.True, "Not pre-second level"); using (new DeferredChannelMessageContext()) { Assert.That(DeferredChannelMessageContext.IsSet(), Is.True, "Not second level"); } Assert.That(DeferredChannelMessageContext.IsSet(), Is.True, "Not post-second level"); } Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "Set afterwards"); }
public void Test_AddOrUpdateMessage_Merge_No_Merge_Function() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { TestMessage msg = new TestMessage(); msg.Data.Add(1); context.AddOrUpdateMessage("TestChannel", msg, null); TestMessage msg2 = new TestMessage(); msg2.Data.Add(2); Assert.Throws <InvalidOperationException>(() => context.AddOrUpdateMessage("TestChannel", msg2, null)); } }
public void Test_AddOrUpdateMessage_TryGetMessage() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { TestMessage msg = new TestMessage(); msg.Data.Add(1); context.AddOrUpdateMessage("TestChannel", msg, null); TestMessage addedMsg; Assert.That(context.TryGetMessage("TestChannel", out addedMsg), Is.True, "TryGetMessage failed"); Assert.AreSame(msg, addedMsg); } }
/// <summary> /// Publish_s the implementation. /// </summary> /// <param name="callback">The callback.</param> /// <param name="message">The message.</param> /// <param name="publishMethod">The publish method.</param> /// <param name="options">The options.</param> /// <param name="publishToOriginator">if set to <c>true</c> [publish to originator].</param> /// <param name="mergeAction">The merge action.</param> /// <returns></returns> private Task <long> Publish_Impl(Func <T, PublishOptions, bool, Task <long> > callback, T message, PublishMethod publishMethod, PublishOptions options = PublishOptions.None, bool publishToOriginator = false, Action <T, T> mergeAction = null) { if (Equals(message, default(T))) { return(null); } if (Suppression.IsActive( )) { return(null); } DeferredChannelMessageContext deferredMessageContext = null; try { ///// // If running in a delayed context, use the current message store. ///// if (publishMethod == PublishMethod.Deferred) { deferredMessageContext = DeferredChannelMessageContext.GetContext( ); if (deferredMessageContext.ContextType != ContextType.Attached && !DeferredChannelMessageContext.SuppressNoContextWarning) { EventLog.Application.WriteWarning("Channel {0} has been created with PublishMethod.Deferred with no DeferredChannelMessageContext set. Messages will be sent immediately. Either set a DeferredChannelMessageContext or publish the message with PublishMethod.Immediate.", ChannelName); } } if (deferredMessageContext != null && deferredMessageContext.ContextType == ContextType.Attached) { deferredMessageContext.AddOrUpdateMessage(ChannelName, message, mergeAction); } else { return(callback(message, options, publishToOriginator)); } } finally { if (deferredMessageContext != null) { deferredMessageContext.Dispose( ); } } return(null); }
public void Test_DetachedContext() { TestMessage msg; TestMessage msgDetached = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { using (DeferredChannelMessageContext detachedContext = new DeferredChannelMessageContext(ContextType.Detached)) { detachedContext.AddOrUpdateMessage("Detached", msgDetached, null); Assert.That(detachedContext.TryGetMessage("Detached", out msg), Is.True, "Detached context missing msg"); Assert.That(outerContext.TryGetMessage("Detached", out msg), Is.False, "Outer context contains msg"); } } }
public void TestEntityFieldCacheBulkRemove( ) { using (var domain = new TestAppDomain( )) { Func <MessageEventArgs <EntityFieldCacheMessage>, bool> predicate = m => m.Message.RemoveKeys.Any(rk => rk.Id == new EntityRef("core:resource").Id); var instance = domain.InjectType <SubscriberRefObject <EntityFieldCacheMessage>, EntityFieldCacheMessage>(EntityFieldCache.CacheName, RunAsDefaultTenant.DefaultTenantName, predicate); var entityRef = new EntityRef("core:resource"); var fieldRef = new EntityRef("core:name"); ///// // Get an entity in the primary domain. ///// Entity.Get(entityRef, fieldRef); MessageEventArgs <EntityFieldCacheMessage> message; using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { using (Entity.DistributedMemoryManager.GetChannel <EntityFieldCacheMessage>(EntityFieldCache.CacheName)) { ///// // Clear the cache from the primary domain. ///// EntityFieldCache.Instance.Remove(entityRef.Id); Thread.Sleep(DefaultTimeout); message = instance.ReceivedMessages.FirstOrDefault(predicate); Assert.IsNull(message, "No message should have been received."); } } bool waitOne = instance.MessageReceived.WaitOne(DefaultTimeout); Assert.IsTrue(waitOne, "No message received in " + DefaultTimeout + "ms."); Assert.GreaterOrEqual(instance.ReceivedMessages.Count, 1, "Received message count is invalid."); message = instance.ReceivedMessages.FirstOrDefault(predicate); Assert.IsNotNull(message, "Received message is invalid"); } }
public void Test_GetContext() { Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "Set initially"); using (DeferredChannelMessageContext newContext = new DeferredChannelMessageContext()) { using (DeferredChannelMessageContext attachedContext = DeferredChannelMessageContext.GetContext()) { Assert.That(newContext.ContextType, Is.EqualTo(ContextType.New), "Incorrect context type"); Assert.That(attachedContext.ContextType, Is.EqualTo(ContextType.Attached), "Incorrect context type"); } Assert.That(DeferredChannelMessageContext.IsSet(), Is.True, "Attached Dispose() removed context"); } Assert.That(DeferredChannelMessageContext.IsSet(), Is.False, "New Dispose() did not remove context"); }
private void ProcessTask(BackgroundTask task) { HandleTask(task, handler => { using (EntryPointContext.SetEntryPoint("BackgroundTask")) { ProcessMonitorWriter.Instance.Write("BackgroundTask"); using (DeferredChannelMessageContext deferredMsgContext = new DeferredChannelMessageContext()) { var contextData = task.Context; using (CustomContext.SetContext(contextData)) { handler.HandleTask(task); } } } }); }
public void Test_AttachedContext() { TestMessage msg; TestMessage msgAttached = new TestMessage(); using (DeferredChannelMessageContext outerContext = new DeferredChannelMessageContext()) { using (DeferredChannelMessageContext attachedContext = new DeferredChannelMessageContext(ContextType.Attached)) { attachedContext.AddOrUpdateMessage("Attached", msgAttached, null); Assert.That(attachedContext.TryGetMessage("Attached", out msg), Is.True, "Attached context missing msg"); Assert.That(outerContext.TryGetMessage("Attached", out msg), Is.True, "Outer context missing msg"); Assert.AreSame(msg, msgAttached); } } }
public void Test_AddOrUpdateMessage_Merge_TryGetMessage_Message() { using (DeferredChannelMessageContext context = new DeferredChannelMessageContext()) { TestMessage msg = new TestMessage(); msg.Data.Add(1); context.AddOrUpdateMessage("TestChannel", msg, null); TestMessage msg2 = new TestMessage(); msg2.Data.Add(2); context.AddOrUpdateMessage("TestChannel", msg2, (e, n) => e.Data.UnionWith(n.Data)); TestMessage addedMsg; Assert.That(context.TryGetMessage("TestChannel", out addedMsg), Is.True, "TryGetMessage failed"); Assert.AreEqual(2, addedMsg.Data.Count); Assert.Contains(1, addedMsg.Data); Assert.Contains(2, addedMsg.Data); } }
public void Setup() { _deferredChannelMessageContext = new DeferredChannelMessageContext(); }
protected override void Execute(IJobExecutionContext jobContext) { var stopWatch = new Stopwatch(); perfCounters.GetPerformanceCounter <RatePerSecond32PerformanceCounter>(WorkflowPerformanceCounters.ScheduleFireRateCounterName).Increment(); stopWatch.Start(); var jobRef = ScheduledItemHelper.GetScheduledItemRef(jobContext); Diagnostics.EventLog.Application.WriteTrace($"Starting job {jobRef.Id}."); try { using (DeferredChannelMessageContext deferredMsgContext = new DeferredChannelMessageContext()) // needed to keep redis happy { // Set the context to the owner of the scheduled item. var scheduledItem = Entity.Get <ScheduledItem>(jobRef); if (scheduledItem == null) { Diagnostics.EventLog.Application.WriteTrace($"Attempted to start a job which references a nonexistant item. Ignoring. JobContext: {jobContext.ToString()}"); return; } if (RunAsOwner) { var owner = scheduledItem.SecurityOwner; if (owner == null) { var message = $"Unable to start scheduled job as import configuration has no owner"; Diagnostics.EventLog.Application.WriteError($"StartImportJob.Execute: {message}. {scheduledItem.Id}"); throw GenerateJobException(message, scheduledItem); } var identityInfo = new IdentityInfo(owner.Id, owner.Name); var contextData = new RequestContextData(RequestContext.GetContext()); contextData.Identity = identityInfo; using (CustomContext.SetContext(contextData)) { Execute(jobRef); } } else { Execute(jobRef); } } } catch (JobExecutionException ex) { EDC.ReadiNow.Diagnostics.EventLog.Application.WriteTrace("Job execution exception. Ex: {0}", ex.ToString()); throw; // The job has already handled the problem and taken action. } catch (PlatformSecurityException ex) { Diagnostics.EventLog.Application.WriteError($"Platform security exception thrown to scheduler. This should never occur. Ex: {ex}"); } catch (Exception ex) { Diagnostics.EventLog.Application.WriteError("Exception thrown to scheduler. This should never occur and should be handled by the scheduled item. Ex: {0}", ex.ToString()); } stopWatch.Stop(); perfCounters.GetPerformanceCounter <AverageTimer32PerformanceCounter>(WorkflowPerformanceCounters.ScheduleJobDurationCounterName).AddTiming(stopWatch); }