public async Task WriteFile_GetFile_Test() { var configuration = _options.FilePools.GetConfiguration <DefaultFilePool>(); FilePathUtil.DeleteDirIfExist(configuration.Path, true); var filePool = _filePoolFactory.GetOrCreate <DefaultFilePool>(); var content = Encoding.UTF8.GetBytes("Hello,Spool Unit Test!"); var spoolFile1 = await filePool.WriteFileAsync(new MemoryStream(content), ".txt"); Assert.Equal(DefaultFilePool.Name, spoolFile1.FilePool); Assert.Equal(1, spoolFile1.TrainIndex); var testPath = PathUtil.MapPath("../../../test-files"); var file2 = Path.Combine(testPath, "t1.txt"); var spoolFile2 = await filePool.WriteFileAsync(file2); var pending1 = filePool.GetPendingCount(); Assert.Equal(2, pending1); var processing1 = filePool.GetProcessingCount(); Assert.Equal(0, processing1); //Write third var spoolFile3 = await filePool.WriteFileAsync(file2); var spoolFiles = filePool.GetFiles(2); Assert.Equal(2, spoolFiles.Count); var pending2 = filePool.GetPendingCount(); Assert.Equal(1, pending2); var processing2 = filePool.GetProcessingCount(); Assert.Equal(2, processing2); var spoolFile_q1 = spoolFiles.FirstOrDefault(x => x.Path == spoolFile1.Path); Assert.NotNull(spoolFile_q1); Assert.Equal("default", spoolFile_q1.FilePool); Assert.Equal(".txt", spoolFile_q1.FileExt); Assert.True(File.Exists(spoolFile_q1.Path)); var spoolFile_q2 = spoolFiles.FirstOrDefault(x => x.Path == spoolFile2.Path); Assert.NotNull(spoolFile_q2); Assert.Equal("default", spoolFile_q2.FilePool); Assert.Equal(".txt", spoolFile_q2.FileExt); Assert.True(File.Exists(spoolFile_q2.Path)); Assert.True(Directory.Exists(configuration.Path)); Directory.Delete(configuration.Path, true); }
static void Main(string[] args) { Console.WriteLine("Hello World!"); IServiceCollection services = new ServiceCollection(); services .AddLogging(l => { l.AddConsole(); }) .AddSpool(); services.Configure <SpoolOptions>(options => { options.FilePools.ConfigureDefault(c => { c.Name = DefaultFilePool.Name; c.Path = "E:\\SpoolTest"; c.EnableFileWatcher = true; c.FileWatcherPath = "D:\\SpoolWatcher"; c.EnableAutoReturn = true; c.FileWatcherLastWrite = 3; c.FileWatcherSkipZeroFile = true; c.FileWatcherCopyThread = 2; c.ScanReturnFileMillSeconds = 2000; c.AutoReturnSeconds = 300; }); }); var serviceProvider = services.BuildServiceProvider(); _filePoolFactory = serviceProvider.GetService <IFilePoolFactory>(); _logger = serviceProvider.GetService <ILogger <Program> >(); var filePool = _filePoolFactory.GetOrCreate(DefaultFilePool.Name); //Run(); Console.ReadLine(); }
public static void Run() { Task.Run(async() => { var count = 0; var data = Encoding.UTF8.GetBytes("Hello,it's spool!"); while (count <= 100000) { try { var filePool = _filePoolFactory.GetOrCreate <DefaultFilePool>(); await filePool.WriteFileAsync(new MemoryStream(data), ".txt"); } catch (Exception ex) { _logger.LogError(ex, "写入出错"); } Interlocked.Increment(ref count); } }); }
/// <summary> /// Get or create file pool with generic type /// </summary> /// <typeparam name="TFilePool"></typeparam> /// <param name="filePoolFactory"></param> /// <returns></returns> public static IFilePool GetOrCreate <TFilePool>(this IFilePoolFactory filePoolFactory) { return(filePoolFactory.GetOrCreate( FilePoolNameAttribute.GetFilePoolName <TFilePool>() )); }
/// <summary> /// ctor /// </summary> public FilePool(IFilePoolFactory filePoolFactory) { _filePool = filePoolFactory.GetOrCreate <TFilePool>(); }