Beispiel #1
0
        void LogicTick()
        {
            tickCount++;

            if (tickCount % 2 == 0)
            {
                downloadLimiter.UpdateChunks(Settings.MaximumDownloadSpeed, TotalDownloadSpeed);
                uploadLimiter.UpdateChunks(Settings.MaximumUploadSpeed, TotalUploadSpeed);
            }

            ConnectionManager.CancelPendingConnects();
            ConnectionManager.TryConnect();
            DiskManager.Tick();

            for (int i = 0; i < allTorrents.Count; i++)
            {
                allTorrents[i].Mode.Tick(tickCount);
            }

            RaiseStatsUpdate(new StatsUpdateEventArgs());
        }
Beispiel #2
0
        public async Task ExceedReadRate()
        {
            // Ensure the read rate is smaller than a block
            settings.MaximumDiskReadRate = 1;
            await diskManager.Tick(1000);

            // Queue up 6 reads, none should process.
            var buffer = new byte[Piece.BlockSize];
            int count  = 6;
            var tasks  = new List <Task> ();

            for (int i = 0; i < count; i++)
            {
                tasks.Add(diskManager.ReadAsync(fileData, 0, buffer, buffer.Length).AsTask());
            }

            Assert.AreEqual(buffer.Length * count, diskManager.PendingReads, "#1");

            // We should still process none.
            await diskManager.Tick(1000);

            Assert.AreEqual(buffer.Length * count, diskManager.PendingReads, "#2");

            // Give a proper max read rate.
            settings.MaximumDiskReadRate = Piece.BlockSize * 2;
            for (int i = 0; i < 2; i++)
            {
                await diskManager.Tick(1000);

                count -= 2;
                Assert.AreEqual(buffer.Length * count, diskManager.PendingReads, "#3." + i);
            }

            // If we add more reads after we used up our allowance they still won't process.
            for (int i = 0; i < 2; i++)
            {
                count++;
                tasks.Add(diskManager.ReadAsync(fileData, 0, buffer, buffer.Length).AsTask());
            }
            Assert.AreEqual(buffer.Length * count, diskManager.PendingReads, "#4." + count);

            while (count > 0)
            {
                await diskManager.Tick(1000);

                count -= 2;
                Assert.AreEqual(buffer.Length * count, diskManager.PendingReads, "#5." + count);
            }

            foreach (var v in tasks)
            {
                Assert.DoesNotThrowAsync(async() => await v.WithTimeout(1000), "#6");
            }
        }
        public async Task ExceedReadRate()
        {
            // Ensure the read rate is smaller than a block
            await diskManager.UpdateSettingsAsync(new EngineSettingsBuilder { MaximumDiskReadRate = 1 }.ToSettings());

            await diskManager.Tick(1000).WithTimeout();

            // Queue up 6 reads, none should process.
            var buffer = new byte[Piece.BlockSize];
            int count  = 6;
            var tasks  = new List <Task> ();

            for (int i = 0; i < count; i++)
            {
                tasks.Add(diskManager.ReadAsync(fileData, new BlockInfo(0, 0, buffer.Length), buffer).AsTask());
            }

            Assert.AreEqual(buffer.Length * count, diskManager.PendingReadBytes, "#1");

            // We should still process none.
            await diskManager.Tick(1000).WithTimeout();

            Assert.AreEqual(buffer.Length * count, diskManager.PendingReadBytes, "#2");

            // Give a proper max read rate.
            await diskManager.UpdateSettingsAsync(new EngineSettingsBuilder { MaximumDiskReadRate = Piece.BlockSize * 2 }.ToSettings());

            for (int i = 0; i < 2; i++)
            {
                await diskManager.Tick(1000).WithTimeout();

                for (int t = 0; t < 2; t++)
                {
                    var completed = await Task.WhenAny(tasks).WithTimeout();

                    await completed;
                    tasks.Remove(completed);
                }
                Assert.IsFalse(tasks.Any(t => t.IsCompleted));

                count -= 2;
                Assert.AreEqual(buffer.Length * count, diskManager.PendingReadBytes, "#3." + i);
            }

            // If we add more reads after we used up our allowance they still won't process.
            for (int i = 0; i < 2; i++)
            {
                count++;
                tasks.Add(diskManager.ReadAsync(fileData, new BlockInfo(0, 0, buffer.Length), buffer).AsTask());
            }
            Assert.AreEqual(buffer.Length * count, diskManager.PendingReadBytes, "#4." + count);
            while (count > 0)
            {
                await diskManager.Tick(1000).WithTimeout();

                for (int t = 0; t < 2; t++)
                {
                    var completed = await Task.WhenAny(tasks).WithTimeout();

                    await completed;
                    tasks.Remove(completed);
                }
                Assert.IsFalse(tasks.Any(t => t.IsCompleted));

                count -= 2;
                Assert.AreEqual(buffer.Length * count, diskManager.PendingReadBytes, "#5." + count);
            }
        }
        public async Task ExceedReadRate()
        {
            // Ensure the read rate is smaller than a block
            settings.MaximumDiskReadRate = 1;
            await diskManager.Tick(1000);

            // Queue up 6 reads, none should process.
            var buffer = new byte[Piece.BlockSize];
            var tasks  = new List <Task> ();

            for (int i = 0; i < 6; i++)
            {
                tasks.Add(diskManager.ReadAsync(fileData, 0, buffer, buffer.Length));
            }

            // We should still process none.
            await diskManager.Tick(1000);

            Assert.IsTrue(tasks.All(t => t.IsCompleted == false), "#1");

            // Give a proper max read rate.
            settings.MaximumDiskReadRate = Piece.BlockSize * 2;
            for (int i = 0; i < 2; i++)
            {
                await diskManager.Tick(1000);

                Assert.AreEqual(2, tasks.Count(t => t.IsCompleted), "#1");
                tasks.RemoveAll(t => t.IsCompleted);
            }

            // If we add more reads after we used up our allowance they still won't process.
            for (int i = 0; i < 2; i++)
            {
                tasks.Add(diskManager.ReadAsync(fileData, 0, buffer, buffer.Length));
            }

            while (tasks.Count > 0)
            {
                await diskManager.Tick(1000);

                Assert.AreEqual(2, tasks.Count(t => t.IsCompleted), "#1");
                tasks.RemoveAll(t => t.IsCompleted);
            }
        }