示例#1
0
        public async Task <bool> Download(string linkUrl, string outputPath)
        {
            using (var disposer = new AutoDisposer())
            {
                var req      = WebRequest.Create(linkUrl);
                var response = await disposer.Add(req.GetResponseAsync());

                var readStream  = disposer.Add(response.GetResponseStream());
                var writeStream = disposer.Add(new FileStream(outputPath, FileMode.CreateNew));

                var expectedLength = long.Parse(response.Headers["Content-Length"]);
                var haveRead       = 0;
                var chunkSize      = 8192;

                var thisChunk = new byte[chunkSize];
                while (haveRead < expectedLength)
                {
                    var toRead = expectedLength - haveRead;
                    if (toRead > chunkSize)
                    {
                        toRead = chunkSize;
                    }
                    var readBytes = await readStream.ReadAsync(thisChunk, 0, (int)toRead);

                    await writeStream.WriteAsync(thisChunk, 0, readBytes);

                    writeStream.Flush();
                    haveRead += readBytes;
                }
            }
            return(true);
        }
示例#2
0
        public async Task<bool> Download(string linkUrl, string outputPath)
        {
            using (var disposer = new AutoDisposer())
            {
                var req = WebRequest.Create(linkUrl);
                var response = await disposer.Add(req.GetResponseAsync());
                var readStream = disposer.Add(response.GetResponseStream());
                var writeStream = disposer.Add(new FileStream(outputPath, FileMode.CreateNew));

                var expectedLength = long.Parse(response.Headers["Content-Length"]);
                var haveRead = 0;
                var chunkSize = 8192;

                var thisChunk = new byte[chunkSize];
                while (haveRead < expectedLength)
                {
                    var toRead = expectedLength - haveRead;
                    if (toRead > chunkSize)
                        toRead = chunkSize;
                    var readBytes = await readStream.ReadAsync(thisChunk, 0, (int)toRead);
                    await writeStream.WriteAsync(thisChunk, 0, readBytes);
                    writeStream.Flush();
                    haveRead += readBytes;
                }
            }
            return true;
        }
示例#3
0
        Download_GivenDownloadInfoAndOutputFolder_WhenDownloadUninterrupted_ShouldDownloadFileToOutputFolder()
        {
            using (var disposer = new AutoDisposer())
            {
                var deleter    = disposer.Add(new AutoDeleter());
                var tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                var fileName   = GetRandomAlphaNumericString(5, 10) + ".exe";
                deleter.Add(tempFolder);
                var expectedFile = Path.Combine(tempFolder, fileName);
                var server       = disposer.Add(Create());
                //---------------Set up test pack-------------------
                var url           = server.GetFullUrlFor(fileName);
                var expectedBytes = GetRandomBytes(100, 200);
                server.AddFileHandler(
                    (processor, stream) =>
                {
                    if (processor.FullUrl == "/" + fileName)
                    {
                        return(expectedBytes);
                    }

                    processor.WriteStatusHeader(HttpStatusCode.NotFound, "File not found");
                    return(null);
                });

                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                Download(url, fileName, tempFolder);

                //---------------Test Result -----------------------
                Assert.IsTrue(File.Exists(expectedFile));
                CollectionAssert.AreEquivalent(expectedBytes, File.ReadAllBytes(expectedFile));
            }
        }
示例#4
0
        public Damage(int value, Vector2D position, AutoDisposer autoDisposer)
        {
            this.SetupUpdateAndAutoDispose(autoDisposer);
            this.position = position;

            {
                var font = new Font(
                    Config.MainConfig.MainFontPath,
                    35,
                    new Color(1, 1, 1, 1),
                    new Font.FontFrame[] {
                    new Font.FontFrame(5, new Color(1, 1, 1, 1)),
                    //new Font.FontFrame(2, new Color(1, 0.5, 0.5, 0.5)),
                    new Font.FontFrame(5, new Color(1, 0, 0, 0))
                },
                    0);
                var x  = position.X;
                var y  = position.Y;
                var ts = new TextSprite(value.ToString(), font, new Vector2D(x, y));
                layer.Add(ts, 20);

                ts.Rect.Position.X -= ts.GetRect().Size.X / 2;

                textSprite = ts;
            }
        }
 public void Dispose()
 {
     lock (_lock)
     {
         _disposer?.Dispose();
         _disposer = null;
     }
 }
        public void AutodisposablesAreOnlyDisposedOnce()
        {
            //---------------Set up test pack-------------------
            var disposable = Substitute.For<IDisposable>();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var ad = new AutoDisposer();
            ad.Add(disposable);
            ad.Dispose();
            ad.Dispose();

            //---------------Test Result -----------------------
            disposable.Received(1).Dispose();
        }
        public void Add_GenericVersion_ShouldReturnThing()
        {
            //---------------Set up test pack-------------------
            var disposer = new AutoDisposer();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var thing = disposer.Add(new SomeDisposable());

            //---------------Test Result -----------------------
            Assert.IsInstanceOf<SomeDisposable>(thing);
            Assert.IsFalse(thing.Disposed);
            disposer.Dispose();
            Assert.IsTrue(thing.Disposed);
        }
示例#8
0
        public void Add_GenericVersion_ShouldReturnThing()
        {
            //---------------Set up test pack-------------------
            var disposer = new AutoDisposer();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var thing = disposer.Add(new SomeDisposable());

            //---------------Test Result -----------------------
            Assert.IsInstanceOf <SomeDisposable>(thing);
            Assert.IsFalse(thing.Disposed);
            disposer.Dispose();
            Assert.IsTrue(thing.Disposed);
        }
        public void Add_WhenGivenFilePathForExistingFile_ShouldDeleteFileWhenDisposed()
        {
            //---------------Set up test pack-------------------
            var disposable = Substitute.For<IDisposable>();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (var ad = new AutoDisposer())
            {
                ad.Add(disposable);
            }

            //---------------Test Result -----------------------
            disposable.Received(1).Dispose();
        }
示例#10
0
        public void Add_WhenGivenFilePathForExistingFile_ShouldDeleteFileWhenDisposed()
        {
            //---------------Set up test pack-------------------
            var disposable = Substitute.For <IDisposable>();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            using (var ad = new AutoDisposer())
            {
                ad.Add(disposable);
            }

            //---------------Test Result -----------------------
            disposable.Received(1).Dispose();
        }
示例#11
0
        public void AutodisposablesAreOnlyDisposedOnce()
        {
            //---------------Set up test pack-------------------
            var disposable = Substitute.For <IDisposable>();

            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            var ad = new AutoDisposer();

            ad.Add(disposable);
            ad.Dispose();
            ad.Dispose();

            //---------------Test Result -----------------------
            disposable.Received(1).Dispose();
        }
示例#12
0
        public ActionEffectUpdate(string scriptPath, Action action, AutoDisposer autoDisposer)
        {
            this.autoDisposer = autoDisposer;

            script = new Emugen.Script.Script <ScriptAPI>(scriptPath, new ScriptAPI());
            script.api.MainAction = action;
            SetupUpdateAndAutoDispose(autoDisposer);
            script.Run();

            foreach (var i in script.api.TimeEvents)
            {
                if (timerMax < i.Time)
                {
                    timerMax = i.Time;
                }
            }
        }
示例#13
0
        public void ShouldPlayNicelyInParallel()
        {
            //---------------Set up test pack-------------------
            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                Parallel.For(0, 100, i =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    disposer.Add(new TempDBSqlCe());
                });

                //---------------Test Result -----------------------
            }
        }
示例#14
0
        public void ShouldPlayNicelyInParallel()
        {
            //---------------Set up test pack-------------------
            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                // localDb seems to take a little longer to spin up; reduced the parallel count to 10 to make
                //  running all tests viable
                Parallel.For(0, 10, i =>
                {
                    // ReSharper disable once AccessToDisposedClosure
                    disposer.Add(new TempDBLocalDb());
                });

                //---------------Test Result -----------------------
            }
        }
示例#15
0
        public void Dispose_ShouldDisposeOfRegisteredArticlesInReverseOrder()
        {
            //---------------Set up test pack-------------------
            var calls = new List <int>();

            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                disposer.Add(new SomeDisposableWithCallback(() => calls.Add(1)));
                disposer.Add(new SomeDisposableWithCallback(() => calls.Add(2)));

                //---------------Test Result -----------------------
            }
            Assert.AreEqual(2, calls.Count);
            Assert.AreEqual(2, calls[0]);
            Assert.AreEqual(1, calls[1]);
        }
示例#16
0
        private void RunScripts(IEnumerable <string> scripts)
        {
            if (scripts == null || !scripts.Any())
            {
                return;
            }
            using (var disposer = new AutoDisposer())
            {
                var conn = disposer.Add(CreateOpenDatabaseConnection());
                var cmd  = disposer.Add(conn.CreateCommand());

                void Exec(string s)
                {
                    cmd.CommandText = s;
                    cmd.ExecuteNonQuery();
                }

                foreach (var script in scripts)
                {
                    Exec(script);
                }
            }
        }
