-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from jaredmoo/MultiShardConnectionMultipleEnu…
…meration Refactored MultiShardConnection validation and evaluate enumerables once
- Loading branch information
Showing
3 changed files
with
151 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
using System; | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
namespace Microsoft.Azure.SqlDatabase.ElasticScale.Query.UnitTests | ||
{ | ||
public static class EnumerableHelpers | ||
{ | ||
public static IEnumerable<T> ToConsumable<T>(this IEnumerable<T> source) | ||
{ | ||
return new ConsumingEnumerable<T>(source); | ||
} | ||
|
||
/// <summary> | ||
/// IEnumerable wrapper that can only be enumerated once. | ||
/// </summary> | ||
/// <typeparam name="T"></typeparam> | ||
public class ConsumingEnumerable<T> : IEnumerable<T> | ||
{ | ||
private IEnumerable<T> _source; | ||
private int _consumed; | ||
|
||
public ConsumingEnumerable(IEnumerable<T> source) | ||
{ | ||
_source = source; | ||
_consumed = 0; | ||
} | ||
|
||
public IEnumerator<T> GetEnumerator() | ||
{ | ||
int wasConsumed = Interlocked.Exchange(ref _consumed, 1); | ||
if (wasConsumed == 0) | ||
{ | ||
return _source.GetEnumerator(); | ||
} | ||
else | ||
{ | ||
throw new InvalidOperationException("GetEnumerator() has already been called. Cannot enumerate more than once"); | ||
} | ||
} | ||
|
||
IEnumerator IEnumerable.GetEnumerator() | ||
{ | ||
return GetEnumerator(); | ||
} | ||
} | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
Test/ElasticScale.Query.UnitTests/MultiShardConnectionTests.cs
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.Generic; | ||
using System.Linq; | ||
using Microsoft.Azure.SqlDatabase.ElasticScale.ShardManagement; | ||
using Microsoft.Azure.SqlDatabase.ElasticScale.Test.Common; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
namespace Microsoft.Azure.SqlDatabase.ElasticScale.Query.UnitTests | ||
{ | ||
[TestClass] | ||
public class MultiShardConnectionTests | ||
{ | ||
private const string dummyConnectionString = "User ID=x;Password=x"; | ||
|
||
/// <summary> | ||
/// Verifies that <see cref="MultiShardConnection.MultiShardConnection(IEnumerable{ShardLocation}, string)"/> | ||
/// throws when the input shardlocations are null | ||
/// </summary> | ||
[TestMethod] | ||
public void TestMultiShardConnectionConstructorThrowsNullShardLocations() | ||
{ | ||
AssertExtensions.AssertThrows<ArgumentNullException>( | ||
() => new MultiShardConnection((IEnumerable<ShardLocation>)null, dummyConnectionString)); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that <see cref="MultiShardConnection.MultiShardConnection(IEnumerable{ShardLocation}, string)"/> | ||
/// throws when the input shards are null | ||
/// </summary> | ||
[TestMethod] | ||
public void TestMultiShardConnectionConstructorThrowsNullShards() | ||
{ | ||
AssertExtensions.AssertThrows<ArgumentNullException>( | ||
() => new MultiShardConnection((IEnumerable<Shard>)null, dummyConnectionString)); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that <see cref="MultiShardConnection.MultiShardConnection(IEnumerable{ShardLocation}, string)"/> | ||
/// does not multiply evaluate the input enumerable | ||
/// </summary> | ||
[TestMethod] | ||
public void TestMultiShardConnectionConstructorEvaluatesShardLocations() | ||
{ | ||
List<ShardLocation> shardLocations = new List<ShardLocation> | ||
{ | ||
new ShardLocation("server1", "db1"), | ||
new ShardLocation("server2", "db2"), | ||
new ShardLocation("server3", "db3") | ||
}; | ||
|
||
MultiShardConnection conn = new MultiShardConnection(shardLocations.ToConsumable(), dummyConnectionString); | ||
AssertExtensions.AssertSequenceEqual(shardLocations, conn.ShardLocations); | ||
} | ||
|
||
/// <summary> | ||
/// Verifies that <see cref="MultiShardConnection.MultiShardConnection(IEnumerable{Shard}, string)"/> | ||
/// does not multiply evaluate the input enumerable | ||
/// </summary> | ||
[TestMethod] | ||
public void TestMultiShardConnectionConstructorEvaluatesShards() | ||
{ | ||
MultiShardTestUtils.DropAndCreateDatabases(); | ||
ShardMap shardMap = MultiShardTestUtils.CreateAndGetTestShardMap(); | ||
|
||
List<Shard> shards = shardMap.GetShards().ToList(); | ||
|
||
MultiShardConnection conn = new MultiShardConnection(shards.ToConsumable(), dummyConnectionString); | ||
AssertExtensions.AssertSequenceEqual(shards, conn.Shards); | ||
} | ||
} | ||
} |