Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions reverse-proxy.sln
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29519.161
# Visual Studio Version 17
VisualStudioVersion = 17.0.31804.322
MinimumVisualStudioVersion = 10.0.40219.1
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{3E275568-3767-43AC-B51B-FA75C0B5CE91}"
ProjectSection(SolutionItems) = preProject
Expand Down Expand Up @@ -104,6 +104,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "backend", "samples\Kuberene
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ReverseProxy.Minimal.Sample", "samples\ReverseProxy.Minimal.Sample\ReverseProxy.Minimal.Sample.csproj", "{AA34BE13-7193-4036-A886-A7EE6CD36940}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Kubernetes.UnitTests", "src\OperatorFramework\test\UnitTests\Microsoft.Kubernetes.UnitTests.csproj", "{D6599484-5A3F-471C-87B0-7014C56F14CE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -408,6 +410,14 @@ Global
{AA34BE13-7193-4036-A886-A7EE6CD36940}.Release|Any CPU.Build.0 = Release|Any CPU
{AA34BE13-7193-4036-A886-A7EE6CD36940}.Release|x64.ActiveCfg = Release|Any CPU
{AA34BE13-7193-4036-A886-A7EE6CD36940}.Release|x64.Build.0 = Release|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Debug|x64.ActiveCfg = Debug|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Debug|x64.Build.0 = Debug|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Release|Any CPU.Build.0 = Release|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Release|x64.ActiveCfg = Release|Any CPU
{D6599484-5A3F-471C-87B0-7014C56F14CE}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -455,6 +465,7 @@ Global
{84B920E2-A501-457D-8F1A-9CC7AF8B5F2D} = {04E87669-7E7B-4217-83A4-EF80B534D14B}
{EB5663B9-31BA-4930-A302-4B87B8DEB74A} = {32B6B967-6EAF-42A1-9FD6-A59F752FF76B}
{AA34BE13-7193-4036-A886-A7EE6CD36940} = {149C61A2-D9F8-49B9-9F9B-3C953FEF53AA}
{D6599484-5A3F-471C-87B0-7014C56F14CE} = {E96BB4D7-EECC-4A78-BC7D-E167663FD6F2}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {31F6924A-E427-4830-96E9-B47CEB7BFE78}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,18 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Kubernetes.Controller.Hosting.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Kubernetes.Controller.Hosting
{

[TestClass]
public class BackgroundHostedServiceTests
{
[TestMethod]
[Fact]
public async Task StartAndStopUnderHosting()
{
// arrange
var latches = new TestLatches();

using var host = new HostBuilder()
Expand All @@ -32,21 +28,17 @@ public async Task StartAndStopUnderHosting()
})
.Build();

// act
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));
await host.StartAsync(cts.Token).ConfigureAwait(false);
await latches.RunEnter.WhenSignalAsync(cts.Token).ConfigureAwait(false);
latches.RunResult.Signal();
await latches.RunExit.WhenSignalAsync(cts.Token).ConfigureAwait(false);
await host.StopAsync(cts.Token).ConfigureAwait(false);

// assert
}

[TestMethod]
[Fact]
public async Task StartAndStopUnderWebHost()
{
// arrange
var latches = new TestLatches();

using var host = new WebHostBuilder()
Expand All @@ -59,7 +51,6 @@ public async Task StartAndStopUnderWebHost()
.Configure(app => { })
.Build();

// act
// TODO: figure out why the hosting takes so long to unwind naturally
// and increase this safety cancellation up from 3 seconds
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(3));
Expand All @@ -71,15 +62,12 @@ public async Task StartAndStopUnderWebHost()
await latches.RunExit.WhenSignalAsync(cts.Token).ConfigureAwait(false);

await runTask.ConfigureAwait(false);

// assert
}


[TestMethod]
[Fact]
public async Task IfRunAsyncThrowsItComesBackFromHost()
{
// arrange
var context = new TestLatches();

using var host = new WebHostBuilder()
Expand All @@ -92,7 +80,6 @@ public async Task IfRunAsyncThrowsItComesBackFromHost()
.Configure(app => { })
.Build();

// act
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(15));