示例#17
0
 private void CreateEmailTables()
 {
     const string EMAIL_TABLE = @"
     CREATE TABLE [Email] (
     [EmailID] [uniqueidentifier] NOT NULL PRIMARY KEY,
     [Sender] [nvarchar](4000) NULL,
     [Subject] [nvarchar](4000) NOT NULL,
     [Body] [nvarchar](4000) NOT NULL,
     [SendAt] [datetime] NOT NULL,
     [SendAttempts] [int] NOT NULL,
     [Sent] [bit] NOT NULL,
     [LastError] [nvarchar](4000) NULL,
     [Created] [datetime] NOT NULL,
     [LastModified] [datetime] NULL,
     [Enabled] [bit] NOT NULL
     )";
     const string EMAIL_RECIPIENT_TABLE = @"
     CREATE TABLE [EmailRecipient] (
     [EmailRecipientID] [uniqueidentifier] NOT NULL PRIMARY KEY,
     [EmailID] [uniqueidentifier] NOT NULL,
     [Recipient] [nvarchar](4000) NOT NULL,
     [PrimaryRecipient] [bit] NOT NULL,
     [CC] [bit] NOT NULL,
     [BCC] [bit] NOT NULL,
     [Created] [datetime] NOT NULL,
     [LastModified] [datetime] NULL,
     [Enabled] [bit] NOT NULL);";
     const string EMAIL_ATTACHMENT_TABLE = @"
     CREATE TABLE [EmailAttachment](
     [EmailAttachmentID] [uniqueidentifier] NOT NULL PRIMARY KEY,
     [EmailID] [uniqueidentifier] NOT NULL,
     [Name] [nvarchar](260) NOT NULL,
     [Inline] [bit] NOT NULL,
     [ContentID] [nvarchar](260) NOT NULL,
     [MIMEType] [nvarchar](260) NOT NULL,
     [Data] [image] NOT NULL,
     [Created] [datetime] NOT NULL,
     [LastModified] [datetime] NULL,
     [Enabled] [bit] NOT NULL)";
     using (var disposer = new AutoDisposer())
     {
         var conn = disposer.Add(new SqlCeConnection(this.ConnectionString));
         conn.Open();
         var cmd = disposer.Add(conn.CreateCommand());
         foreach (var script in new[] { EMAIL_TABLE, EMAIL_RECIPIENT_TABLE, EMAIL_ATTACHMENT_TABLE })
         {
             cmd.CommandText = script;
             Action<string> debug = s => System.Diagnostics.Debug.WriteLine(s);
             debug("Running script:");
             debug(script);
             debug("---------");
             cmd.ExecuteNonQuery();
         }
     }
 }
示例#18
0
        public void Dispose_ShouldDisposeOfRegisteredArticlesInReverseOrder()
        {
            //---------------Set up test pack-------------------
            var calls = new List<int>();
            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                disposer.Add(new SomeDisposableWithCallback(() => calls.Add(1)));
                disposer.Add(new SomeDisposableWithCallback(() => calls.Add(2)));

                //---------------Test Result -----------------------
            }
            Assert.AreEqual(2, calls.Count);
            Assert.AreEqual(2, calls[0]);
            Assert.AreEqual(1, calls[1]);
        }
示例#19
0
        public void ShouldPlayNicelyInParallel()
        {
            //---------------Set up test pack-------------------
            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                // localDb seems to take a little longer to spin up; reduced the parallel count to 10 to make
                //  running all tests viable
                Parallel.For(0, 10, i =>
                {
                    disposer.Add(new TempDBLocalDb());
                });

                //---------------Test Result -----------------------
            }
        }
示例#20
0
        public void Download_GivenDownloadInfoAndOutputFolder_WhenDownloadUninterrupted_ShouldDownloadFileToOutputFolder()
        {
            using (var disposer = new AutoDisposer())
            {
                var deleter = disposer.Add(new AutoDeleter());
                var tempFolder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
                var fileName = RandomValueGen.GetRandomAlphaNumericString(5, 10) + ".exe";
                deleter.Add(tempFolder);
                var expectedFile = Path.Combine(tempFolder, fileName);
                var server = Create();
                //---------------Set up test pack-------------------
                var url = server.GetFullUrlFor(fileName);
                var expectedBytes = RandomValueGen.GetRandomBytes(100, 200);
                server.AddFileHandler((processor, stream) =>
                {
                    if (processor.FullUrl == "/" + fileName)
                    {
                        return expectedBytes;
                    }
                    processor.WriteStatusHeader(404, "File not found");
                    return null;
                });

                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                Download(url, fileName, tempFolder);

                //---------------Test Result -----------------------
                Assert.IsTrue(File.Exists(expectedFile));
                CollectionAssert.AreEquivalent(expectedBytes, File.ReadAllBytes(expectedFile));
            }
        }
示例#21
0
        public void ShouldPlayNicelyInParallel()
        {
            //---------------Set up test pack-------------------
            using (var disposer = new AutoDisposer())
            {
                //---------------Assert Precondition----------------

                //---------------Execute Test ----------------------
                Parallel.For(0, 100, i =>
                    {
                        disposer.Add(new TempDBSqlCe());
                    });

                //---------------Test Result -----------------------
            }
        }
 public WebResponseReader(WebRequest request)
 {
     _request  = request;
     _disposer = new AutoDisposer();
 }
 public WebResponseReader(WebResponse response)
 {
     _response = response;
     _disposer = new AutoDisposer();
 }