Skip to content

Commit

Permalink
Merge pull request #44 from richiej84/master
Browse files Browse the repository at this point in the history
Batches not always getting executed
  • Loading branch information
gblmarquez authored Sep 21, 2016
2 parents 68155b5 + 588afe0 commit 80f5a3b
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ public override IEnumerable<T> ExecuteBatches(IEnumerable<T> entities, Func<ITab
}

IEnumerable<ITableEntity> tableEntities = entities.Select(p => _entityConverter.GetEntity(p));
IEnumerable<TableBatchOperation> batches = _partitioner.GetBatches(tableEntities, operation);

return batches.AsParallel().SelectMany(GetEntities);
IEnumerable<TableBatchOperation> batches = _partitioner.GetBatches(tableEntities, operation);

// Force evaluation of the execution by calling ToArray()
return batches.AsParallel().SelectMany(GetEntities).ToArray();
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ public override IEnumerable<T> ExecuteBatches(IEnumerable<T> entities, Func<ITab
}

IEnumerable<ITableEntity> tableEntities = entities.Select(p => _entityConverter.GetEntity(p));
IEnumerable<TableBatchOperation> batches = _partitioner.GetBatches(tableEntities, operation);

return from batch in batches
from result in _cloudTable.ExecuteBatch(batch)
select _entityConverter.GetEntity((DynamicTableEntity) result.Result);
IEnumerable<TableBatchOperation> batches = _partitioner.GetBatches(tableEntities, operation);

// Force evaluation of the execution by calling ToArray()
var results = batches.SelectMany(t => _cloudTable.ExecuteBatch(t)).ToArray();
return results.Select(t => _entityConverter.GetEntity((DynamicTableEntity) t.Result));
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public void ExecuteBatches()
Assert.IsAssignableFrom<IEnumerable<Country>>(result);
entityConverterMock.Verify(p => p.GetEntity(It.IsAny<Country>()), Times.Exactly(2));
batchPartitionerMock.Verify(p => p.GetBatches(It.IsAny<IEnumerable<ITableEntity>>(), It.IsAny<Func<ITableEntity, TableOperation>>()), Times.Once());
}

[Fact]
public void ExecuteBatchesEvenWhenNotEvaluated()
{
// Arrange
Mock<ICloudTable> cloudTableMock = MocksFactory.GetCloudTableMock();
Mock<ITableEntityConverter<Country>> entityConverterMock = MocksFactory.GetTableEntityConverterMock<Country>();
Mock<ITableBatchPartitioner> batchPartitionerMock = MocksFactory.GetTableBatchPartitionerMock();
var executor = new TableRequestParallelExecutor<Country>(cloudTableMock.Object, entityConverterMock.Object, batchPartitionerMock.Object);
var entities = ObjectsFactory.GetCountries();

// Act
// We don't evaluate the call as we're not interested in the resulting entities. This should still execute the operations.
executor.ExecuteBatches(entities, TableOperation.Insert);

// Assert
cloudTableMock.Verify(t => t.ExecuteBatch(It.IsAny<TableBatchOperation>()));
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,24 @@ public void ExecuteBatches()
Assert.IsAssignableFrom<IEnumerable<Country>>(result);
entityConverterMock.Verify(p => p.GetEntity(It.IsAny<Country>()), Times.Exactly(2));
batchPartitionerMock.Verify(p => p.GetBatches(It.IsAny<IEnumerable<ITableEntity>>(), It.IsAny<Func<ITableEntity, TableOperation>>()), Times.Once());
}

[Fact]
public void ExecuteBatchesEvenWhenNotEvaluated()
{
// Arrange
Mock<ICloudTable> cloudTableMock = MocksFactory.GetCloudTableMock();
Mock<ITableEntityConverter<Country>> entityConverterMock = MocksFactory.GetTableEntityConverterMock<Country>();
Mock<ITableBatchPartitioner> batchPartitionerMock = MocksFactory.GetTableBatchPartitionerMock();
var executor = new TableRequestSequentialExecutor<Country>(cloudTableMock.Object, entityConverterMock.Object, batchPartitionerMock.Object);
var entities = ObjectsFactory.GetCountries();

// Act
// We don't evaluate the call as we're not interested in the resulting entities. This should still execute the operations.
executor.ExecuteBatches(entities, TableOperation.Insert);

// Assert
cloudTableMock.Verify(t => t.ExecuteBatch(It.IsAny<TableBatchOperation>()));
}

[Fact]
Expand Down

0 comments on commit 80f5a3b

Please sign in to comment.