Skip to content

Instantly share code, notes, and snippets.

@JustinPealing
JustinPealing / AwaitableObjectPool.cs
Created May 4, 2018 18:44
Awaitable pool of objects of type T
public class ObjectPool<T>
{
private readonly Queue<T> _items = new Queue<T>();
private readonly Queue<TaskCompletionSource<T>> _waits = new Queue<TaskCompletionSource<T>>();
public void Add(T value)
{
lock (_waits)
{
if (_waits.TryDequeue(out var wait))
@JustinPealing
JustinPealing / FizzBuzz.xslt
Last active September 30, 2016 10:02
FizzBuzz in XSLT - run against any XML file
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:call-template name="FizzBuzz">
<xsl:with-param name="i" select="1" />
</xsl:call-template>
</xsl:template>
<xsl:template name="FizzBuzz">
@JustinPealing
JustinPealing / FakeDbSet.cs
Created September 7, 2015 21:49
Fake implementation of DbSet<T> for unit testing
public class FakeDbSet<T> : DbSet<T>, IQueryable
where T : class
{
private readonly Func<T, object[], bool> _find;
readonly ObservableCollection<T> _items;
private IQueryable<T> _query;
public FakeDbSet(Func<T, object[], bool> find = null)
{
_find = find;
@JustinPealing
JustinPealing / ExceptionAssert.cs
Created August 13, 2015 23:38
Assert on thrown exceptions
public static class ExceptionAssert
{
public static T Throws<T>(Action action)
where T : Exception
{
try
{
action();
}
catch (T ex)
@JustinPealing
JustinPealing / Logging.cs
Created August 13, 2015 23:34
Boilerplate Console Application Logging with log4net
using log4net;
using log4net.Appender;
using log4net.Core;
using log4net.Layout;
using log4net.Repository.Hierarchy;
public static class Logging
{
private const string ConversionPattern = "%date{HH:mm:ss,fff} %-5level %-15logger{1} %message%newline";
private static readonly PatternLayout PatternLayout;