Skip to content

Commit

Permalink
Started work on the non-live Async tests
Browse files Browse the repository at this point in the history
Also did a bit of shuffling around.
  • Loading branch information
OJ committed Jul 3, 2011
1 parent 085ab12 commit b000aba
Show file tree
Hide file tree
Showing 8 changed files with 471 additions and 282 deletions.
37 changes: 37 additions & 0 deletions CorrugatedIron.Tests/AsyncMethodTester.cs
@@ -0,0 +1,37 @@
using System;
using System.Threading;

namespace CorrugatedIron.Tests
{
public class AsyncMethodTester<TCallbackResult>
{
private readonly AutoResetEvent _eventHandle;
private TCallbackResult _result;

public TCallbackResult Result
{
get
{
_eventHandle.WaitOne();
return _result;
}
}

public Action<TCallbackResult> HandleResult
{
get
{
return result =>
{
_result = result;
_eventHandle.Set();
};
}
}

public AsyncMethodTester()
{
_eventHandle = new AutoResetEvent(false);
}
}
}
13 changes: 9 additions & 4 deletions CorrugatedIron.Tests/CorrugatedIron.Tests.csproj
Expand Up @@ -61,10 +61,12 @@
<Compile Include="..\Metadata\VersionInfo.cs">
<Link>Properties\VersionInfo.cs</Link>
</Compile>
<Compile Include="Comms\RiakClientSetBucketPropertiesTests.cs" />
<Compile Include="Comms\RiakClientTestBase.cs" />
<Compile Include="Comms\RiakClientPingTests.cs" />
<Compile Include="Comms\RiakClientTests.cs" />
<Compile Include="AsyncMethodTester.cs" />
<Compile Include="RiakAsyncClientTests.cs" />
<Compile Include="RiakClientSetBucketPropertiesTests.cs" />
<Compile Include="RiakClientTestBase.cs" />
<Compile Include="RiakClientPingTests.cs" />
<Compile Include="RiakClientTests.cs" />
<Compile Include="Encoding\MessageEncoderUnitTests.cs" />
<Compile Include="Extensions\UnitTestExtensions.cs" />
<Compile Include="Models\MapReduce\RiakMapReduceTests.cs" />
Expand All @@ -84,6 +86,9 @@
<Name>CorrugatedIron</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Folder Include="Comms\" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
151 changes: 151 additions & 0 deletions CorrugatedIron.Tests/RiakAsyncClientTests.cs
@@ -0,0 +1,151 @@
// Copyright (c) 2010 - OJ Reeves & Jeremiah Peschka
//
// This file is provided to you under the Apache License,
// Version 2.0 (the "License"); you may not use this file
// except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

using System;
using System.Collections.Generic;
using System.Linq;
using CorrugatedIron.Extensions;
using CorrugatedIron.Models;
using CorrugatedIron.Tests.Extensions;
using Moq;
using NUnit.Framework;

namespace CorrugatedIron.Tests.RiakAsyncClientTests
{
public abstract class RiakAsyncClientTestBase<TResult>
{
protected Mock<IRiakClient> ClientMock;
protected RiakAsyncClient AsyncClient;
protected AsyncMethodTester<TResult> Tester;
protected TResult Result;

protected RiakAsyncClientTestBase()
{
ClientMock = new Mock<IRiakClient>();
AsyncClient = new RiakAsyncClient(ClientMock.Object);
Tester = new AsyncMethodTester<TResult>();
}
}

[TestFixture]
public class WhenPingingServerAsync : RiakAsyncClientTestBase<RiakResult>
{
[SetUp]
public void SetUp()
{
ClientMock.Setup(m => m.Ping()).Returns(RiakResult.Success());
AsyncClient.Ping(Tester.HandleResult);
Result = Tester.Result;
}

[Test]
public void AsyncClientInvokesCorrectClientFunction()
{
ClientMock.Verify(m => m.Ping(), Times.Once());
}

[Test]
public void AsyncClientReturnsCorrectResult()
{
Result.ShouldNotBeNull();
Result.IsSuccess.ShouldBeTrue();
}
}

[TestFixture]
public class WhenCallingGetWithObjectIdAsync : RiakAsyncClientTestBase<RiakResult<RiakObject>>
{
[SetUp]
public void SetUp()
{
ClientMock.Setup(m => m.Get(It.IsAny<string>(), It.IsAny<string>(), 2)).Returns(RiakResult<RiakObject>.Success(new RiakObject("foo", "bar", "baz")));
AsyncClient.Get(new RiakObjectId("foo", "bar"), Tester.HandleResult);
Result = Tester.Result;
}

[Test]
public void AsyncClientInvokesCorrectClientFunction()
{
ClientMock.Verify(m => m.Get(It.IsAny<string>(), It.IsAny<string>(), 2), Times.Once());
}

[Test]
public void AsyncClientReturnsCorrectResult()
{
Result.ShouldNotBeNull();
Result.IsSuccess.ShouldBeTrue();
Result.Value.ShouldNotBeNull();
Result.Value.Bucket.ShouldEqual("foo");
Result.Value.Key.ShouldEqual("bar");
Result.Value.Value.FromRiakString().ShouldEqual("baz");
}
}

[TestFixture]
public class WhenCallingGetWithBucketKeyAsync : RiakAsyncClientTestBase<RiakResult<RiakObject>>
{
[SetUp]
public void SetUp()
{
ClientMock.Setup(m => m.Get(It.IsAny<string>(), It.IsAny<string>(), 2)).Returns(RiakResult<RiakObject>.Success(new RiakObject("foo", "bar", "baz")));
AsyncClient.Get("foo", "bar", Tester.HandleResult);
Result = Tester.Result;
}

[Test]
public void AsyncClientInvokesCorrectClientFunction()
{
ClientMock.Verify(m => m.Get(It.IsAny<string>(), It.IsAny<string>(), 2), Times.Once());
}

[Test]
public void AsyncClientReturnsCorrectResult()
{
Result.ShouldNotBeNull();
Result.IsSuccess.ShouldBeTrue();
Result.Value.ShouldNotBeNull();
Result.Value.Bucket.ShouldEqual("foo");
Result.Value.Key.ShouldEqual("bar");
Result.Value.Value.FromRiakString().ShouldEqual("baz");
}
}

[TestFixture]
public class WhenCallingGetManyAsync : RiakAsyncClientTestBase<IEnumerable<RiakResult<RiakObject>>>
{
[SetUp]
public void SetUp()
{
ClientMock.Setup(m => m.Get(It.IsAny<IEnumerable<RiakObjectId>>(), 2)).Returns(new List<RiakResult<RiakObject>>());
AsyncClient.Get(new List<RiakObjectId>(), Tester.HandleResult, 2);
Result = Tester.Result;
}

[Test]
public void AsyncClientInvokesCorrectClientFunction()
{
ClientMock.Verify(m => m.Get(It.IsAny<IEnumerable<RiakObjectId>>(), 2), Times.Once());
}

[Test]
public void AsyncClientReturnsCorrectResult()
{
Result.ShouldNotBeNull();
Result.ShouldBe<List<RiakResult<RiakObject>>>();
Result.Count().ShouldEqual(0);
}
}
}

0 comments on commit b000aba

Please sign in to comment.