Exemplo n.º 1
0
		public void TestProducerConsumer()
		{
			var ProducerConsumer = new ProducerConsumer<byte>();
			Assert.AreEqual(0, ProducerConsumer.AvailableForRead);
			ProducerConsumer.Write(Encoding.UTF8.GetBytes("Hello World!"));
			Assert.AreEqual(12, ProducerConsumer.AvailableForRead);
			ProducerConsumer.Skip(0);
			Assert.AreEqual(12, ProducerConsumer.AvailableForRead);
			ProducerConsumer.Skip(6);
			Assert.AreEqual(6, ProducerConsumer.AvailableForRead);
			Assert.AreEqual("World!", Encoding.UTF8.GetString(ProducerConsumer.PeekGet(6)));
			Assert.AreEqual(6, ProducerConsumer.AvailableForRead);
		}
        public void EnumerateTransfersStreamsNewlyAvailableTransferItems()
        {
            var jobResponse1 = Stubs.BuildJobResponse(
                Stubs.Chunk1(null, false, false),
                Stubs.Chunk2(null, false, false),
                Stubs.Chunk3(null, false, false)
                );
            var jobResponse2 = Stubs.BuildJobResponse(
                Stubs.Chunk2(Stubs.NodeId2, true, true)
                );
            var jobResponse3 = Stubs.BuildJobResponse(
                Stubs.Chunk2(Stubs.NodeId2, true, true),
                Stubs.Chunk1(Stubs.NodeId1, true, true)
                );
            var jobResponse4 = Stubs.BuildJobResponse(
                Stubs.Chunk3(Stubs.NodeId2, true, true)
                );

            var node1Client = new Mock <IDs3Client>(MockBehavior.Strict).Object;
            var node2Client = new Mock <IDs3Client>(MockBehavior.Strict).Object;

            var clientFactory = new Mock <IDs3ClientFactory>(MockBehavior.Strict);

            clientFactory.Setup(cf => cf.GetClientForNodeId(Stubs.NodeId1)).Returns(node1Client);
            clientFactory.Setup(cf => cf.GetClientForNodeId(Stubs.NodeId2)).Returns(node2Client);

            var actionSequence = Queue(new { Item = (object)null, Type = "" });

            var client = new Mock <IDs3Client>(MockBehavior.Strict);

            client.Setup(c => c.BuildFactory(Stubs.Nodes)).Returns(clientFactory.Object);
            var chunkResponses = new[]
            {
                GetAvailableJobChunksResponse.RetryAfter(TimeSpan.FromMinutes(5)),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse2),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(8), jobResponse2),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse3),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(7), jobResponse3),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(6), jobResponse3),
                GetAvailableJobChunksResponse.RetryAfter(TimeSpan.FromMinutes(4)),
                GetAvailableJobChunksResponse.Success(TimeSpan.FromMinutes(11), jobResponse4)
            };
            var chunkResponseQueue = new Queue <GetAvailableJobChunksResponse>(chunkResponses);

            client
            .Setup(c => c.GetAvailableJobChunks(AvailableChunks(Stubs.JobId)))
            .Returns(() =>
            {
                var r = chunkResponseQueue.Dequeue();
                actionSequence.Enqueue(new { Item = (object)r, Type = "Allocated" });
                return(r);
            });

            var sleeps = new List <TimeSpan>();

            var source = new ReadTransferItemSource(sleeps.Add, client.Object, jobResponse1);

            var blobs = new[]
            {
                new Blob(Range.ByLength(0, 10), "foo"),
                new Blob(Range.ByLength(15, 20), "bar"),
                new Blob(Range.ByLength(0, 15), "bar"),
                new Blob(Range.ByLength(10, 10), "foo"),
                new Blob(Range.ByLength(0, 10), "hello"),
                new Blob(Range.ByLength(35, 11), "bar")
            };
            var producerConsumer  = new ProducerConsumer(1);
            var completeBlobsTask = Task.Run(() =>
            {
                for (int i = 0; i < blobs.Length; i++)
                {
                    producerConsumer.Read(() =>
                                          actionSequence.Enqueue(new { Item = (object)blobs[i], Type = "Completed" })
                                          );
                    source.CompleteBlob(blobs[i]);
                }
            });

            CollectionAssert.AreEqual(
                new[]
            {
                new TransferItem(node2Client, blobs[0]),
                new TransferItem(node2Client, blobs[1]),
                new TransferItem(node1Client, blobs[2]),
                new TransferItem(node1Client, blobs[3]),
                new TransferItem(node2Client, blobs[4]),
                new TransferItem(node2Client, blobs[5]),
            },
                source
                .EnumerateAvailableTransfers()
                .Select(ti =>
            {
                producerConsumer.Write(() =>
                                       actionSequence.Enqueue(new { Item = (object)ti.Blob, Type = "Returned" })
                                       );
                return(ti);
            })
                .ToArray(),
                new TransferItemSourceHelpers.TransferItemComparer()
                );
            completeBlobsTask.Wait();
            var results = actionSequence.ToArray();

            CollectionAssert.AreEqual(
                new[]
            {
                new { Item = (object)chunkResponses[0], Type = "Allocated" },
                new { Item = (object)chunkResponses[1], Type = "Allocated" },
                new { Item = (object)blobs[0], Type = "Returned" },
                new { Item = (object)blobs[0], Type = "Completed" },
                new { Item = (object)blobs[1], Type = "Returned" },
                new { Item = (object)blobs[1], Type = "Completed" },
                new { Item = (object)chunkResponses[2], Type = "Allocated" },
                new { Item = (object)chunkResponses[3], Type = "Allocated" },
                new { Item = (object)blobs[2], Type = "Returned" },
                new { Item = (object)blobs[2], Type = "Completed" },
                new { Item = (object)blobs[3], Type = "Returned" },
                new { Item = (object)blobs[3], Type = "Completed" },
                new { Item = (object)chunkResponses[4], Type = "Allocated" },
                new { Item = (object)chunkResponses[5], Type = "Allocated" },
                new { Item = (object)chunkResponses[6], Type = "Allocated" },
                new { Item = (object)chunkResponses[7], Type = "Allocated" },
                new { Item = (object)blobs[4], Type = "Returned" },
                new { Item = (object)blobs[4], Type = "Completed" },
                new { Item = (object)blobs[5], Type = "Returned" },
                new { Item = (object)blobs[5], Type = "Completed" },
            },
                results
                );
            CollectionAssert.AreEqual(
                new[]
            {
                TimeSpan.FromMinutes(5),
                TimeSpan.FromMinutes(8),
                TimeSpan.FromMinutes(7),
                TimeSpan.FromMinutes(6),
                TimeSpan.FromMinutes(4)
            },
                sleeps
                );

            clientFactory.VerifyAll();
            client.VerifyAll();
        }