-
Notifications
You must be signed in to change notification settings - Fork 493
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pass byte[] through as payload for C2D for binary payloads (#3229)
* Pass byte[] through as payload for C2D for binary payloads * Add a test and rename other test classes to match newest names * Share task completion helper more broadly * Fix .NET version requirement
- Loading branch information
David R. Williamson
authored
Mar 31, 2023
1 parent
7a2e0c1
commit 3e65aed
Showing
21 changed files
with
198 additions
and
186 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,37 +1,31 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace Microsoft.Azure.Devices.E2ETests.helpers | ||
namespace Microsoft.Azure.Devices.E2ETests | ||
{ | ||
public class TaskCompletionSourceHelper | ||
/// <summary> | ||
/// Modern .NET supports waiting on the TaskCompletionSource with a cancellation token, but older ones | ||
/// do not. We can bind that task with a call to Task.Delay to get the same effect, though. | ||
/// </summary> | ||
internal static class TaskCompletionSourceHelper | ||
{ | ||
/// <summary> | ||
/// Gets the result of the provided task completion source or throws OperationCancelledException if the provided | ||
/// cancellation token is cancelled beforehand. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the result of the task completion source.</typeparam> | ||
/// <param name="taskCompletionSource">The task completion source to asynchronously wait for the result of.</param> | ||
/// <param name="cancellationToken">The cancellation token.</param> | ||
/// <returns>The result of the provided task completion source if it completes before the provided cancellation token is cancelled.</returns> | ||
/// <exception cref="OperationCanceledException">If the cancellation token is cancelled before the provided task completion source finishes.</exception> | ||
public static async Task<T> GetTaskCompletionSourceResultAsync<T>(TaskCompletionSource<T> taskCompletionSource, CancellationToken cancellationToken) | ||
internal static async Task<T> WaitAsync<T>(this TaskCompletionSource<T> taskCompletionSource, CancellationToken ct) | ||
{ | ||
// Note that Task.Delay(-1, cancellationToken) effectively waits until the cancellation token is cancelled. The -1 value | ||
// just means that the task is allowed to run indefinitely. | ||
Task finishedTask = await Task.WhenAny(taskCompletionSource.Task, Task.Delay(-1, cancellationToken)).ConfigureAwait(false); | ||
#if NET6_0_OR_GREATER | ||
return await taskCompletionSource.Task.WaitAsync(ct).ConfigureAwait(false); | ||
#else | ||
await Task | ||
.WhenAny( | ||
taskCompletionSource.Task, | ||
Task.Delay(-1, ct)) | ||
.ConfigureAwait(false); | ||
|
||
// If the finished task is not the cancellation token | ||
if (finishedTask is Task<T>) | ||
{ | ||
return await ((Task<T>)finishedTask).ConfigureAwait(false); | ||
} | ||
|
||
// Otherwise throw operation cancelled exception since the cancellation token was cancelled before the task finished. | ||
throw new OperationCanceledException(); | ||
ct.ThrowIfCancellationRequested(); | ||
return await taskCompletionSource.Task.ConfigureAwait(false); | ||
#endif | ||
} | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.