Skip to content

Commit

Permalink
Add missing argument checks in Consumer<>.CreateAsync
Browse files Browse the repository at this point in the history
References #21
  • Loading branch information
andreashuber-lawo committed Apr 25, 2016
1 parent 653cb87 commit 7929fe3
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
20 changes: 19 additions & 1 deletion Lawo.EmberPlusSharp/Model/Consumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,14 @@ public static Task<Consumer<TRoot>> CreateAsync(S101Client client, int timeout,
/// <param name="slot">The slot to communicate with. All outgoing <see cref="S101Message"/> objects will have
/// their <see cref="S101Message.Slot"/> property set to this value. Incoming messages are ignored, if their
/// <see cref="S101Message.Slot"/> property does not match this value.</param>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="timeout"/> is less than -1.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <list type="bullet">
/// <item><paramref name="timeout"/> is less than -1, and/or</item>
/// <item><paramref name="childrenRetrievalPolicy"/> is either less than
/// <see cref="ChildrenRetrievalPolicy.None"/> or greater than <see cref="ChildrenRetrievalPolicy.All"/>.
/// </item>
/// </list></exception>
/// <exception cref="ArgumentNullException"><paramref name="client"/> equals <c>null</c>.</exception>
/// <exception cref="Exception">An exception was thrown from one of the callbacks passed to the
/// <see cref="S101Client"/> constructor, see <see cref="Exception.Message"/> for more information.</exception>
/// <exception cref="ModelException">The model does either not match the data sent by the provider, or the
Expand All @@ -201,6 +208,17 @@ public static Task<Consumer<TRoot>> CreateAsync(S101Client client, int timeout,
public static async Task<Consumer<TRoot>> CreateAsync(
S101Client client, int timeout, ChildrenRetrievalPolicy childrenRetrievalPolicy, byte slot)
{
if (client == null)
{
throw new ArgumentNullException("client");
}

if ((childrenRetrievalPolicy < ChildrenRetrievalPolicy.None) ||
(childrenRetrievalPolicy > ChildrenRetrievalPolicy.All))
{
throw new ArgumentOutOfRangeException("childrenRetrievalPolicy");
}

var result = new Consumer<TRoot>(client, timeout, childrenRetrievalPolicy, slot);
await result.RetrieveChildrenAsync();
result.ReceiveLoop();
Expand Down
11 changes: 11 additions & 0 deletions Lawo.EmberPlusSharpTest/Model/ConsumerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,16 @@ public void ExceptionTest()
AsyncPump.Run(
async () =>
{
await AssertThrowAsync<ArgumentNullException>(() => Consumer<SingleNodeRoot>.CreateAsync(null));

using (var dummy = new S101Client(Stream.Null, Stream.Null.ReadAsync, Stream.Null.WriteAsync))
{
await AssertThrowAsync<ArgumentOutOfRangeException>(
() => Consumer<SingleNodeRoot>.CreateAsync(dummy, -2),
() => Consumer<SingleNodeRoot>.CreateAsync(dummy, 10000, ChildrenRetrievalPolicy.None - 1),
() => Consumer<SingleNodeRoot>.CreateAsync(dummy, 10000, ChildrenRetrievalPolicy.All + 1));
}

TestStandardExceptionConstructors<ModelException>();
await AssertThrowInCreateAsync<TimeoutException, SingleNodeRoot>(
"IncompleteLog1.xml",
Expand Down Expand Up @@ -1761,6 +1771,7 @@ private static int CountChildren(IEnumerable<IElement> children)
private static void CheckValues(MainRoot root)
{
Assert.IsNull(root.Parent);
Assert.AreEqual(0, root.Number);
Assert.AreEqual(string.Empty, root.Identifier);
Assert.IsNull(root.Description);
Assert.IsTrue(root.GetPath() == "/");
Expand Down

0 comments on commit 7929fe3

Please sign in to comment.