Skip to content

Commit 2aadaba

Browse files
committed
add watch testcases
1 parent b5b95df commit 2aadaba

File tree

4 files changed

+315
-25
lines changed

4 files changed

+315
-25
lines changed

src/Watcher.cs

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@ public enum WatchEventType
2222

2323
public class Watcher<T> : IDisposable
2424
{
25-
private readonly StreamReader _streamReader;
25+
public bool Watching { get; private set; }
26+
2627
private readonly CancellationTokenSource _cts;
28+
private readonly StreamReader _streamReader;
2729

2830
public Watcher(StreamReader streamReader, Action<WatchEventType, T> onEvent, Action<Exception> onError)
2931
{
@@ -37,25 +39,40 @@ public Watcher(StreamReader streamReader, Action<WatchEventType, T> onEvent, Act
3739

3840
Task.Run(async () =>
3941
{
40-
while (!streamReader.EndOfStream)
42+
try
4143
{
42-
if (token.IsCancellationRequested)
43-
{
44-
return;
45-
}
44+
Watching = true;
4645

47-
try
46+
while (!streamReader.EndOfStream)
4847
{
48+
if (token.IsCancellationRequested)
49+
{
50+
return;
51+
}
52+
4953
var line = await streamReader.ReadLineAsync();
50-
var @event = SafeJsonConvert.DeserializeObject<WatchEvent>(line);
5154

52-
OnEvent?.Invoke(@event.Type, @event.Object);
53-
}
54-
catch (Exception e)
55-
{
56-
OnError?.Invoke(e);
55+
try
56+
{
57+
var @event = SafeJsonConvert.DeserializeObject<WatchEvent>(line);
58+
OnEvent?.Invoke(@event.Type, @event.Object);
59+
}
60+
catch (Exception e)
61+
{
62+
// error if deserialized failed or onevent throws
63+
OnError?.Invoke(e);
64+
}
5765
}
5866
}
67+
catch (Exception e)
68+
{
69+
// error when transport error, IOException ect
70+
OnError?.Invoke(e);
71+
}
72+
finally
73+
{
74+
Watching = false;
75+
}
5976
}, token);
6077
}
6178

tests/AuthTests.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Net;
55
using System.Net.Http.Headers;
66
using System.Security.Cryptography.X509Certificates;
7+
using System.Threading.Tasks;
78
using k8s.Models;
89
using k8s.Tests.Mock;
910
using Microsoft.AspNetCore.Hosting;
@@ -39,7 +40,7 @@ public void TestAnonymous()
3940
using (var server = new MockKubeApiServer(cxt =>
4041
{
4142
cxt.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
42-
return false;
43+
return Task.FromResult(false);
4344
}))
4445
{
4546
var client = new Kubernetes(new KubernetesClientConfiguration
@@ -69,10 +70,10 @@ public void TestBasicAuth()
6970
if (header != expect)
7071
{
7172
cxt.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
72-
return false;
73+
return Task.FromResult(false);
7374
}
7475

75-
return true;
76+
return Task.FromResult(true);
7677
}))
7778
{
7879
{
@@ -256,10 +257,10 @@ public void TestToken()
256257
if (header != expect)
257258
{
258259
cxt.Response.StatusCode = (int) HttpStatusCode.Unauthorized;
259-
return false;
260+
return Task.FromResult(false);
260261
}
261262

262-
return true;
263+
return Task.FromResult(true);
263264
}))
264265
{
265266
{

tests/Mock/MockKubeApiServer.cs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,19 @@ public class MockKubeApiServer : IDisposable
2020

2121
private readonly IWebHost _webHost;
2222

23-
public MockKubeApiServer(Func<HttpContext, bool> shouldNext = null, Action<ListenOptions> listenConfigure = null,
23+
public MockKubeApiServer(Func<HttpContext, Task<bool>> shouldNext = null, Action<ListenOptions> listenConfigure = null,
2424
string resp = MockPodResponse)
2525
{
26-
shouldNext = shouldNext ?? (_ => true);
26+
shouldNext = shouldNext ?? (_ => Task.FromResult(true));
2727
listenConfigure = listenConfigure ?? (_ => { });
2828

2929
_webHost = WebHost.CreateDefaultBuilder()
30-
.Configure(app => app.Run(httpContext =>
30+
.Configure(app => app.Run(async httpContext =>
3131
{
32-
if (shouldNext(httpContext))
32+
if (await shouldNext(httpContext))
3333
{
34-
httpContext.Response.WriteAsync(resp);
34+
await httpContext.Response.WriteAsync(resp);
3535
}
36-
37-
return Task.Delay(0);
3836
}))
3937
.UseKestrel(options => { options.Listen(IPAddress.Loopback, 0, listenConfigure); })
4038
.Build();

0 commit comments

Comments
 (0)