var runTask = host.RunAsync(cts.Token);
Expand All @@ -101,10 +88,9 @@ public async Task IfRunAsyncThrowsItComesBackFromHost()
context.RunResult.Throw(new ApplicationException("Unwind"));
#pragma warning restore CA1303 // Do not pass literals as localized parameters

var ex = await Should.ThrowAsync<AggregateException>(runTask).ConfigureAwait(false);
var ex = await Assert.ThrowsAsync<AggregateException>(() => runTask);

// assert
ex.Flatten().InnerExceptions.ShouldHaveSingleItem().Message.ShouldBe("Unwind");
Assert.Equal("Unwind", Assert.Single(ex.Flatten().InnerExceptions).Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,20 @@
using Microsoft.Extensions.Hosting;
using Microsoft.Kubernetes.Testing;
using Microsoft.Kubernetes.Utils;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Kubernetes.Controller.Informers
{
[TestClass]
public class ResourceInformerTests
{
[TestMethod]
[Fact]
public async Task ResourcesAreListedWhenReadyAsyncIsComplete()
{
// arrange
using var cancellation = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(5));

var testYaml = TestYaml.LoadFromEmbeddedStream<(V1Pod[] pods, NamespacedName[] shouldBe)>();
Expand Down Expand Up @@ -51,20 +48,17 @@ public async Task ResourcesAreListedWhenReadyAsyncIsComplete()
pods[NamespacedName.From(pod)] = pod;
});

// act
await clusterHost.StartAsync(cancellation.Token);
await testHost.StartAsync(cancellation.Token);

await registration.ReadyAsync(cancellation.Token);

// assert
pods.Keys.ShouldBe(testYaml.shouldBe);
Assert.Equal(testYaml.shouldBe, pods.Keys);
}

[TestMethod]
[Fact]
public async Task ResourcesWithApiGroupAreListed()
{
// arrange
using var cancellation = new CancellationTokenSource(Debugger.IsAttached ? TimeSpan.FromMinutes(5) : TimeSpan.FromSeconds(5));

var testYaml = TestYaml.LoadFromEmbeddedStream<(V1Deployment[] deployments, NamespacedName[] shouldBe)>();
Expand Down Expand Up @@ -92,14 +86,12 @@ public async Task ResourcesWithApiGroupAreListed()
deployments[NamespacedName.From(deployment)] = deployment;
});

// act
await clusterHost.StartAsync(cancellation.Token);
await testHost.StartAsync(cancellation.Token);

await registration.ReadyAsync(cancellation.Token);

// assert
deployments.Keys.ShouldBe(testYaml.shouldBe);
Assert.Equal(testYaml.shouldBe, deployments.Keys);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@
// Licensed under the MIT License.

using Microsoft.Kubernetes.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Xunit;

