-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose middleware properties in activity/orchestrator
The properties bag from DispatchMiddlewareContext contains information that might be useful from activities/orchestrators. However, they currently do not show up from those context options. This change plumbs the dictionary through and surfaces it for both activities and orchestrators. As part of this, a new interface IContextProperties is added that just has the properties dictionary. However, this allows for shared code to set and get properties by type or by name.
- Loading branch information
1 parent
54436c6
commit dba54bd
Showing
13 changed files
with
356 additions
and
21 deletions.
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
Test/DurableTask.Core.Tests/PropertiesMiddlewareTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
#nullable enable | ||
namespace DurableTask.Core.Tests | ||
{ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Threading.Tasks; | ||
using DurableTask.Emulator; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
[TestClass] | ||
public class PropertiesMiddlewareTests | ||
{ | ||
private const string PropertyKey = "Test"; | ||
private const string PropertyValue = "Value"; | ||
|
||
TaskHubWorker worker = null!; | ||
TaskHubClient client = null!; | ||
|
||
[TestInitialize] | ||
public async Task Initialize() | ||
{ | ||
var service = new LocalOrchestrationService(); | ||
this.worker = new TaskHubWorker(service); | ||
|
||
await this.worker | ||
.AddTaskOrchestrations(typeof(NoActivities), typeof(RunActivityOrchestrator)) | ||
.AddTaskActivities(typeof(ReturnPropertyActivity)) | ||
.StartAsync(); | ||
|
||
this.client = new TaskHubClient(service); | ||
} | ||
|
||
[TestCleanup] | ||
public async Task TestCleanup() | ||
{ | ||
await this.worker.StopAsync(true); | ||
} | ||
|
||
private sealed class NoActivities : TaskOrchestration<string, string> | ||
{ | ||
public override Task<string> RunTask(OrchestrationContext context, string input) | ||
{ | ||
return Task.FromResult(context.GetProperty<string>(PropertyKey)!); | ||
} | ||
} | ||
|
||
private sealed class ReturnPropertyActivity : TaskActivity<string, string> | ||
{ | ||
protected override string Execute(TaskContext context, string input) | ||
{ | ||
return context.GetProperty<string>(PropertyKey)!; | ||
} | ||
} | ||
|
||
private sealed class RunActivityOrchestrator : TaskOrchestration<string, string> | ||
{ | ||
public override Task<string> RunTask(OrchestrationContext context, string input) | ||
{ | ||
return context.ScheduleTask<string>(typeof(ReturnPropertyActivity)); | ||
} | ||
} | ||
|
||
[TestMethod] | ||
public async Task OrchestrationGetsProperties() | ||
{ | ||
this.worker.AddOrchestrationDispatcherMiddleware((context, next) => | ||
{ | ||
context.SetProperty<string>(PropertyKey, PropertyValue); | ||
return next(); | ||
}); | ||
|
||
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(NoActivities), null); | ||
|
||
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10); | ||
var state = await this.client.WaitForOrchestrationAsync(instance, timeout); | ||
|
||
Assert.AreEqual($"\"{PropertyValue}\"", state.Output); | ||
} | ||
|
||
[TestMethod] | ||
public async Task OrchestrationDoesNotGetPropertiesFromActivityMiddleware() | ||
{ | ||
this.worker.AddActivityDispatcherMiddleware((context, next) => | ||
{ | ||
context.SetProperty<string>(PropertyKey, PropertyValue); | ||
return next(); | ||
}); | ||
|
||
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(NoActivities), null); | ||
|
||
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10); | ||
var state = await this.client.WaitForOrchestrationAsync(instance, timeout); | ||
|
||
Assert.IsNull(state.Output); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ActivityGetsProperties() | ||
{ | ||
this.worker.AddActivityDispatcherMiddleware((context, next) => | ||
{ | ||
context.SetProperty<string>(PropertyKey, PropertyValue); | ||
return next(); | ||
}); | ||
|
||
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(RunActivityOrchestrator), null); | ||
|
||
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10); | ||
var state = await this.client.WaitForOrchestrationAsync(instance, timeout); | ||
|
||
Assert.AreEqual($"\"{PropertyValue}\"", state.Output); | ||
} | ||
|
||
[TestMethod] | ||
public async Task ActivityDoesNotGetPropertiesFromOrchestratorMiddleware() | ||
{ | ||
this.worker.AddOrchestrationDispatcherMiddleware((context, next) => | ||
{ | ||
context.SetProperty<string>(PropertyKey, PropertyValue); | ||
return next(); | ||
}); | ||
|
||
OrchestrationInstance instance = await this.client.CreateOrchestrationInstanceAsync(typeof(RunActivityOrchestrator), null); | ||
|
||
TimeSpan timeout = TimeSpan.FromSeconds(Debugger.IsAttached ? 1000 : 10); | ||
var state = await this.client.WaitForOrchestrationAsync(instance, timeout); | ||
|
||
Assert.IsNull(state.Output); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
#nullable enable | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace DurableTask.Core | ||
{ | ||
/// <summary> | ||
/// Extension methods that help get properties from <see cref="IContextProperties"/>. | ||
/// </summary> | ||
public static class ContextPropertiesExtensions | ||
{ | ||
/// <summary> | ||
/// Sets a property value to the context using the full name of the type as the key. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the property.</typeparam> | ||
/// <param name="properties">Properties to set property for.</param> | ||
/// <param name="value">The value of the property.</param> | ||
public static void SetProperty<T>(this IContextProperties properties, T? value) => properties.SetProperty(typeof(T).FullName, value); | ||
|
||
/// <summary> | ||
/// Sets a named property value to the context. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the property.</typeparam> | ||
/// <param name="properties">Properties to set property for.</param> | ||
/// <param name="key">The name of the property.</param> | ||
/// <param name="value">The value of the property.</param> | ||
public static void SetProperty<T>(this IContextProperties properties, string key, T? value) | ||
{ | ||
if (value is null) | ||
{ | ||
properties.Properties.Remove(key); | ||
} | ||
else | ||
{ | ||
properties.Properties[key] = value; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a property value from the context using the full name of <typeparamref name="T"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the property.</typeparam> | ||
/// <param name="properties">Properties to get property from.</param> | ||
/// <returns>The value of the property or <c>default(T)</c> if the property is not defined.</returns> | ||
public static T? GetProperty<T>(this IContextProperties properties) => properties.GetProperty<T>(typeof(T).FullName); | ||
|
||
internal static T GetRequiredProperty<T>(this IContextProperties properties) | ||
=> properties.GetProperty<T>() ?? throw new InvalidOperationException($"Could not find property for {typeof(T).FullName}"); | ||
|
||
/// <summary> | ||
/// Gets a named property value from the context. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
/// <param name="properties">Properties to get property from.</param> | ||
/// <param name="key">The name of the property value.</param> | ||
/// <returns>The value of the property or <c>default(T)</c> if the property is not defined.</returns> | ||
public static T? GetProperty<T>(this IContextProperties properties, string key) => properties.Properties.TryGetValue(key, out object value) ? (T)value : default; | ||
|
||
/// <summary> | ||
/// Gets the tags from the current properties. | ||
/// </summary> | ||
/// <param name="properties"></param> | ||
/// <returns></returns> | ||
public static IDictionary<string, string> GetTags(this IContextProperties properties) => properties.GetRequiredProperty<OrchestrationExecutionContext>().OrchestrationTags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
using System.Collections.Generic; | ||
|
||
#nullable enable | ||
|
||
namespace DurableTask.Core | ||
{ | ||
/// <summary> | ||
/// Collection of properties for context objects to store arbitrary state. | ||
/// </summary> | ||
public interface IContextProperties | ||
{ | ||
/// <summary> | ||
/// Gets the properties of the current instance | ||
/// </summary> | ||
IDictionary<string, object> Properties { get; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// Copyright Microsoft Corporation | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// ---------------------------------------------------------------------------------- | ||
|
||
namespace DurableTask.Core | ||
{ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
internal sealed class PropertiesDictionary : Dictionary<string, object>, IContextProperties | ||
{ | ||
public PropertiesDictionary() | ||
: base(StringComparer.Ordinal) | ||
{ | ||
} | ||
|
||
IDictionary<string, object> IContextProperties.Properties => this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.