-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose the responses through a class preventing additions.
The user should be able to consume such blocking collection, without being able to put responses there.
- Loading branch information
1 parent
4a7aa48
commit 1865fc7
Showing
8 changed files
with
145 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using Xunit; | ||
|
||
namespace Honeycomb.Tests | ||
{ | ||
public class ResponseCollectionTest | ||
{ | ||
[Fact] | ||
public void Wrap () | ||
{ | ||
var responses = new BlockingCollection<Response> (); | ||
responses.Add (new Response ()); | ||
responses.Add (null); | ||
|
||
var coll = new ResponseCollection (responses); | ||
Assert.Equal (coll.Responses, responses); | ||
Assert.Equal (coll.Count, 2); | ||
Assert.False (coll.IsAddingCompleted); | ||
Assert.False (coll.IsCompleted); | ||
|
||
responses.CompleteAdding (); | ||
Assert.True (coll.IsAddingCompleted); | ||
Assert.False (coll.IsCompleted); | ||
|
||
responses.Take (); | ||
responses.Take (); | ||
Assert.True (coll.IsAddingCompleted); | ||
Assert.True (coll.IsCompleted); | ||
} | ||
|
||
[Fact] | ||
public void Consume () | ||
{ | ||
var responses = new BlockingCollection<Response> (); | ||
responses.Add (null); | ||
responses.Add (null); | ||
|
||
var coll = new ResponseCollection (responses); | ||
|
||
Response response1, response2; | ||
response1 = coll.Take (); | ||
coll.TryTake (out response2); | ||
|
||
Assert.Null (response1); | ||
Assert.Null (response2); | ||
Assert.False (coll.IsAddingCompleted); | ||
Assert.False (coll.IsCompleted); | ||
Assert.Equal (coll.Count, 0); | ||
} | ||
|
||
[Fact] | ||
public void Enumerate () | ||
{ | ||
var responses = new BlockingCollection<Response> (); | ||
for (int i = 0; i < 5; i++) | ||
responses.Add (null); | ||
|
||
var coll = new ResponseCollection (responses); | ||
|
||
int count = 0; | ||
foreach (var response in coll) { | ||
count++; | ||
Assert.Null (response); | ||
} | ||
|
||
Assert.Equal (count, 5); | ||
Assert.Equal (coll.Count, 5); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.Concurrent; | ||
|
||
namespace Honeycomb | ||
{ | ||
public sealed class ResponseCollection : IEnumerable<Response> | ||
{ | ||
BlockingCollection<Response> responses; | ||
|
||
internal ResponseCollection (BlockingCollection<Response> responses) | ||
{ | ||
this.responses = responses; | ||
} | ||
|
||
internal BlockingCollection<Response> Responses { | ||
get { return responses; } | ||
} | ||
|
||
public int Count { | ||
get { return responses.Count; } | ||
} | ||
|
||
public bool IsAddingCompleted { | ||
get { return responses.IsAddingCompleted; } | ||
} | ||
|
||
public bool IsCompleted { | ||
get { return responses.IsCompleted; } | ||
} | ||
|
||
public Response Take () | ||
{ | ||
return responses.Take (); | ||
} | ||
|
||
public bool TryTake (out Response response) | ||
{ | ||
return responses.TryTake (out response); | ||
} | ||
|
||
public bool TryTake (out Response response, TimeSpan timeout) | ||
{ | ||
return responses.TryTake (out response, timeout); | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator () | ||
{ | ||
return ((IEnumerable)responses).GetEnumerator (); | ||
} | ||
|
||
IEnumerator<Response> IEnumerable<Response>.GetEnumerator () | ||
{ | ||
return ((IEnumerable<Response>)responses).GetEnumerator (); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters