Is it possible to mock ServicePartitionLists? #172
-
I found this answer on how to abstract away a FabricClient https://stackoverflow.com/a/49315283/15354710 However, I can't find a way to mock the ServicePartitionList that would be the result. My goal is to retrieve information of the Int64RangePartitionInformation types, for each of the partitions in the ServicePartitionList. Do I need to abstract it further in order to get what I need, e.g. something like this: public interface IPartitionInformationProvider
{
Task<ServicePartitionList> GetPartitionListAsync(Uri serviceUri);
Task<List<Int64RangePartitionInformation>> GetPartitionInfos(Uri serviceUri);
} public class DefaultPartitionInformationProvider : IPartitionInformationProvider
{
private readonly FabricClient _fabricClient;
public DefaultPartitionInformationProvider(FabricClient fabricClient)
{
_fabricClient = fabricClient;
}
public async Task<ServicePartitionList> GetPartitionListAsync(Uri serviceUri)
{
return await _fabricClient.QueryManager.GetPartitionListAsync(serviceUri);
}
public async Task<List<Int64RangePartitionInformation>> GetPartitionInfos(Uri serviceUri)
{
var partitionList = await _fabricClient.QueryManager.GetPartitionListAsync(serviceUri);
var partitionInformationList = partitionList
.Select(partition => (Int64RangePartitionInformation)partition.PartitionInformation)
.ToList();
return partitionInformationList;
}
} Which is mockable like this using the ServiceFabric.Mocks package public class MockPartitionInformationProvider : IPartitionInformationProvider
{
public Task<ServicePartitionList> GetPartitionListAsync(Uri serviceUri)
{
throw new NotImplementedException();
}
public Task<List<Int64RangePartitionInformation>> GetPartitionInfos(Uri serviceUri)
{
return Task.FromResult(new List<Int64RangePartitionInformation>
{
MockQueryPartitionFactory.CreateIntPartitonInfo(),
MockQueryPartitionFactory.CreateIntPartitonInfo()
});
}
} Is this the way to go? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hi, thanks for your question. This might work: public interface IServicePartitionInformationProvider
{
Task<ServicePartitionList> GetServicePartitionList(Uri serviceUri);
Task<List<ServicePartitionInformation>> GetServicePartitionInformationList(Uri serviceUri);
}
public class MockServicePartitionInformationProvider : IServicePartitionInformationProvider
{
private readonly IList<Partition> _partitions;
public MockServicePartitionInformationProvider(params Partition[] partitions)
{
if (partitions is null)
{
throw new ArgumentNullException(nameof(partitions));
}
_partitions = partitions.ToList();
}
public Task<ServicePartitionList> GetServicePartitionList(Uri _)
{
var ctor = typeof(ServicePartitionList).GetConstructor(Constants.InstancePublicNonPublic, null,
new[]
{
typeof(IList<Partition>)
}, null);
var instance = (ServicePartitionList)ctor.Invoke(new[] { _partitions });
return Task.FromResult(instance);
}
public Task<List<ServicePartitionInformation>> GetServicePartitionInformationList(Uri _)
{
var info = _partitions
.Select(p => p.PartitionInformation)
.ToList();
return Task.FromResult(info);
}
} Usage: [TestClass]
public class MockPartitionInformationProviderTests
{
[TestMethod]
public async Task CanGetServicePartitionList()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var partitionList = await provider.GetServicePartitionList(null);
Assert.IsNotNull(partitionList);
}
[TestMethod]
public async Task GetServicePartitionListReturnsOnePartition()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var partitionList = await provider.GetServicePartitionList(null);
Assert.AreEqual(1, partitionList.Count);
}
[TestMethod]
public async Task GetServicePartitionListReturnsPartition()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var partitionList = await provider.GetServicePartitionList(null);
Assert.AreEqual(partition, partitionList[0]);
}
[TestMethod]
public async Task CanGetServicePartitionInformationList()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var infoList = await provider.GetServicePartitionInformationList(null);
Assert.IsNotNull(infoList);
}
[TestMethod]
public async Task GetServicePartitionInformationListReturnsPartition()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var infoList = await provider.GetServicePartitionInformationList(null);
Assert.AreEqual(partition.PartitionInformation, infoList[0]);
}
[TestMethod]
public async Task GetServicePartitionInformationListReturnsInt64RangePartition()
{
var partition = MockQueryPartitionFactory.CreateStatelessPartition(MockQueryPartitionFactory.CreateIntPartitonInfo(), 1, System.Fabric.Health.HealthState.Ok, System.Fabric.Query.ServicePartitionStatus.Ready);
var provider = new MockServicePartitionInformationProvider(partition);
var infoList = await provider.GetServicePartitionInformationList(null);
Assert.IsInstanceOfType<Int64RangePartitionInformation>(infoList[0]);
}
} |
Beta Was this translation helpful? Give feedback.
Hi, thanks for your question.
You can use reflection to create an instance of
ServicePartitionList
.This might work: