Skip to content
This repository was archived by the owner on Sep 5, 2019. It is now read-only.

Commit 5f8cb91

Browse files
authored
Improved Code Coverage (#72)
1 parent 4cf4fbc commit 5f8cb91

File tree

6 files changed

+299
-62
lines changed

6 files changed

+299
-62
lines changed

src/Core/DiagnosticEvents.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Diagnostics;
4+
using System.Threading.Tasks;
45

56
namespace GreenDonut
67
{
@@ -31,9 +32,9 @@ public static void ReceivedBatchError<TKey>(
3132
keys
3233
};
3334

34-
if (_source.IsEnabled(_errorEventName, payload))
35+
if (_source.IsEnabled(_batchErrorEventName, payload))
3536
{
36-
_source.Write(_errorEventName, payload);
37+
_source.Write(_batchErrorEventName, payload);
3738
}
3839
}
3940

@@ -47,16 +48,16 @@ public static void ReceivedError<TKey>(
4748
key
4849
};
4950

50-
if (_source.IsEnabled(_batchErrorEventName, payload))
51+
if (_source.IsEnabled(_errorEventName, payload))
5152
{
52-
_source.Write(_batchErrorEventName, payload);
53+
_source.Write(_errorEventName, payload);
5354
}
5455
}
5556

