public void IncrementMultiThread() { var provider = GlobalSetup.Container.Resolve <IBlobStorageProvider>(); provider.CreateContainerIfNotExist(ContainerName); //creating thread parameters var counter = new BlobCounter(provider, ContainerName, "SomeBlobName"); counter.Reset(0); var random = new Random(); const int threadsCount = 4; var increments = Range.Array(threadsCount).Select(e => Range.Array(5).Select(i => random.Next(20)).ToArray()).ToArray(); var localSums = increments.AsParallel().Select( e => { var c = new BlobCounter(provider, ContainerName, "SomeBlobName"); foreach (var increment in e) { c.Increment(increment); } return(e.Sum()); }).ToList(); Assert.AreEqual(increments.Sum(i => i.Sum()), localSums.Sum(), "Broken invariant."); Assert.AreEqual(localSums.Sum(), counter.GetValue(), "Values should be equal, BlobCounter supposed to be thread-safe"); }
protected override void Start(SquareMessage message) { if (message.IsStart) { var counterName = TemporaryBlobName <decimal> .GetNew(message.Expiration); var counter = new BlobCounter(Blobs, counterName); counter.Reset(BlobCounter.Aleph); var blobNames = Blobs.ListBlobNames(message.ContainerName).ToList(); foreach (var blobName in blobNames) { Put(new SquareMessage { BlobName = blobName, ContainerName = message.ContainerName, IsStart = false, BlobCounter = counterName }); } // dealing with rare race condition if (0m >= counter.Increment(-BlobCounter.Aleph + blobNames.Count)) { Finish(counter); } } else { var value = Blobs.GetBlob <double>(message.ContainerName, message.BlobName).Value; Blobs.PutBlob(message.ContainerName, message.BlobName, value * value); var counter = new BlobCounter(Blobs, message.BlobCounter); if (0m >= counter.Increment(-1)) { Finish(counter); } } }
public void IncrementMultiThread() { var provider = GlobalSetup.Container.Resolve<IBlobStorageProvider>(); provider.CreateContainer(ContainerName); //creating thread parameters var count = new BlobCounter(provider, ContainerName, "SomeBlobName"); count.Reset(0); var random = new Random(); const int threadsCount = 4; var increments = Range.Array(threadsCount).Convert(e => Range.Array(5).Convert(i => random.Next(20) - 10)); var localSums = increments.SelectInParallel(e => { var counter = new BlobCounter(provider, ContainerName, "SomeBlobName"); foreach (var increment in e) { counter.Increment(increment); } return e.Sum(); }, threadsCount); Assert.AreEqual(localSums.Sum(), count.GetValue(), "values should be equal, BlobCounter supposed to be thread-safe"); }