namespace Microsoft.Kubernetes.Controller.Queues
{
[TestClass]
public class DelayingQueueTests
{
[TestMethod]
[Fact]
public void DelayingQueuePassesCallsThrough()
{
// arrange
var added = new List<string>();
var doned = new List<string>();
var fake = new FakeQueue<string>
Expand All @@ -28,21 +25,18 @@ public void DelayingQueuePassesCallsThrough()
var clock = new FakeSystemClock();
IDelayingQueue<string> delayingQueue = new DelayingQueue<string>(clock, fake);

// act
delayingQueue.Add("one");
delayingQueue.Done("two");
var len = delayingQueue.Len();

// assert
added.ShouldHaveSingleItem().ShouldBe("one");
doned.ShouldHaveSingleItem().ShouldBe("two");
len.ShouldBe(42);
Assert.Equal("one", Assert.Single(added));
Assert.Equal("two", Assert.Single(doned));
Assert.Equal(42, len);
}

[TestMethod]
[Fact(Skip = "Flaky test")]
public async Task DelayingQueueAddsWhenTimePasses()
{
// arrange
var added = new List<string>();
var fake = new FakeQueue<string>
{
Expand All @@ -51,7 +45,6 @@ public async Task DelayingQueueAddsWhenTimePasses()
var clock = new FakeSystemClock();
IDelayingQueue<string> delayingQueue = new DelayingQueue<string>(clock, fake);

// act
delayingQueue.AddAfter("50ms", TimeSpan.FromMilliseconds(50));
delayingQueue.AddAfter("100ms", TimeSpan.FromMilliseconds(100));
clock.Advance(TimeSpan.FromMilliseconds(25));
Expand All @@ -73,19 +66,17 @@ public async Task DelayingQueueAddsWhenTimePasses()
await Task.Delay(TimeSpan.FromMilliseconds(40));
var countAfter135ms = added.Count;

// assert
countAfter25ms.ShouldBe(0);
countAfter55ms.ShouldBe(1);
countAfter80ms.ShouldBe(2);
countAfter105ms.ShouldBe(3);
countAfter135ms.ShouldBe(4);
added.ShouldBe(new[] { "50ms", "75ms", "100ms", "125ms" }, ignoreOrder: false);
Assert.Equal(0, countAfter25ms);
Assert.Equal(1, countAfter55ms);
Assert.Equal(2, countAfter80ms);
Assert.Equal(3, countAfter105ms);
Assert.Equal(4, countAfter135ms);
Assert.Equal(new[] { "50ms", "75ms", "100ms", "125ms" }, added);
}

[TestMethod]
[Fact]
public async Task ZeroDelayAddsInline()
{
// arrange
var state = "setup";
var added = new List<(string state, string item)>();
var fake = new FakeQueue<string>
Expand All @@ -95,7 +86,6 @@ public async Task ZeroDelayAddsInline()
var clock = new FakeSystemClock();
IDelayingQueue<string> delayingQueue = new DelayingQueue<string>(clock, fake);

// act
state = "before-one";
delayingQueue.AddAfter("one", TimeSpan.FromMilliseconds(1));
state = "after-one";
Expand All @@ -111,14 +101,12 @@ public async Task ZeroDelayAddsInline()
state = "after-three";
await Task.Delay(TimeSpan.FromMilliseconds(40));

// assert
added.ShouldHaveSingleItem().ShouldBe(("before-two", "two"));
Assert.Equal(("before-two", "two"), Assert.Single(added));
}

[TestMethod]
[Fact(Skip = "Flaky test")]
public async Task NoAddingAfterShutdown()
{
// arrange
var added = new List<string>();
var fake = new FakeQueue<string>
{
Expand All @@ -127,15 +115,13 @@ public async Task NoAddingAfterShutdown()
var clock = new FakeSystemClock();
IDelayingQueue<string> delayingQueue = new DelayingQueue<string>(clock, fake);

// act
delayingQueue.AddAfter("one", TimeSpan.FromMilliseconds(10));
delayingQueue.ShutDown();
delayingQueue.AddAfter("two", TimeSpan.FromMilliseconds(10));
clock.Advance(TimeSpan.FromMilliseconds(25));
await Task.Delay(TimeSpan.FromMilliseconds(40));

// assert
added.ShouldBeEmpty();
Assert.Empty(added);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@
// Licensed under the MIT License.

using Microsoft.Kubernetes.Fakes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Shouldly;
using System;
using System.Collections.Generic;
using Xunit;

namespace Microsoft.Kubernetes.Controller.Queues
{
[TestClass]
public class RateLimitingQueueTests
{
[TestMethod]
[Fact]
public void AddRateLimitedCallsWhenAndAddDelay()
{
// arrange
var whenResults = new Dictionary<string, TimeSpan>
{
{ "one", TimeSpan.FromMilliseconds(15) },
Expand All @@ -38,24 +35,23 @@ public void AddRateLimitedCallsWhenAndAddDelay()
};
var queue = new RateLimitingQueue<string>(rateLimiter, @base);

// act
queue.AddRateLimited("one");
queue.AddRateLimited("two");
queue.AddRateLimited("three");

// assert
whenCalls.ShouldBe(new[]
Assert.Equal(new[]
{
"one",
"two",
"three"
});
addAfterCalls.ShouldBe(new[]
}, whenCalls);

Assert.Equal(new[]
{
("one", TimeSpan.FromMilliseconds(15)),
("two", TimeSpan.FromMilliseconds(0)),
("three", TimeSpan.FromMilliseconds(30)),
});
("three", TimeSpan.FromMilliseconds(30))
}, addAfterCalls);
}
}
}
Loading