Skip to content

Commit 1e1d7b5

Browse files
committed
Updated code for Pipelines
1 parent 514d7b5 commit 1e1d7b5

5 files changed

Lines changed: 92 additions & 166 deletions

File tree

.appveyor.yml

Lines changed: 0 additions & 20 deletions
This file was deleted.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ PipelinesTestLogger can report test results automatically to the CI build.
1313

1414
## Credit
1515

16-
This project is based on [appveyor.testlogger](https://github.com/spekt/appveyor.testlogger).
16+
This project is based on [appveyor.testlogger](https://github.com/spekt/appveyor.testlogger) and [xunit](https://github.com/xunit/xunit/blob/master/src/xunit.runner.reporters/VstsReporter.cs).

src/PipelinesTestLogger/AsyncProducerConsumerCollection.cs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void Cancel()
2323
waiting.Clear();
2424
}
2525

26-
foreach (var tcs in allWaiting)
26+
foreach (TaskCompletionSource<T[]> tcs in allWaiting)
2727
{
2828
tcs.TrySetResult(new T[] { });
2929
}
@@ -34,8 +34,14 @@ public void Add(T item)
3434
TaskCompletionSource<T[]> tcs = null;
3535
lock (collection)
3636
{
37-
if (waiting.Count > 0) tcs = waiting.Dequeue();
38-
else collection.Enqueue(item);
37+
if (waiting.Count > 0)
38+
{
39+
tcs = waiting.Dequeue();
40+
}
41+
else
42+
{
43+
collection.Enqueue(item);
44+
}
3945
}
4046

4147
tcs?.TrySetResult(new [] {item});
@@ -51,13 +57,13 @@ public Task<T[]> TakeAsync()
5157
{
5258
if (collection.Count > 0)
5359
{
54-
var result = Task.FromResult(collection.ToArray());
60+
Task<T[]> result = Task.FromResult(collection.ToArray());
5561
collection.Clear();
5662
return result;
5763
}
5864
else if (canceled == false)
5965
{
60-
var tcs = new TaskCompletionSource<T[]>();
66+
TaskCompletionSource<T[]> tcs = new TaskCompletionSource<T[]>();
6167
waiting.Enqueue(tcs);
6268
return tcs.Task;
6369
}

src/PipelinesTestLogger/LoggerQueue.cs

Lines changed: 32 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Net.Http;
4+
using System.Net.Http.Headers;
45
using System.Text;
56
using System.Threading;
67
using System.Threading.Tasks;
@@ -9,45 +10,43 @@ namespace PipelinesTestLogger
910
{
1011
internal class LoggerQueue
1112
{
12-
private static readonly HttpClient client = new HttpClient();
13+
private static readonly HttpClient _client = new HttpClient();
1314

14-
/// <summary>
15-
/// it is localhost with a random port, e.g. http://localhost:9023/
16-
/// </summary>
17-
private readonly string appveyorApiUrl;
15+
private readonly string _apiUrl;
1816

19-
private readonly AsyncProducerConsumerCollection<string> queue = new AsyncProducerConsumerCollection<string>();
20-
private readonly Task consumeTask;
21-
private readonly CancellationTokenSource consumeTaskCancellationSource = new CancellationTokenSource();
17+
private readonly AsyncProducerConsumerCollection<string> _queue = new AsyncProducerConsumerCollection<string>();
18+
private readonly Task _consumeTask;
19+
private readonly CancellationTokenSource _consumeTaskCancellationSource = new CancellationTokenSource();
2220

2321
private int totalEnqueued = 0;
2422
private int totalSent = 0;
2523

26-
public LoggerQueue(string appveyorApiUrl)
24+
public LoggerQueue(string accessToken, string apiUrl)
2725
{
28-
this.appveyorApiUrl = appveyorApiUrl;
29-
this.consumeTask = ConsumeItemsAsync(consumeTaskCancellationSource.Token);
26+
_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
27+
_apiUrl = apiUrl;
28+
_consumeTask = ConsumeItemsAsync(_consumeTaskCancellationSource.Token);
3029
}
3130

3231
public void Enqueue(string json)
3332
{
34-
queue.Add(json);
33+
_queue.Add(json);
3534
totalEnqueued++;
3635
}
3736

3837
public void Flush()
3938
{
4039
// Cancel any idle consumers and let them return
41-
queue.Cancel();
40+
_queue.Cancel();
4241

4342
try
4443
{
45-
// any active consumer will circle back around and batch post the remaining queue.
46-
consumeTask.Wait(TimeSpan.FromSeconds(60));
44+
// Any active consumer will circle back around and batch post the remaining queue.
45+
_consumeTask.Wait(TimeSpan.FromSeconds(60));
4746

4847
// Cancel any active HTTP requests if still hasn't finished flushing
49-
consumeTaskCancellationSource.Cancel();
50-
if (!consumeTask.Wait(TimeSpan.FromSeconds(10)))
48+
_consumeTaskCancellationSource.Cancel();
49+
if (!_consumeTask.Wait(TimeSpan.FromSeconds(10)))
5150
{
5251
throw new TimeoutException("cancellation didn't happen quickly");
5352
}
@@ -56,48 +55,36 @@ public void Flush()
5655
{
5756
Console.WriteLine(ex);
5857
}
59-
60-
#if DEBUG
61-
Console.WriteLine("PipelinesTestLogger: {0} test results reported ({1} enqueued).", totalSent, totalEnqueued);
62-
#endif
6358
}
6459

6560
private async Task ConsumeItemsAsync(CancellationToken cancellationToken)
6661
{
6762
while (true)
6863
{
69-
string[] nextItems = await this.queue.TakeAsync();
70-
if (nextItems == null || nextItems.Length == 0) return; // Queue is cancelling and and empty.
71-
72-
if (nextItems.Length == 1) await PostItemAsync(nextItems[0], cancellationToken);
73-
else if (nextItems.Length > 1) await PostBatchAsync(nextItems, cancellationToken);
64+
string[] nextItems = await _queue.TakeAsync();
7465

75-
if (cancellationToken.IsCancellationRequested) return;
76-
}
77-
}
78-
79-
private async Task PostItemAsync(string json, CancellationToken cancellationToken)
80-
{
81-
HttpContent content = new StringContent(json, Encoding.UTF8, "application/json");
82-
try
83-
{
84-
var response = await client.PostAsync(appveyorApiUrl + "api/tests", content, cancellationToken);
85-
response.EnsureSuccessStatusCode();
86-
totalSent += 1;
87-
}
88-
catch (Exception e)
89-
{
90-
Console.WriteLine(e);
66+
if (nextItems == null || nextItems.Length == 0)
67+
{
68+
// Queue is canceling and is empty
69+
return;
70+
}
71+
72+
await PostResultsAsync(nextItems, cancellationToken);
73+
74+
if (cancellationToken.IsCancellationRequested)
75+
{
76+
return;
77+
}
9178
}
9279
}
9380

94-
private async Task PostBatchAsync(ICollection<string> jsonEntities, CancellationToken cancellationToken)
81+
private async Task PostResultsAsync(ICollection<string> jsonEntities, CancellationToken cancellationToken)
9582
{
96-
var jsonArray = "[" + string.Join(",", jsonEntities) + "]";
83+
string jsonArray = "[" + string.Join(",", jsonEntities) + "]";
9784
HttpContent content = new StringContent(jsonArray, Encoding.UTF8, "application/json");
9885
try
9986
{
100-
var response = await client.PostAsync(appveyorApiUrl + "api/tests/batch", content, cancellationToken);
87+
HttpResponseMessage response = await _client.PostAsync(_apiUrl, content, cancellationToken);
10188
response.EnsureSuccessStatusCode();
10289
totalSent += jsonEntities.Count;
10390
}

0 commit comments

Comments
 (0)