Skip to content

Instantly share code, notes, and snippets.

@riezebosch
Created February 5, 2019 09:39
Show Gist options
  • Save riezebosch/7b8663fa82af52ba8b0a71e5f08179eb to your computer and use it in GitHub Desktop.
Save riezebosch/7b8663fa82af52ba8b0a71e5f08179eb to your computer and use it in GitHub Desktop.
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