5657
public static void ReceivedValueFromCache<TKey, TValue>(
5758
TKey key,
5859
object cacheKey,
59-
TValue value)
60+
Task<TValue> value)
6061
{
6162
var payload = new
6263
{

src/DiagnosticSource.Tests/DiagnosticEventsTests.cs

Lines changed: 89 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,114 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Diagnostics;
4+
using System.Threading;
35
using System.Threading.Tasks;
6+
using ChilliCream.Testing;
47
using Xunit;
58

69
namespace GreenDonut
710
{
811
public class DiagnosticEventsTests
912
{
10-
[Fact(DisplayName = "ExecuteBatchRequest: Should record a batch request plus error")]
11-
public async Task ExecuteBatchRequest()
13+
private readonly Dictionary<string, Result<string>> _results =
14+
new Dictionary<string, Result<string>>
15+
{
16+
{ "Foo", "Qux" },
17+
{ "Bar", "Quux" },
18+
{ "Baz", new Exception("Error: Corge") }
19+
};
20+
21+
[Fact(DisplayName = "VerifyEvents: Should match snapshot")]
22+
public async Task VerifyEvents()
1223
{
1324
var listener = new TestListener();
1425
var observer = new TestObserver(listener);
1526

1627
using (DiagnosticListener.AllListeners.Subscribe(observer))
1728
{
1829
// arrange
19-
FetchDataDelegate<string, string> fetch =
20-
async (keys, cancellationToken) =>
21-
{
22-
var error = new Exception("Quux");
23-
24-
return await Task.FromResult(new Result<string>[]
25-
{
26-
error
27-
}).ConfigureAwait(false);
28-
};
29-
var options = new DataLoaderOptions<string>
30+
var batchOptions = new DataLoaderOptions<string>
3031
{
31-
AutoDispatching = true
32+
AutoDispatching = true,
33+
Batching = true,
34+
BatchRequestDelay = TimeSpan.FromMilliseconds(150),
35+
Caching = true
3236
};
33-
var loader = new DataLoader<string, string>(options, fetch);
37+
var batchLoader = new DataLoader<string, string>(
38+
batchOptions,
39+
FetchDataAsync);
40+
var batchErrorLoader = new DataLoader<string, string>(
41+
batchOptions,
42+
(keys, canncellationToken) =>
43+
throw new Exception("BatchError: Foo"));
44+
var singleOptions = new DataLoaderOptions<string>
45+
{
46+
AutoDispatching = true,
47+
Batching = false,
48+
Caching = true
49+
};
50+
var singleLoader = new DataLoader<string, string>(
51+
singleOptions,
52+
FetchDataAsync);
3453

3554
// act
36-
try
37-
{
38-
await loader.LoadAsync("Foo").ConfigureAwait(false);
39-
}
40-
catch
41-
{
42-
}
55+
await Catch(() => batchLoader.LoadAsync("Foo"))
56+
.ConfigureAwait(false);
57+
await Task.Delay(400).ConfigureAwait(false);
58+
await Catch(() => batchLoader.LoadAsync("Foo", "Bar"))
59+
.ConfigureAwait(false);
60+
await Task.Delay(400).ConfigureAwait(false);
61+
await Catch(() => batchLoader.LoadAsync("Bar", "Baz"))
62+
.ConfigureAwait(false);
63+
await Task.Delay(400).ConfigureAwait(false);
64+
await Catch(() => batchLoader.LoadAsync("Qux"))
65+
.ConfigureAwait(false);
66+
await Task.Delay(400).ConfigureAwait(false);
67+
await Catch(() => batchErrorLoader.LoadAsync("Foo"))
68+
.ConfigureAwait(false);
69+
await Task.Delay(400).ConfigureAwait(false);
70+
await Catch(() => singleLoader.LoadAsync("Foo"))
71+
.ConfigureAwait(false);
72+
await Task.Delay(400).ConfigureAwait(false);
73+
await Catch(() => singleLoader.LoadAsync("Foo", "Bar"))
74+
.ConfigureAwait(false);
75+
await Task.Delay(400).ConfigureAwait(false);
76+
await Catch(() => singleLoader.LoadAsync("Bar", "Baz"))
77+
.ConfigureAwait(false);
78+
await Task.Delay(400).ConfigureAwait(false);
79+
await Catch(() => singleLoader.LoadAsync("Qux"))
80+
.ConfigureAwait(false);
4381

4482
// assert
45-
Assert.Collection(listener.Keys,
46-
(key) => Assert.Equal("Foo", key));
47-
Assert.Collection(listener.Values,
48-
(item) =>
49-
{
50-
Assert.Equal("Foo", item.Key);
51-
Assert.Null(item.Value);
52-
});
53-
Assert.Collection(listener.BatchErrors,
54-
(item) =>
55-
{
56-
Assert.Equal("Foo", item.Key);
57-
Assert.Equal("Quux", item.Value.Message);
58-
});
83+
listener.Snapshot();
84+
}
85+
}
86+
87+
private async Task<IReadOnlyList<Result<string>>> FetchDataAsync(
88+
IReadOnlyList<string> keys,
89+
CancellationToken cancellationToken)
90+
{
91+
var results = new Result<string>[keys.Count];
92+
93+
for (var i = 0; i < keys.Count; i++)
94+
{
95+
results[i] = _results.TryGetValue(keys[i],
96+
out Result<string> result)
97+
? result
98+
: Result<string>.Resolve(null);
99+
}
100+
101+
return await Task.FromResult(results).ConfigureAwait(false);
102+
}
103+
104+
private async Task Catch(Func<Task> execute)
105+
{
106+
try
107+
{
108+
await execute().ConfigureAwait(false);
109+
}
110+
catch
111+
{
59112
}
60113
}
61114
}

src/DiagnosticSource.Tests/DiagnosticSource.Tests.csproj

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp2.1</TargetFramework>
@@ -14,6 +14,7 @@
1414
</PropertyGroup>
1515

1616
<ItemGroup>
17+
<PackageReference Include="ChilliCream.Testing.Utilities" Version="0.2.0" />
1718
<PackageReference Include="Microsoft.Extensions.DiagnosticAdapter" Version="2.1.0" />
1819
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
1920
<PackageReference Include="Microsoft.CodeCoverage" Version="15.8.0" />
@@ -38,4 +39,10 @@
3839
<ProjectReference Include="..\Core\Core.csproj" />
3940
</ItemGroup>
4041

42+
<ItemGroup>
43+
<None Update="__snapshots__\*">
44+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
45+
</None>
46+
</ItemGroup>
47+
4148
</Project>
Lines changed: 66 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,100 @@
11
using System;
22
using System.Collections.Concurrent;
33
using System.Collections.Generic;
4-
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Threading.Tasks;
56
using Microsoft.Extensions.DiagnosticAdapter;
67

78
namespace GreenDonut
89
{
910
public class TestListener
10-
: DiagnosticListener
1111
{
12-
public readonly ConcurrentDictionary<object, Exception> BatchErrors =
13-
new ConcurrentDictionary<object, Exception>();
14-
public readonly ConcurrentQueue<object> Keys =
15-
new ConcurrentQueue<object>();
16-
public readonly ConcurrentDictionary<object, object> Values =
17-
new ConcurrentDictionary<object, object>();
18-
19-
public TestListener()
20-
: base("GreenDonut")
21-
{ }
12+
public readonly ConcurrentQueue<KeyValuePair<string, string>>
13+
BatchErrors =
14+
new ConcurrentQueue<KeyValuePair<string, string>>();
15+
public readonly ConcurrentQueue<string> BatchKeys =
16+
new ConcurrentQueue<string>();
17+
public readonly ConcurrentQueue<KeyValuePair<string, string>>
18+
BatchEntries =
19+
new ConcurrentQueue<KeyValuePair<string, string>>();
20+
public readonly ConcurrentQueue<KeyValuePair<string, Task<string>>>
21+
CachedEntries =
22+
new ConcurrentQueue<KeyValuePair<string, Task<string>>>();
23+
public readonly ConcurrentQueue<KeyValuePair<string, string>>
24+
Entries =
25+
new ConcurrentQueue<KeyValuePair<string, string>>();
26+
public readonly ConcurrentQueue<KeyValuePair<string, string>>
27+
Errors =
28+
new ConcurrentQueue<KeyValuePair<string, string>>();
29+
public readonly ConcurrentQueue<string> Keys =
30+
new ConcurrentQueue<string>();
2231

2332
[DiagnosticName("GreenDonut.BatchError")]
24-
public void OnBatchError(object key, Exception exception)
33+
public void OnBatchError(
34+
IReadOnlyList<string> keys,
35+
Exception exception)
36+
{
37+
BatchErrors.Enqueue(
38+
new KeyValuePair<string, string>(
39+
keys.FirstOrDefault(),
40+
exception.Message));
41+
}
42+
43+
[DiagnosticName("GreenDonut.CachedValue")]
44+
public void OnCachedValue(string key, Task<string> value)
45+
{
46+
CachedEntries.Enqueue(
47+
new KeyValuePair<string, Task<string>>(key, value));
48+
}
49+
50+
[DiagnosticName("GreenDonut.Error")]
51+
public void OnError(string key, Exception exception)
2552
{
26-
BatchErrors.TryAdd(key, exception);
53+
Errors.Enqueue(
54+
new KeyValuePair<string, string>(key, exception.Message));
2755
}
2856

2957
[DiagnosticName("GreenDonut.ExecuteBatchRequest")]
3058
public void OnExecuteBatchRequest() { }
3159

3260
[DiagnosticName("GreenDonut.ExecuteBatchRequest.Start")]
3361
public void OnExecuteBatchRequestStart(
34-
IReadOnlyList<object> keys)
62+
IReadOnlyList<string> keys)
3563
{
3664
for (var i = 0; i < keys.Count; i++)
3765
{
38-
Keys.Enqueue(keys[i]);
66+
BatchKeys.Enqueue(keys[i]);
3967
}
4068
}
4169

4270
[DiagnosticName("GreenDonut.ExecuteBatchRequest.Stop")]
4371
public void OnExecuteBatchRequestStop(
44-
IReadOnlyList<object> keys,
45-
IReadOnlyList<object> values)
72+
IReadOnlyList<string> keys,
73+
IReadOnlyList<string> values)
4674
{
4775
for (var i = 0; i < keys.Count; i++)
4876
{
49-
Values.TryAdd(keys[i], values[i]);
77+
BatchEntries.Enqueue(
78+
new KeyValuePair<string, string>(keys[i], values[i]));
5079
}
5180
}
81+
82+
[DiagnosticName("GreenDonut.ExecuteSingleRequest")]
83+
public void OnExecuteSingleRequest() { }
84+
85+
[DiagnosticName("GreenDonut.ExecuteSingleRequest.Start")]
86+
public void OnExecuteSingleRequestStart(string key)
87+
{
88+
Keys.Enqueue(key);
89+
}
90+
91+
[DiagnosticName("GreenDonut.ExecuteSingleRequest.Stop")]
92+
public void OnExecuteSingleRequestStop(
93+
string key,
94+
IReadOnlyCollection<string> values)
95+
{
96+
Entries.Enqueue(new KeyValuePair<string, string>(key,
97+
values.FirstOrDefault()));
98+
}
5299
}
53100
}

src/DiagnosticSource.Tests/TestObserver.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public void OnError(Exception error) { }
2020

2121
public void OnNext(DiagnosticListener value)
2222
{
23-
if (value.Name == _listener.Name)
23+
if (value.Name == "GreenDonut")
2424
{
2525
value.SubscribeWithAdapter(_listener);
2626
}

0 commit comments

Comments
 (0)