示例#1
0
        private static void Main(string[] args)
        {
            //Configure the distributor
            var distributor = new Distributor<File, Vendor>();
            distributor.RegisterEndpointRepository(new FileSystemEndpointRepository());
            distributor.RegisterEndpointRepository(new SharepointEndpointRepository());
            distributor.RegisterDeliverer(new FileSystemDeliverer());
            distributor.RegisterDeliverer(new SharepointDeliverer());
            distributor.MaximumConcurrentDeliveries(3);

            //Distribute a file to a vendor
            try
            {
                var task = distributor.DistributeAsync(FakeFile, FakeVendor);
                task.Wait();

                Console.WriteLine("\nDistribution succeeded.");
            }
            catch(AggregateException aggregateException)
            {
                Console.WriteLine("\nDistribution failed.");

                foreach (var exception in aggregateException.Flatten().InnerExceptions)
                {
                    Console.WriteLine($"* {exception.Message}");
                }
            }

            Console.WriteLine("\nPress enter to exit.");

            Console.ReadLine();
        }
        public void DistributeAsync_DistributesOneAtATimeWhenThrottledToOne()
        {
            var distributable1 = new FakeDistributable();
            var distributable2 = new FakeDistributable();
            var recipient = new FakeRecipient();
            var endpoint = new FakeEndpoint();

            var distributor = new Distributor<FakeDistributable, FakeRecipient>();

            var deliverer = new FakeLoggedDeliverer<FakeDistributable, FakeEndpoint>(new TimeSpan(0, 0, 0, 0, 100));

            var endpointRepository = new Mock<IEndpointRepository<FakeRecipient>>();
            endpointRepository.Setup(e => e.GetEndpointsForRecipient(recipient))
                .Returns(new[] { endpoint });

            distributor.RegisterDeliverer(deliverer);
            distributor.RegisterEndpointRepository(endpointRepository.Object);
            distributor.MaximumConcurrentDeliveries(1);

            var task1 = distributor.DistributeAsync(distributable1, recipient);
            var task2 = distributor.DistributeAsync(distributable2, recipient);

            Task.WaitAll(task1, task2);

            var lastStartTime = deliverer.LogEntries.Max(e => e.StartDateTime);
            var firstEndTime = deliverer.LogEntries.Min(e => e.EndDateTime);

            lastStartTime.ShouldBeGreaterThanOrEqualTo(firstEndTime);
        }