public Task GetLoadMetricsSingleItem() { return(this.RunDataSizeUnitsPermutationAsync(async config => { Uri collectionName = new Uri("test://dictionary"); int key = 1; string value = "Homer"; BinaryValueConverter converter = new BinaryValueConverter(collectionName, new JsonReliableStateSerializerResolver()); double size = converter.Serialize <int>(key).Buffer.Length + converter.Serialize <string>(value).Buffer.Length; double expectedMemory = size / (double)config.MemoryMetricUnits; double expectedDisk = size / (double)config.DiskMetricUnits; MockReliableDictionary <BinaryValue, BinaryValue> store = new MockReliableDictionary <BinaryValue, BinaryValue>(collectionName); MetricReliableDictionary <int, string> target = new MetricReliableDictionary <int, string>(store, converter, config); using (ITransaction tx = new MockTransaction()) { await target.SetAsync(tx, key, value); await tx.CommitAsync(); } using (ITransaction tx = new MockTransaction()) { IEnumerable <DecimalLoadMetric> result = await target.GetLoadMetricsAsync(tx, CancellationToken.None); Assert.AreEqual <int>(1, result.Count(x => x.Name == config.MemoryMetricName && x.Value == expectedMemory)); Assert.AreEqual <int>(1, result.Count(x => x.Name == config.DiskMetricName && x.Value == expectedDisk)); } })); }
public async Task AggregateMultipleMetrics() { string expectedMetric1 = "one"; string expectedMetric2 = "two"; int expectedMetric1Value = 1; int expectedMetric2Value = 2; Uri name = new Uri("test://dictionary"); MockReliableStateManager stateManager = new MockReliableStateManager(); MockReliableDictionary <int, int> dictionary = new MockReliableDictionary <int, int>(name); dictionary.OnGetLoadMetrics = () => new DecimalLoadMetric[] { new DecimalLoadMetric(expectedMetric1, (double)expectedMetric1Value), new DecimalLoadMetric(expectedMetric2, (double)expectedMetric2Value) }; stateManager.SetMock(name, dictionary); MetricAggregator target = new MetricAggregator(); IEnumerable <LoadMetric> actual = await target.Aggregate(stateManager, CancellationToken.None); Assert.AreEqual <int>(1, actual.Count(x => x.Name == expectedMetric1 && x.Value == expectedMetric1Value)); Assert.AreEqual <int>(1, actual.Count(x => x.Name == expectedMetric2 && x.Value == expectedMetric2Value)); }
public async Task Storing_value() { var dictionary = new MockReliableDictionary <Guid, MyDto>(new Uri("fabric://popo")); var dto = new MyDto(Guid.NewGuid(), "toto", "jean", 32, new DateTime(1985, 02, 11)); var tx = new MockTransaction(null, 1); await dictionary.AddAsync(tx, dto.Id, dto); }
public async Task DictionaryAddDuplicateKeyExceptionTypeTest() { const string key = "key"; var dictionary = new MockReliableDictionary <string, string>(new Uri("fabric://MockReliableDictionary")); var tx = new MockTransaction(null, 1); await dictionary.AddAsync(tx, key, "value"); await Assert.ThrowsExceptionAsync <ArgumentException>(async() => { await dictionary.AddAsync(tx, key, "value"); }); }
public async Task DictionaryAddAndRetrieveTest() { const string key = "key"; const string value = "value"; var dictionary = new MockReliableDictionary <string, string>(new Uri("fabric://MockReliableDictionary")); var tx = new MockTransaction(null, 1); await dictionary.AddAsync(tx, key, value); var actual = await dictionary.TryGetValueAsync(tx, key); Assert.AreEqual(actual.Value, value); }
public async Task DictionaryCountTest() { const string key = "key"; const string value = "value"; var dictionary = new MockReliableDictionary <string, string>(new Uri("fabric://MockReliableDictionary")); var tx = new MockTransaction(null, 1); await dictionary.AddAsync(tx, key, value); var actual = dictionary.Count; Assert.AreEqual(1, actual); }
public async Task AggregateEmptyMetrics() { Uri name = new Uri("test://dictionary"); MockReliableStateManager stateManager = new MockReliableStateManager(); MockReliableDictionary <int, int> dictionary = new MockReliableDictionary <int, int>(name); dictionary.OnGetLoadMetrics = () => new DecimalLoadMetric[0]; stateManager.SetMock(name, dictionary); MetricAggregator target = new MetricAggregator(); IEnumerable <LoadMetric> actual = await target.Aggregate(stateManager, CancellationToken.None); Assert.IsFalse(actual.Any()); }
public async Task DictionaryCreateKeyEnumerableAsyncTest() { const string key = "key"; const string value = "value"; var dictionary = new MockReliableDictionary <string, string>(new Uri("fabric://MockReliableDictionary")); var tx = new MockTransaction(null, 1); await dictionary.AddAsync(tx, key, value); var enumerable = await dictionary.CreateKeyEnumerableAsync(tx); var enumerator = enumerable.GetAsyncEnumerator(); await enumerator.MoveNextAsync(CancellationToken.None); var actual = enumerator.Current; Assert.AreEqual(key, actual); }
public async Task AggregateMultipleCollectionsSameMetrics() { string expectedMetric1 = "one"; string expectedMetric2 = "two"; double inputMetric1Value = 1.9; double inputMetric2Value = 4.1; int expectedMetric1Value = 2; int expectedMetric2Value = 4; Uri collection1Name = new Uri("test://dictionary1"); Uri collection2Name = new Uri("test://dictionary2"); MockReliableStateManager stateManager = new MockReliableStateManager(); MockReliableDictionary <int, int> dictionary1 = new MockReliableDictionary <int, int>(collection1Name); MockReliableDictionary <int, int> dictionary2 = new MockReliableDictionary <int, int>(collection2Name); dictionary1.OnGetLoadMetrics = () => new DecimalLoadMetric[] { new DecimalLoadMetric(expectedMetric1, inputMetric1Value / 2.0), new DecimalLoadMetric(expectedMetric2, inputMetric2Value / 2.0) }; dictionary2.OnGetLoadMetrics = () => new DecimalLoadMetric[] { new DecimalLoadMetric(expectedMetric1, inputMetric1Value / 2.0), new DecimalLoadMetric(expectedMetric2, inputMetric2Value / 2.0) }; stateManager.SetMock(collection1Name, dictionary1); stateManager.SetMock(collection2Name, dictionary2); MetricAggregator target = new MetricAggregator(); IEnumerable <LoadMetric> actual = await target.Aggregate(stateManager, CancellationToken.None); Assert.AreEqual <int>(1, actual.Count(x => x.Name == expectedMetric1 && x.Value == expectedMetric1Value)); Assert.AreEqual <int>(1, actual.Count(x => x.Name == expectedMetric2 && x.Value == expectedMetric2Value)); }
public Task GetLoadMetricsEmptyDictionary() { return(RunDataSizeUnitsPermutationAsync(async config => { int expected = 0; Uri collectionName = new Uri("test://dictionary"); MockReliableDictionary <BinaryValue, BinaryValue> store = new MockReliableDictionary <BinaryValue, BinaryValue>(collectionName); BinaryValueConverter converter = new BinaryValueConverter(collectionName, new JsonReliableStateSerializerResolver()); MetricReliableDictionary <int, string> target = new MetricReliableDictionary <int, string>(store, converter, config); using (ITransaction tx = new MockTransaction()) { IEnumerable <DecimalLoadMetric> result = await target.GetLoadMetricsAsync(tx, CancellationToken.None); Assert.AreEqual <int>(1, result.Count(x => x.Name == config.MemoryMetricName && x.Value == expected)); Assert.AreEqual <int>(1, result.Count(x => x.Name == config.DiskMetricName && x.Value == expected)); } })); }
public async void ShouldSaveConfig() { var dict = new MockReliableDictionary <ConfigKey, KvpConfig>(); using (var tx = new MockTransaction()) { var config = new KvpConfig() { Value = "Test", Name = "Test", Type = "String", Version = "1.0.0" }; var key = new ConfigKey(config); await dict.TryAddAsync(tx, key, config); await tx.CommitAsync(); } using (var tx = new MockTransaction()) { var config = new KvpConfig() { Value = "Test", Name = "Test", Type = "String", Version = "1.0.0" }; var key = new ConfigKey(config); var val = await dict.TryGetValueAsync(tx, key); Assert.True(val.HasValue); Assert.Equal(val.Value.Name, config.Name); Assert.Equal(val.Value.Type, config.Type); Assert.Equal(val.Value.Version, config.Version); Assert.Equal(val.Value.Value, config.Value); } }
public async Task InsertRecord_should_index_members() { IReliableDictionary <Guid, MyDto> wrappedReliableDictionary = new MockReliableDictionary <Guid, MyDto>(new Uri("fabric://popo")); Func <ITransaction> transactionBuilder = () => new MockTransaction(null, 1); var store = await DataStore <Guid, MyDto> .Build( () => new ServiceFabAsyncKeyValueStore <Guid, MyDto>(wrappedReliableDictionary, transactionBuilder), async _ => new ServiceFabAsyncKeyValueStore <FieldValue, HashSet <Guid> >(new MockReliableDictionary <FieldValue, HashSet <Guid> >(new Uri("fabric://popo")), transactionBuilder), new Expression <Func <MyDto, string> >[] { dto => dto.FirstName, dto => dto.Lastname, dto => dto.Birth.Day.ToString() }); for (var i = 0; i < 10; i++) { var d = 1000 + i * 200; var dto = new MyDto(Guid.NewGuid(), $"firstname {i}", $"lastname {i}", i, new DateTime(1985, 02, 11) - TimeSpan.FromDays(d)); await store.Insert(dto); } var someId = (await store.Records.AllKeys()).First(); var resultsById = (await new QueryById(someId).Execute(store)).ToList(); var expectedById = (await store.Records.AllValues()).First(); Check.That(resultsById).CountIs(1); Check.That(resultsById.Single().FirstName).IsEqualTo(expectedById.FirstName); Check.That(resultsById.Single().Lastname).IsEqualTo(expectedById.Lastname); Check.That(resultsById.Single().Score).IsEqualTo(expectedById.Score); var expectedByAndoperation = (await new And( new QueryByField("FirstName", "firstname 5"), new QueryByField("Lastname", "lastname 5")).Execute(store)).ToList(); Check.That(expectedByAndoperation).CountIs(1); Check.That(expectedByAndoperation.Single().FirstName).IsEqualTo("firstname 5"); var expectedByOroperation = (await new Or( new QueryByField("FirstName", "firstname 5"), new QueryByField("Lastname", "lastname 4")).Execute(store)) .OrderBy(r => r.Score) .ToList(); Check.That(expectedByOroperation).CountIs(2); Check.That(expectedByOroperation[0].FirstName).IsEqualTo("firstname 4"); Check.That(expectedByOroperation[1].FirstName).IsEqualTo("firstname 5"); var expectedByOroperation2 = (await new Or( new QueryByField("FirstName", "firstname 5"), new Or( new QueryByField("Lastname", "lastname 4"), new QueryByField("Lastname", "lastname 2") )).Execute(store)) .OrderBy(r => r.Score) .ToList(); Check.That(expectedByOroperation2).CountIs(3); Check.That(expectedByOroperation2[0].FirstName).IsEqualTo("firstname 2"); Check.That(expectedByOroperation2[1].FirstName).IsEqualTo("firstname 4"); Check.That(expectedByOroperation2[2].FirstName).IsEqualTo("firstname 5"); var myDtos = await store.Find(q => q.Where(m => m.Lastname == "lastname 4" || m.FirstName == "firstname 2")); Check.That(myDtos).CountIs(2); }