public void GetContent_CachesRemoteSourcesInFiles()
        {
            // Arrange
            // Arbitrary permalink
            const string                url            = "https://raw.githubusercontent.com/JeringTech/Markdig.Extensions.FlexiBlocks/6998b1c27821d8393ad39beb54f782515c39d98b/test/FlexiBlocks.Tests/exampleInclude.md";
            var                         dummyUri       = new Uri(url);
            IContentRetrieverService    testSubject    = _serviceProvider.GetRequiredService <IContentRetrieverService>();
            ReadOnlyCollection <string> expectedResult = testSubject.GetContent(dummyUri, _fixture.TempDirectory);

            ((IDisposable)_serviceProvider).Dispose(); // Avoid retrieving from in-memory cache

            Mock <IHttpClientService> mockHttpClientService = _mockRepository.Create <IHttpClientService>();
            var services = new ServiceCollection();

            services.AddFlexiBlocks();
            services.AddSingleton(mockHttpClientService.Object);
            _serviceProvider = services.BuildServiceProvider();
            testSubject      = _serviceProvider.GetRequiredService <IContentRetrieverService>();

            // Act
            ReadOnlyCollection <string> result = testSubject.GetContent(dummyUri, _fixture.TempDirectory);

            // Assert
            Assert.Equal(expectedResult, result);
            mockHttpClientService.Verify(h => h.GetAsync(It.IsAny <Uri>(), HttpCompletionOption.ResponseHeadersRead, default), Times.Never);
        }
        public void GetContent_CachesSourcesInMemory()
        {
            // Arrange
            const string dummySource = "this\nis\na\ndummy\nsource";
            var          dummyUri    = new Uri(_dummyFile);

            using (FileStream fileStream = File.Open(_dummyFile, FileMode.Create, FileAccess.Write, FileShare.None))
            {
                byte[] bytes = Encoding.UTF8.GetBytes(dummySource);
                fileStream.Write(bytes, 0, bytes.Length);
            }
            IContentRetrieverService testSubject = _serviceProvider.GetRequiredService <IContentRetrieverService>();

            // Act
            var threads = new List <Thread>();
            var results = new ConcurrentBag <ReadOnlyCollection <string> >();

            for (int i = 0; i < 3; i++)
            {
                var thread = new Thread(() => results.Add(testSubject.GetContent(dummyUri)));
                thread.Start();
                threads.Add(thread);
            }
            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            // Assert
            ReadOnlyCollection <string>[] resultsArr = results.ToArray();
            Assert.Equal(3, resultsArr.Length);
            Assert.Equal(dummySource.Split('\n'), resultsArr[0]);
            Assert.Same(resultsArr[0], resultsArr[1]); // Result should get cached after first call
            Assert.Same(resultsArr[1], resultsArr[2]); // The same relation is transitive, so we do not need to check if the item at index 0 is the same as the item at index 2
        }
Exemplo n.º 3
0
 /// <summary>
 /// Creates a <see cref="FlexiIncludeBlockFactory"/>.
 /// </summary>
 /// <param name="contextObjectsService">The service for storing <see cref="FlexiIncludeBlock"/> trees.</param>
 /// <param name="directoryService">The service for validating cache directories.</param>
 /// <param name="optionsService">The service for creating <see cref="IFlexiIncludeBlockOptions"/> and <see cref="IFlexiIncludeBlocksExtensionOptions"/>.</param>
 /// <param name="contentRetrieverService">The service that handles content retrieval.</param>
 /// <param name="leadingWhitespaceEditorService">The service for editing of leading whitespace.</param>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="contextObjectsService"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="directoryService"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="optionsService"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="contentRetrieverService"/> is <c>null</c>.</exception>
 /// <exception cref="ArgumentNullException">Thrown if <paramref name="leadingWhitespaceEditorService"/> is <c>null</c>.</exception>
 public FlexiIncludeBlockFactory(IContextObjectsService contextObjectsService,
                                 IDirectoryService directoryService,
                                 IOptionsService <IFlexiIncludeBlockOptions, IFlexiIncludeBlocksExtensionOptions> optionsService,
                                 IContentRetrieverService contentRetrieverService,
                                 ILeadingWhitespaceEditorService leadingWhitespaceEditorService)
 {
     _contextObjectsService          = contextObjectsService ?? throw new ArgumentNullException(nameof(contextObjectsService));
     _directoryService               = directoryService ?? throw new ArgumentNullException(nameof(directoryService));
     _optionsService                 = optionsService ?? throw new ArgumentNullException(nameof(optionsService));
     _contentRetrieverService        = contentRetrieverService ?? throw new ArgumentNullException(nameof(contentRetrieverService));
     _leadingWhitespaceEditorService = leadingWhitespaceEditorService ?? throw new ArgumentNullException(nameof(leadingWhitespaceEditorService));
 }