Skip to content

Instantly share code, notes, and snippets.

View StephenCleary's full-sized avatar

Stephen Cleary StephenCleary

View GitHub Profile
@StephenCleary
StephenCleary / JsonCreationConverter.cs
Created September 6, 2017 01:16
A JsonConverter that determines the type of the deserialized object by its JSON representation. Combination of several approaches including CustomCreatorConverter and https://stackoverflow.com/questions/22537233/json-net-how-to-deserialize-interface-property-based-on-parent-holder-object/22539730#22539730
/// <summary>
/// Creates a custom object based on the json values.
/// </summary>
/// <typeparam name="T">The object type to convert.</typeparam>
public abstract class JsonCreationConverter<T> : JsonConverter
{
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.Null)
return null;
@StephenCleary
StephenCleary / coordination.ts
Created August 18, 2017 04:48
AsyncEx... for TypeScript
class Future<T> {
private resolver: (value: T) => void; // readonly
private rejecter: (error: Error) => void; // readonly
private _isCompleted: boolean = false;
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolver = resolve;
this.rejecter = reject;
});
@StephenCleary
StephenCleary / AnonymousDisposable.cs
Created March 24, 2017 13:13
LockAsync abstraction around SemaphoreSlim
using System;
using System.Threading;
/// <summary>
/// A base class for disposables that need exactly-once semantics in a threadsafe way. All disposals of this instance block until the disposal is complete.
/// </summary>
/// <remarks>
/// <para>If <see cref="Dispose()"/> is called multiple times, only the first call will execute the disposal code. Other calls to <see cref="Dispose()"/> will wait for the disposal to complete.</para>
/// </remarks>
public sealed class AnonymousDisposable : IDisposable
@StephenCleary
StephenCleary / async.cs
Last active February 9, 2017 11:54 — forked from davidfowl/async.cs
Async quiz: How many threads are used? What does it print?
using System;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
// The more evil version. :)
class Program
{
static void Main(string[] args)
{
internal class AwaitableMessages
{
public static Task<T> NextMessageAsync<T>()
{
var tcs = new TaskCompletionSource<T>();
Messenger.Default.Register<T>(null, item => tcs.TrySetResult(item));
return tcs.Task;
}
}
@StephenCleary
StephenCleary / UnitTests.cs
Created March 3, 2015 01:23
How Task.Run responds to CancellationToken
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class TaskRunCancellationTokenUnitTests
{
[TestMethod]
public void CancellationTokenPassedToSynchronousTaskRun_CancelsTaskWithTaskCanceledException()
@StephenCleary
StephenCleary / UnitTests.cs
Last active August 29, 2015 14:16
Wait/Result can expect OperationCanceledException as its AggregateException.InnerException, regardless of how the cancellation was done.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class WaitCanTreatInnerExceptionAsOperationCanceledException
{
[TestMethod]
public void CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException()
@StephenCleary
StephenCleary / UnitTests.cs
Last active April 11, 2023 19:59
Await can just catch OperationCanceledException, regardless of how the cancellation was done.
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AwaitCanJustCatchOperationCanceledException
{
[TestMethod]
public async Task CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException()
// I'm starting 10 calls at every 50 msec. Each call has a random execution time between 1-19 seconds.
// Many times the last answer comes from the 9th call instead of the 10th.
// I'm not sure I'm testing it properly.
void Main()
{
var rand = new Random();
Enumerable.Range(1,10)
@StephenCleary
StephenCleary / UnitTests.cs
Last active February 4, 2024 12:29
How StartNew responds to CancellationToken
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class StartNewCancellationTokenUnitTests
{
[TestMethod]
public void CancellationTokenPassedToStartNew_CancelsTaskWithTaskCanceledException()