From 8a6e01ce642739efa53280e0fb9e12a77b6cc594 Mon Sep 17 00:00:00 2001 From: "Maxim Syrykh (@slggr)" Date: Tue, 14 Apr 2015 00:20:51 +0500 Subject: [PATCH 1/2] =?UTF-8?q?=D0=97=D0=B0=D0=BF=D0=B8=D0=BB=D0=B8=D0=BB?= =?UTF-8?q?=20=D1=83=D0=BC=D0=BD=D0=BE=D0=B3=D0=BE=20=D0=BA=D0=BB=D0=B8?= =?UTF-8?q?=D0=B5=D0=BD=D1=82=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Проходит 6\8 тестов Не проходит [тест 2] --- SimpleStorage/Client/SimpleStorageClient.cs | 81 ++++---- .../SimpleStorage.Tests/FuctionalTestBase.cs | 54 +++--- .../Sharding/Task1Tests.cs | 174 ++++++++--------- .../Sharding/Task2Tests.cs | 155 ++++++++------- .../Sharding/Task3Tests.cs | 179 +++++++++--------- 5 files changed, 320 insertions(+), 323 deletions(-) diff --git a/SimpleStorage/Client/SimpleStorageClient.cs b/SimpleStorage/Client/SimpleStorageClient.cs index e69d4ed..be80769 100644 --- a/SimpleStorage/Client/SimpleStorageClient.cs +++ b/SimpleStorage/Client/SimpleStorageClient.cs @@ -1,39 +1,44 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using Domain; - -namespace Client -{ - public class SimpleStorageClient : ISimpleStorageClient - { - private readonly IEnumerable endpoints; - - public SimpleStorageClient(params string[] endpoints) - { - if (endpoints == null || !endpoints.Any()) - throw new ArgumentException("Empty endpoints!", "endpoints"); - this.endpoints = endpoints; - } - - public void Put(string id, Value value) - { - var putUri = endpoints.First() + "api/values/" + id; - using (var client = new HttpClient()) - using (var response = client.PutAsJsonAsync(putUri, value).Result) - response.EnsureSuccessStatusCode(); - } - - public Value Get(string id) - { - var requestUri = endpoints.First() + "api/values/" + id; - using (var client = new HttpClient()) - using (var response = client.GetAsync(requestUri).Result) - { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync().Result; - } - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using Domain; + +namespace Client +{ + public class SimpleStorageClient : ISimpleStorageClient + { + private readonly IEnumerable endpoints; + + public SimpleStorageClient(params string[] endpoints) + { + if (endpoints == null || !endpoints.Any()) + throw new ArgumentException("Empty endpoints!", "endpoints"); + this.endpoints = endpoints; + } + + public void Put(string id, Value value) + { + var putUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; + using (var client = new HttpClient()) + using (var response = client.PutAsJsonAsync(putUri, value).Result) + response.EnsureSuccessStatusCode(); + } + + public Value Get(string id) + { + var requestUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; + using (var client = new HttpClient()) + using (var response = client.GetAsync(requestUri).Result) { + response.EnsureSuccessStatusCode(); + return response.Content.ReadAsAsync().Result; + } + } + + static int getShardNumber(string id, int count) + { + return (Math.Abs(id.GetHashCode()) % count); + } + + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/FuctionalTestBase.cs b/SimpleStorage/SimpleStorage.Tests/FuctionalTestBase.cs index 9ce484e..91a452c 100644 --- a/SimpleStorage/SimpleStorage.Tests/FuctionalTestBase.cs +++ b/SimpleStorage/SimpleStorage.Tests/FuctionalTestBase.cs @@ -1,28 +1,28 @@ -using Domain; -using NUnit.Framework; -using SimpleStorage.Infrastructure; -using SimpleStorage.IoC; -using StructureMap; - -namespace SimpleStorage.Tests -{ - [TestFixture] - public abstract class FuctionalTestBase - { - protected Container container; - - [SetUp] - public virtual void SetUp() - { - container = IoCFactory.GetContainer(); - - container.Configure(c => c.For().Use(new StateRepository())); - - var operationLog = new OperationLog(); - container.Configure(c => c.For().Use(operationLog)); - - var storage = new Storage(operationLog, new ValueComparer()); - container.Configure(c => c.For().Use(storage)); - } - } +using Domain; +using NUnit.Framework; +using SimpleStorage.Infrastructure; +using SimpleStorage.IoC; +using StructureMap; + +namespace SimpleStorage.Tests +{ + [TestFixture] + public abstract class FuctionalTestBase + { + protected Container container; + + [SetUp] + public virtual void SetUp() + { + container = IoCFactory.GetContainer(); + + container.Configure(c => c.For().Use(new StateRepository())); + + var operationLog = new OperationLog(); + container.Configure(c => c.For().Use(operationLog)); + + var storage = new Storage(operationLog, new ValueComparer()); + container.Configure(c => c.For().Use(storage)); + } + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/Sharding/Task1Tests.cs b/SimpleStorage/SimpleStorage.Tests/Sharding/Task1Tests.cs index c8cbec9..7b53cb1 100644 --- a/SimpleStorage/SimpleStorage.Tests/Sharding/Task1Tests.cs +++ b/SimpleStorage/SimpleStorage.Tests/Sharding/Task1Tests.cs @@ -1,91 +1,85 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using Client; -using Domain; -using NUnit.Framework; - -namespace SimpleStorage.Tests.Sharding -{ - [TestFixture] - [Ignore] - public class Task1Tests - { - private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); - private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); - private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); - private readonly string[] endpoints = {endpoint1, endpoint2, endpoint3}; - private SimpleStorageClient client; - - [SetUp] - public void SetUp() - { - client = new SimpleStorageClient(endpoints); - - using (var httpClient = new HttpClient()) - foreach (var endpoint in endpoints) - { - using (var response = - httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) - .Result) - Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); - } - } - - [Test] - public void Sharding_EachShard_ShouldNotContainAllData() - { - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.LessThan(100)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.LessThan(100)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.LessThan(100)); - } - - [Test] - public void Sharding_AllShards_ShouldContainSomeData() - { - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); - } - - [Test] - public void Sharding_Always_ShouldSaveAllData() - { - var items = new List>(); - for (var i = 0; i < 100; i++) - { - var id = Guid.NewGuid().ToString(); - var value = new Value {Content = "content"}; - items.Add(new KeyValuePair(id, value)); - client.Put(id, value); - } - - foreach (var item in items) - { - var actual = client.Get(item.Key); - Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); - Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); - Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); - } - } - - private IEnumerable GetAll(string endpoint) - { - var requestUri = endpoint + "api/admin/getAllLocalData"; - using (var httpClient = new HttpClient()) - using (var response = httpClient.GetAsync(requestUri).Result) - { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync>().Result; - } - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using Client; +using Domain; +using NUnit.Framework; + +namespace SimpleStorage.Tests.Sharding +{ + [TestFixture] + public class Task1Tests + { + private static readonly string endpoint1 = string.Format ("http://127.0.0.1:{0}/", 16000); + private static readonly string endpoint2 = string.Format ("http://127.0.0.1:{0}/", 16001); + private static readonly string endpoint3 = string.Format ("http://127.0.0.1:{0}/", 16002); + private readonly string[] endpoints = { endpoint1, endpoint2, endpoint3 }; + private SimpleStorageClient client; + + [SetUp] + public void SetUp () + { + client = new SimpleStorageClient (endpoints); + + using (var httpClient = new HttpClient ()) + foreach (var endpoint in endpoints) { + using (var response = + httpClient.PostAsync (endpoint + "api/admin/removeAllData", new ByteArrayContent (new byte[0])) + .Result) + Assert.That (response.StatusCode, Is.EqualTo (HttpStatusCode.NoContent)); + } + } + + [Test] + public void Sharding_EachShard_ShouldNotContainAllData () + { + + + Assert.That (GetAll (endpoint1).ToArray (), Has.Length.LessThan (100)); + Assert.That (GetAll (endpoint2).ToArray (), Has.Length.LessThan (100)); + Assert.That (GetAll (endpoint3).ToArray (), Has.Length.LessThan (100)); + } + + [Test] + public void Sharding_AllShards_ShouldContainSomeData () + { + for (var i = 0; i < 100; i++) + client.Put (Guid.NewGuid ().ToString (), new Value { Content = "content" }); + + Assert.That (GetAll (endpoint1).ToArray (), Has.Length.GreaterThan (0)); + Assert.That (GetAll (endpoint2).ToArray (), Has.Length.GreaterThan (0)); + Assert.That (GetAll (endpoint3).ToArray (), Has.Length.GreaterThan (0)); + } + + [Test] + public void Sharding_Always_ShouldSaveAllData () + { + var items = new List> (); + for (var i = 0; i < 100; i++) { + var id = Guid.NewGuid ().ToString (); + var value = new Value { Content = "content" }; + items.Add (new KeyValuePair (id, value)); + client.Put (id, value); + } + + foreach (var item in items) { + var actual = client.Get (item.Key); + Assert.That (actual.Content, Is.EqualTo (item.Value.Content)); + Assert.That (actual.IsDeleted, Is.EqualTo (item.Value.IsDeleted)); + Assert.That (actual.Revision, Is.EqualTo (item.Value.Revision)); + } + } + + private IEnumerable GetAll (string endpoint) + { + var requestUri = endpoint + "api/admin/getAllLocalData"; + using (var httpClient = new HttpClient ()) + using (var response = httpClient.GetAsync (requestUri).Result) { + response.EnsureSuccessStatusCode (); + return response.Content.ReadAsAsync> ().Result; + } + } + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs b/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs index 29be957..63d423b 100644 --- a/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs +++ b/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs @@ -1,79 +1,78 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using Client; -using Domain; -using NUnit.Framework; - -namespace SimpleStorage.Tests.Sharding -{ - [TestFixture] - [Ignore] - public class Task2Tests - { - private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); - private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); - private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); - - [SetUp] - public void SetUp() - { - using (var httpClient = new HttpClient()) - foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) - { - using (var response = - httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) - .Result) - Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); - } - } - - [Test] - public void Sharding_AllShards_ShouldContainSomeData() - { - var client = new SimpleStorageClient(endpoint1); - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value { Content = "content" }); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); - } - - [Test] - public void Sharding_Always_ShouldReadAllData() - { - var client = new SimpleStorageClient(endpoint1); - var items = new List>(); - for (var i = 0; i < 100; i++) - { - var id = Guid.NewGuid().ToString(); - var value = new Value { Content = "content" }; - items.Add(new KeyValuePair(id, value)); - client.Put(id, value); - } - - foreach (var item in items) - foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) - { - var actual = new SimpleStorageClient(endpoint).Get(item.Key); - Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); - Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); - Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); - } - } - - private IEnumerable GetAll(string endpoint) - { - var requestUri = endpoint + "api/admin/getAllLocalData"; - using (var httpClient = new HttpClient()) - using (var response = httpClient.GetAsync(requestUri).Result) - { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync>().Result; - } - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using Client; +using Domain; +using NUnit.Framework; + +namespace SimpleStorage.Tests.Sharding +{ + [TestFixture] + public class Task2Tests + { + private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); + private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); + private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); + + [SetUp] + public void SetUp() + { + using (var httpClient = new HttpClient()) + foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) + { + using (var response = + httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) + .Result) + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + } + } + + [Test] + public void Sharding_AllShards_ShouldContainSomeData() + { + var client = new SimpleStorageClient(endpoint1); + for (var i = 0; i < 100; i++) + client.Put(Guid.NewGuid().ToString(), new Value { Content = "content" }); + + Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); + } + + [Test] + public void Sharding_Always_ShouldReadAllData() + { + var client = new SimpleStorageClient(endpoint1); + var items = new List>(); + for (var i = 0; i < 100; i++) + { + var id = Guid.NewGuid().ToString(); + var value = new Value { Content = "content" }; + items.Add(new KeyValuePair(id, value)); + client.Put(id, value); + } + + foreach (var item in items) + foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) + { + var actual = new SimpleStorageClient(endpoint).Get(item.Key); + Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); + Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); + Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); + } + } + + private IEnumerable GetAll(string endpoint) + { + var requestUri = endpoint + "api/admin/getAllLocalData"; + using (var httpClient = new HttpClient()) + using (var response = httpClient.GetAsync(requestUri).Result) + { + response.EnsureSuccessStatusCode(); + return response.Content.ReadAsAsync>().Result; + } + } + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/Sharding/Task3Tests.cs b/SimpleStorage/SimpleStorage.Tests/Sharding/Task3Tests.cs index 4698d45..4319985 100644 --- a/SimpleStorage/SimpleStorage.Tests/Sharding/Task3Tests.cs +++ b/SimpleStorage/SimpleStorage.Tests/Sharding/Task3Tests.cs @@ -1,91 +1,90 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using Client; -using Domain; -using NUnit.Framework; - -namespace SimpleStorage.Tests.Sharding -{ - [TestFixture] - [Ignore] - public class Task3Tests - { - private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); - private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); - private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); - private readonly string[] endpoints = {endpoint1, endpoint2, endpoint3}; - private SimpleStorageClient client; - - [SetUp] - public void SetUp() - { - client = new SimpleStorageClient(endpoints); - - using (var httpClient = new HttpClient()) - foreach (var endpoint in endpoints) - { - using (var response = - httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) - .Result) - Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); - } - } - - [Test] - public void Sharding_EachShard_ShouldNotContainAllData() - { - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.LessThan(100)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.LessThan(100)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.LessThan(100)); - } - - [Test] - public void Sharding_AllShards_ShouldContainSomeData() - { - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); - } - - [Test] - public void Sharding_Always_ShouldSaveAllData() - { - var items = new List>(); - for (var i = 0; i < 100; i++) - { - var id = Guid.NewGuid().ToString(); - var value = new Value {Content = "content"}; - items.Add(new KeyValuePair(id, value)); - client.Put(id, value); - } - - foreach (var item in items) - { - var actual = client.Get(item.Key); - Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); - Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); - Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); - } - } - - private IEnumerable GetAll(string endpoint) - { - var requestUri = endpoint + "api/admin/getAllLocalData"; - using (var httpClient = new HttpClient()) - using (var response = httpClient.GetAsync(requestUri).Result) - { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync>().Result; - } - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using Client; +using Domain; +using NUnit.Framework; + +namespace SimpleStorage.Tests.Sharding +{ + [TestFixture] + public class Task3Tests + { + private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); + private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); + private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); + private readonly string[] endpoints = {endpoint1, endpoint2, endpoint3}; + private SimpleStorageClient client; + + [SetUp] + public void SetUp() + { + client = new SimpleStorageClient(endpoints); + + using (var httpClient = new HttpClient()) + foreach (var endpoint in endpoints) + { + using (var response = + httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) + .Result) + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + } + } + + [Test] + public void Sharding_EachShard_ShouldNotContainAllData() + { + for (var i = 0; i < 100; i++) + client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); + + Assert.That(GetAll(endpoint1).ToArray(), Has.Length.LessThan(100)); + Assert.That(GetAll(endpoint2).ToArray(), Has.Length.LessThan(100)); + Assert.That(GetAll(endpoint3).ToArray(), Has.Length.LessThan(100)); + } + + [Test] + public void Sharding_AllShards_ShouldContainSomeData() + { + for (var i = 0; i < 100; i++) + client.Put(Guid.NewGuid().ToString(), new Value {Content = "content"}); + + Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); + } + + [Test] + public void Sharding_Always_ShouldSaveAllData() + { + var items = new List>(); + for (var i = 0; i < 100; i++) + { + var id = Guid.NewGuid().ToString(); + var value = new Value {Content = "content"}; + items.Add(new KeyValuePair(id, value)); + client.Put(id, value); + } + + foreach (var item in items) + { + var actual = client.Get(item.Key); + Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); + Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); + Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); + } + } + + private IEnumerable GetAll(string endpoint) + { + var requestUri = endpoint + "api/admin/getAllLocalData"; + using (var httpClient = new HttpClient()) + using (var response = httpClient.GetAsync(requestUri).Result) + { + response.EnsureSuccessStatusCode(); + return response.Content.ReadAsAsync>().Result; + } + } + } } \ No newline at end of file From 4bb26122b74f5edbebffdc791a0a6f368ac4c850 Mon Sep 17 00:00:00 2001 From: Maxim Syrykh Date: Sat, 25 Apr 2015 10:43:24 +0500 Subject: [PATCH 2/2] Add latest test and unix-eof --- SimpleStorage/Client/SimpleStorageClient.cs | 86 +++++----- .../Sharding/Task2Tests.cs | 154 +++++++++--------- .../SimpleStorage.Tests/ShardingTests.cs | 1 - 3 files changed, 120 insertions(+), 121 deletions(-) diff --git a/SimpleStorage/Client/SimpleStorageClient.cs b/SimpleStorage/Client/SimpleStorageClient.cs index be80769..4798e84 100644 --- a/SimpleStorage/Client/SimpleStorageClient.cs +++ b/SimpleStorage/Client/SimpleStorageClient.cs @@ -1,44 +1,44 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Http; -using Domain; - -namespace Client -{ - public class SimpleStorageClient : ISimpleStorageClient - { - private readonly IEnumerable endpoints; - - public SimpleStorageClient(params string[] endpoints) - { - if (endpoints == null || !endpoints.Any()) - throw new ArgumentException("Empty endpoints!", "endpoints"); - this.endpoints = endpoints; - } - - public void Put(string id, Value value) - { - var putUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; - using (var client = new HttpClient()) - using (var response = client.PutAsJsonAsync(putUri, value).Result) - response.EnsureSuccessStatusCode(); - } - - public Value Get(string id) - { - var requestUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; - using (var client = new HttpClient()) - using (var response = client.GetAsync(requestUri).Result) { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync().Result; - } - } - - static int getShardNumber(string id, int count) - { - return (Math.Abs(id.GetHashCode()) % count); - } - - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net.Http; +using Domain; + +namespace Client +{ + public class SimpleStorageClient : ISimpleStorageClient + { + private readonly IEnumerable endpoints; + + public SimpleStorageClient(params string[] endpoints) + { + if (endpoints == null || !endpoints.Any()) + throw new ArgumentException("Empty endpoints!", "endpoints"); + this.endpoints = endpoints; + } + + public void Put(string id, Value value) + { + var putUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; + using (var client = new HttpClient()) + using (var response = client.PutAsJsonAsync(putUri, value).Result) + response.EnsureSuccessStatusCode(); + } + + public Value Get(string id) + { + var requestUri = endpoints.ElementAt(getShardNumber(id, endpoints.Count())) + "api/values/" + id; + using (var client = new HttpClient()) + using (var response = client.GetAsync(requestUri).Result) { + response.EnsureSuccessStatusCode(); + return response.Content.ReadAsAsync().Result; + } + } + + static int getShardNumber(string id, int count) + { + return (Math.Abs(id.GetHashCode()) % count); + } + + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs b/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs index 63d423b..0acb2c6 100644 --- a/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs +++ b/SimpleStorage/SimpleStorage.Tests/Sharding/Task2Tests.cs @@ -1,78 +1,78 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Net.Http; -using Client; -using Domain; -using NUnit.Framework; - -namespace SimpleStorage.Tests.Sharding -{ - [TestFixture] - public class Task2Tests - { - private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); - private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); - private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); - - [SetUp] - public void SetUp() - { - using (var httpClient = new HttpClient()) - foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) - { - using (var response = - httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) - .Result) - Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); - } - } - - [Test] - public void Sharding_AllShards_ShouldContainSomeData() - { - var client = new SimpleStorageClient(endpoint1); - for (var i = 0; i < 100; i++) - client.Put(Guid.NewGuid().ToString(), new Value { Content = "content" }); - - Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); - Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); - } - - [Test] - public void Sharding_Always_ShouldReadAllData() - { - var client = new SimpleStorageClient(endpoint1); - var items = new List>(); - for (var i = 0; i < 100; i++) - { - var id = Guid.NewGuid().ToString(); - var value = new Value { Content = "content" }; - items.Add(new KeyValuePair(id, value)); - client.Put(id, value); - } - - foreach (var item in items) - foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) - { - var actual = new SimpleStorageClient(endpoint).Get(item.Key); - Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); - Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); - Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); - } - } - - private IEnumerable GetAll(string endpoint) - { - var requestUri = endpoint + "api/admin/getAllLocalData"; - using (var httpClient = new HttpClient()) - using (var response = httpClient.GetAsync(requestUri).Result) - { - response.EnsureSuccessStatusCode(); - return response.Content.ReadAsAsync>().Result; - } - } - } +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using Client; +using Domain; +using NUnit.Framework; + +namespace SimpleStorage.Tests.Sharding +{ + [TestFixture] + public class Task2Tests + { + private static readonly string endpoint1 = string.Format("http://127.0.0.1:{0}/", 16000); + private static readonly string endpoint2 = string.Format("http://127.0.0.1:{0}/", 16001); + private static readonly string endpoint3 = string.Format("http://127.0.0.1:{0}/", 16002); + + [SetUp] + public void SetUp() + { + using (var httpClient = new HttpClient()) + foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) + { + using (var response = + httpClient.PostAsync(endpoint + "api/admin/removeAllData", new ByteArrayContent(new byte[0])) + .Result) + Assert.That(response.StatusCode, Is.EqualTo(HttpStatusCode.NoContent)); + } + } + + [Test] + public void Sharding_AllShards_ShouldContainSomeData() + { + var client = new SimpleStorageClient(endpoint1); + for (var i = 0; i < 100; i++) + client.Put(Guid.NewGuid().ToString(), new Value { Content = "content" }); + + Assert.That(GetAll(endpoint1).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint2).ToArray(), Has.Length.GreaterThan(0)); + Assert.That(GetAll(endpoint3).ToArray(), Has.Length.GreaterThan(0)); + } + + [Test] + public void Sharding_Always_ShouldReadAllData() + { + var client = new SimpleStorageClient(endpoint1); + var items = new List>(); + for (var i = 0; i < 100; i++) + { + var id = Guid.NewGuid().ToString(); + var value = new Value { Content = "content" }; + items.Add(new KeyValuePair(id, value)); + client.Put(id, value); + } + + foreach (var item in items) + foreach (var endpoint in new[] { endpoint1, endpoint2, endpoint3 }) + { + var actual = new SimpleStorageClient(endpoint).Get(item.Key); + Assert.That(actual.Content, Is.EqualTo(item.Value.Content)); + Assert.That(actual.IsDeleted, Is.EqualTo(item.Value.IsDeleted)); + Assert.That(actual.Revision, Is.EqualTo(item.Value.Revision)); + } + } + + private IEnumerable GetAll(string endpoint) + { + var requestUri = endpoint + "api/admin/getAllLocalData"; + using (var httpClient = new HttpClient()) + using (var response = httpClient.GetAsync(requestUri).Result) + { + response.EnsureSuccessStatusCode(); + return response.Content.ReadAsAsync>().Result; + } + } + } } \ No newline at end of file diff --git a/SimpleStorage/SimpleStorage.Tests/ShardingTests.cs b/SimpleStorage/SimpleStorage.Tests/ShardingTests.cs index 382b62a..ceef97d 100644 --- a/SimpleStorage/SimpleStorage.Tests/ShardingTests.cs +++ b/SimpleStorage/SimpleStorage.Tests/ShardingTests.cs @@ -9,7 +9,6 @@ namespace SimpleStorage.Tests { [TestFixture] - [Ignore] public class ShardingTests { private const int port1 = 15000;