示例#1
0
        public void Parallel_CreateWriteReadFile_Async()
        {
            using (new NFX.ApplicationModel.ServiceBaseApplication(null, LACONF.AsLaconicConfig()))
            {
                var fs = FS.FileSystem.Instances[NFX_S3];

                var tasks = new List <Task>();

                System.Threading.Tasks.Parallel.For(PARALLEL_FROM, PARALLEL_TO,
                                                    (i) =>
                {
                    var fn = FN_PARALLEL_MASK.Args(i);

                    var session = fs.StartSession();

                    FileSystemDirectory dir = null;
                    FileSystemFile file     = null;

                    var t = session.GetItemAsync(S3_ROOT_FS)
                            .OnOk(item =>
                    {
                        dir = item as FileSystemDirectory;
                        return(dir.CreateFileAsync(fn));
                    }).OnOk(f => {
                        Console.WriteLine("file '{0}' created", f.Name);
                        file = f;
                        return(file.WriteAllTextAsync("Hello, {0}".Args(i)));
                    })
                            .OnOkOrError(_ => {
                        Console.WriteLine("text written into '{0}'", file.Name);
                        if (file != null && !file.Disposed)
                        {
                            file.Dispose();
                            Console.WriteLine("file '{0}' disposed", file.Name);
                        }
                    })
                            .OnOk(() => session.GetItemAsync(fs.CombinePaths(S3_ROOT_FS, fn)))
                            .OnOk(item => {
                        file = item as FileSystemFile;
                        Console.WriteLine("file {0} got", file.Name);
                        return(file.ReadAllTextAsync());
                    })
                            .OnOk(txt => {
                        Console.WriteLine("file '{0}' red {1}", file.Name, txt);
                        Assert.AreEqual("Hello, {0}".Args(i), txt);
                        return(file.DeleteAsync());
                    })
                            .OnOkOrError(_ => {
                        Console.WriteLine("file '{0}' deleted", file.Name);
                        if (file != null && !file.Disposed)
                        {
                            file.Dispose();
                            Console.WriteLine("file '{0}' disposed", file.Name);
                        }
                    })
                            .OnOk(() => session.GetItemAsync(fs.CombinePaths(S3_ROOT_FS, fn)))
                            .OnOk(item => { Assert.IsNull(item); })
                            .OnOkOrError(_ => { if (!session.Disposed)
                                                {
                                                    session.Dispose();
                                                }
                                         });

                    tasks.Add(t);
                });//Parallel.For

                Console.WriteLine("all tasks created");

                Task.WaitAll(tasks.ToArray());

                Assert.AreEqual(0, fs.Sessions.Count());//checking item registation via .ctor/.dctor

                Console.WriteLine("done");
            }
        }