Skip to content

Instantly share code, notes, and snippets.

Avatar

Stephen Cleary StephenCleary

View GitHub Profile
@StephenCleary
StephenCleary / UnitTests.cs
Created Mar 3, 2015
How Task.Run responds to CancellationToken
View UnitTests.cs
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 Aug 29, 2015
Wait/Result can expect OperationCanceledException as its AggregateException.InnerException, regardless of how the cancellation was done.
View UnitTests.cs
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 Mar 4, 2020
Await can just catch OperationCanceledException, regardless of how the cancellation was done.
View UnitTests.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class AwaitCanJustCatchOperationCanceledException
{
[TestMethod]
public async Task CancellationTokenPassedToStartNew_CanTreatAsOperationCanceledException()
View Async
// 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 Aug 3, 2016
How StartNew responds to CancellationToken
View UnitTests.cs
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class StartNewCancellationTokenUnitTests
{
[TestMethod]
public void CancellationTokenPassedToStartNew_CancelsTaskWithTaskCanceledException()
View Program.cs
static void Main(string[] args)
{
int threadId = Task.Run(() =>
{
CallContext.LogicalSetData("k", 0);
Console.WriteLine("Write logical data on thread " + Thread.CurrentThread.ManagedThreadId);
return Thread.CurrentThread.ManagedThreadId;
}).Result;
while (true)
@StephenCleary
StephenCleary / ObjectWithPool.cs
Created Sep 28, 2014
Singly-linked list object pool with the list members embedded in the contained object.
View ObjectWithPool.cs
public sealed class MyObject : IDisposable
{
/// <summary>
/// Used by the pool.
/// </summary>
private MyObject _next;
// TODO: Add other members here.
public static MyObject Create()
View ObservableProgress.cs
using System;
using System.Reactive.Linq;
using System.Threading;
/// <summary>
/// Helper methods for using observable <see cref="IProgress{T}"/> implementations. These are hot observables.
/// </summary>
public static class ObservableProgress
{
/// <summary>
@StephenCleary
StephenCleary / LinkedDic.cs
Last active Dec 31, 2015 — forked from ayende/LinkedDic.cs
Added tests with Builder, SortedImmutableDictionary, and LinkedDictionaryUnoptimized.
View LinkedDic.cs
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Voron.Util
{
public class LinkedDictionary<TKey, TValue> : IEnumerable<KeyValuePair<TKey, TValue>>, IReadOnlyDictionary<TKey, TValue>
where TValue : class, new()
{
private readonly TValue _deleteMarker = new TValue();
@StephenCleary
StephenCleary / Program.cs
Last active Dec 30, 2015
Serialization size test for Azure caching. Requires NuGet packages: Comparers, Newtonsoft.Json
View Program.cs
using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using Comparers;
You can’t perform that action at this time.