<commit_before>﻿using System.IO;
using System.Linq;
using System.Threading;
using BruTile;
using BruTile.Cache;
using BruTile.Predefined;
using Mapsui.Fetcher;
using Mapsui.Geometries;
using Mapsui.Providers;
using NUnit.Framework;

namespace Mapsui.Tests.Fetcher
{
    [TestFixture]
    public class FetchMachineTests
    {
        [Test]
        public void TileFetcherShouldRequestAllTilesJustOnes()
        {
            // Arrange
            var tileProvider = new CountingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 4;
            var expectedTiles = 256;

            // Act
            // Get all tiles of level 3
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            // Assert
            while (fetchDispatcher.Busy) { }

            Assert.AreEqual(expectedTiles, tileProvider.CountByTile.Keys.Count);
            Assert.AreEqual(expectedTiles, tileProvider.CountByTile.Values.Sum());
            Assert.AreEqual(expectedTiles, tileProvider.TotalCount);
        }

        private Feature TileToFeature(ITileSource tileProvider, TileInfo tileInfo)
        {
            var tile = tileProvider.GetTile(tileInfo);
            return new Feature();
        }

        [Test]
        public void TileRequestThatReturnsNullShouldNotBeRequestedAgain()
        {
            // Arrange
            var tileProvider = new NullTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 3;
            var tilesInLevel = 64;

            // Act
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { }
            // do it again
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }
            
            // Assert
            Assert.AreEqual(tilesInLevel, tileProvider.TotalCount);
        }

        [Test]
        [Ignore("Possible hang")]
        public void TileFetcherWithFailingFetchesShouldTryAgain()
        {
            // Arrange
            var tileProvider = new FailingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, (tileInfo) => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 3;
            var tilesInLevel = 64;

            // Act
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { }
            // do it again
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { }

            // Assert
            Assert.AreEqual(tilesInLevel * 2, tileProvider.TotalCount); // tried all tiles twice
        }
        
        [Test]
        [Ignore("Possible hang")]
        public void RepeatedRestartsShouldNotCauseInfiniteLoop()
        {
            // Arrange
            var tileProvider = new CountingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, (tileInfo) => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var numberOfWorkers = 8;
            var numberOfRestarts = 3;

            // Act
            for (int i = 0; i < numberOfRestarts; i++)
            {
                fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[3].UnitsPerPixel);
                tileMachine.Start();
                while (fetchDispatcher.Busy) { }
            }

            // Assert
            Assert.Greater(numberOfWorkers * numberOfRestarts, FetchWorker.RestartCounter);
        }
    }
}<commit_msg>Some rewriting of fetch test<commit_after>﻿using System.IO;
using System.Linq;
using System.Threading;
using BruTile;
using BruTile.Cache;
using BruTile.Predefined;
using Mapsui.Fetcher;
using Mapsui.Geometries;
using Mapsui.Providers;
using NUnit.Framework;

namespace Mapsui.Tests.Fetcher
{
    [TestFixture]
    public class FetchMachineTests
    {
        [Test]
        public void TileFetcherShouldRequestAllTilesJustOnes()
        {
            // Arrange
            var tileProvider = new CountingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 4;
            var expectedTiles = 256;

            // Act
            // Get all tiles of level 3
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            // Assert
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }

            Assert.AreEqual(expectedTiles, tileProvider.CountByTile.Keys.Count);
            Assert.AreEqual(expectedTiles, tileProvider.CountByTile.Values.Sum());
            Assert.AreEqual(expectedTiles, tileProvider.TotalCount);
        }

        private Feature TileToFeature(ITileSource tileProvider, TileInfo tileInfo)
        {
            var tile = tileProvider.GetTile(tileInfo);
            if (tile == null) return new Feature();
            return new Feature{ Geometry = new Raster(new MemoryStream(tile), tileInfo.Extent.ToBoundingBox() )};
        }

        [Test]
        public void TileRequestThatReturnsNullShouldNotBeRequestedAgain()
        {
            // Arrange
            var tileProvider = new NullTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 3;
            var tilesInLevel = 64;

            // Act
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }
            // do it again
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }
            
            // Assert
            Assert.AreEqual(tilesInLevel, tileProvider.TotalCount);
        }

        [Test]
        [Ignore("Possible hang")]
        public void TileFetcherWithFailingFetchesShouldTryAgain()
        {
            // Arrange
            var tileProvider = new FailingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var level = 3;
            var tilesInLevel = 64;

            // Act
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }

            // Act again
            fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[level].UnitsPerPixel);
            tileMachine.Start();
            while (fetchDispatcher.Busy) { Thread.Sleep(1); }

            // Assert
            Assert.AreEqual(tilesInLevel * 2, tileProvider.TotalCount); // tried all tiles twice
        }
        
        [Test]
        [Ignore("Possible hang")]
        public void RepeatedRestartsShouldNotCauseInfiniteLoop()
        {
            // Arrange
            var tileProvider = new CountingTileProvider();
            var tileSchema = new GlobalSphericalMercator();
            var tileSource = new TileSource(tileProvider, tileSchema);
            var cache = new MemoryCache<Feature>();
            var fetchDispatcher = new TileFetchDispatcher(cache, tileSource.Schema, tileInfo => TileToFeature(tileSource, tileInfo));
            var tileMachine = new FetchMachine(fetchDispatcher);
            var numberOfWorkers = 8;
            var numberOfRestarts = 3;

            // Act
            for (int i = 0; i < numberOfRestarts; i++)
            {
                fetchDispatcher.SetViewport(tileSchema.Extent.ToBoundingBox(), tileSchema.Resolutions[3].UnitsPerPixel);
                tileMachine.Start();
                while (fetchDispatcher.Busy) { Thread.Sleep(1); }
            }

            // Assert
            Assert.Greater(numberOfWorkers * numberOfRestarts, FetchWorker.RestartCounter);
        }
    }
}
{'fraction_non_alphanumeric': 0.08985071824241855, 'fraction_numerical': 0.0036616280161487183, 'mean_word_length': 2.264480539380938, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '970309', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2702, 'n_words': 575}