Skip to content

Instantly share code, notes, and snippets.

@riezebosch riezebosch/test.cs
Created Feb 5, 2019

Embed
What would you like to do?
Polly Bulkhead
using System;
using System.Diagnostics;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Polly;
using Polly.Bulkhead;
using Xunit;
namespace test
{
public class UnitTest1
{
interface IVstsRestClient
{
T Get<T>(T input);
}
private class VstsRestClient : IVstsRestClient
{
public T Get<T>(T input)
{
Thread.Sleep(1000);
return input;
}
}
[Fact]
public void ExecuteReturnsResult()
{
var client = new VstsRestClient();
var bulk = Policy.Bulkhead(2);
var result = bulk.Execute(() => client.Get(4));
Assert.Equal(4, result);
}
[Fact]
public void StayingUnderLimits()
{
var client = new VstsRestClient();
var bulk = Policy.Bulkhead(2);
var sw = Stopwatch.StartNew();
bulk.Execute(() => client.Get(3));
bulk.Execute(() => client.Get(4));
Assert.True(sw.Elapsed > TimeSpan.FromSeconds(2));
}
[Fact]
public void CrossingTheBorder()
{
var client = new VstsRestClient();
var bulk = Policy.Bulkhead(2);
var sw = Stopwatch.StartNew();
bulk.Execute(() => client.Get(3));
bulk.Execute(() => client.Get(4));
bulk.Execute(() => client.Get(5));
Assert.True(sw.Elapsed > TimeSpan.FromSeconds(2));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.