Exemplo n.º 1
0
        public void FileDownloadItem()
        {
            var fileId     = GenerateFileId();
            var readStream = new ByteArrayStream(128 * 1024);

            var client = new Client(KTestHost, TestPort);

            client.Connect();

            client.BeginTransaction(fileId);
            client.Upload(FileType.Asset, readStream);
            client.EndTransaction();
            Thread.Sleep(50); // give the server a little time to finish the transaction

            var targetFile   = Path.GetTempFileName();
            var downloadItem = new FileDownloadItem(fileId, FileType.Asset, targetFile);

            var       mre = new ManualResetEvent(false);
            Exception err = null;

            client.DownloadFinished += (sender, args) =>
            {
                try
                {
                    Assert.AreEqual(DownloadResult.Success, args.Result);
                    Assert.AreEqual(args.DownloadItem.Id, fileId);
                    Assert.IsTrue(File.Exists(targetFile));

                    var fileBytes = File.ReadAllBytes(targetFile);
                    Assert.IsTrue(Util.ByteArraysAreEqual(readStream.BackingBuffer, fileBytes));
                }
                catch (Exception e)
                {
                    err = e;
                }
                finally
                {
                    if (File.Exists(targetFile))
                    {
                        File.Delete(targetFile);
                    }

                    mre.Set();
                }
            };

            client.QueueDownload(downloadItem);
            Assert.IsTrue(mre.WaitOne(2000));

            if (err != null)
            {
                throw err;
            }
        }
Exemplo n.º 2
0
        public void UploadDownloadOne()
        {
            var fileId     = GenerateFileId();
            var readStream = new ByteArrayStream(16 * 1024);

            var client = new Client(KTestHost, TestPort);

            client.Connect();

            client.BeginTransaction(fileId);
            client.Upload(FileType.Asset, readStream);
            client.EndTransaction();
            Thread.Sleep(50); // give the server a little time to finish the transaction

            var downloadItem = new TestDownloadItem(fileId, FileType.Asset);

            client.QueueDownload(downloadItem);

            Exception err = null;
            var       mre = new ManualResetEvent(false);

            client.DownloadFinished += (sender, args) =>
            {
                try
                {
                    Assert.AreEqual(0, args.DownloadQueueLength);
                    Assert.AreEqual(DownloadResult.Success, args.Result);
                    Assert.AreEqual(fileId, args.DownloadItem.Id);
                }
                catch (Exception e)
                {
                    err = e;
                }
                finally
                {
                    mre.Set();
                }
            };

            Assert.IsTrue(mre.WaitOne(2000));

            if (err != null)
            {
                throw err;
            }

            Assert.IsTrue(Util.ByteArraysAreEqual(readStream.BackingBuffer, downloadItem.Bytes));
        }
Exemplo n.º 3
0
        public void TransactionIsolation()
        {
            var fileId     = GenerateFileId();
            var readStream = new ByteArrayStream(16 * 1024);

            var client = new Client(KTestHost, TestPort);

            client.Connect();

            Assert.Throws <TransactionIsolationException>(() => client.Upload(FileType.Asset, readStream));
            Assert.Throws <TransactionIsolationException>(() => client.EndTransaction());

            // Back-to-back begin transactions are allowed
            client.BeginTransaction(fileId);
            Assert.DoesNotThrow(() => client.BeginTransaction(fileId));
        }
Exemplo n.º 4
0
        public void ResetDownloadFinishedEventHandler()
        {
            var fileId     = GenerateFileId();
            var readStream = new ByteArrayStream(16 * 1024);

            var client = new Client(KTestHost, TestPort);

            client.Connect();

            client.BeginTransaction(fileId);
            client.Upload(FileType.Asset, readStream);
            client.EndTransaction();
            Thread.Sleep(50);

            var downloadItem = new TestDownloadItem(fileId, FileType.Asset);

            // Add two listeners that will assert if called
            client.DownloadFinished += (sender, args) => { Debug.Assert(false); };
            client.DownloadFinished += (sender, args) => { Debug.Assert(false); };

            // Clear the listeners so they will not be called
            client.ResetDownloadFinishedEventHandler();

            client.QueueDownload(downloadItem);

            var mre = new ManualResetEvent(false);

            ThreadPool.QueueUserWorkItem(state =>
            {
                while (client.DownloadQueueLength > 0)
                {
                    Thread.Sleep(0);
                }

                mre.Set();
            });

            Assert.IsTrue(mre.WaitOne(2000));
        }
Exemplo n.º 5
0
 public Stream GetWriteStream(long size)
 {
     return(m_writeStream ?? (m_writeStream = new ByteArrayStream(size)));
 }
Exemplo n.º 6
0
        public void DownloadMany()
        {
            const int fileCount = 5;

            var fileIds     = new FileId[fileCount];
            var fileStreams = new ByteArrayStream[fileCount];

            var client = new Client(KTestHost, TestPort);

            client.Connect();

            // Upload files
            var rand = new Random();

            for (var i = 0; i < fileCount; i++)
            {
                fileIds[i]     = GenerateFileId();
                fileStreams[i] = new ByteArrayStream(rand.Next(64 * 1024, 128 * 1024));

                client.BeginTransaction(fileIds[i]);
                client.Upload(FileType.Asset, fileStreams[i]);
                client.EndTransaction();
            }

            Thread.Sleep(50);

            // Download
            var       receivedCount = 0;
            Exception err           = null;
            var       mre           = new ManualResetEvent(false);

            client.DownloadFinished += (sender, args) =>
            {
                try
                {
                    Assert.AreEqual(args.Result, DownloadResult.Success);
                    Assert.AreEqual(args.DownloadItem.Id, fileIds[receivedCount]);

                    var downloadItem = (TestDownloadItem)args.DownloadItem;
                    Assert.IsTrue(Util.ByteArraysAreEqual(fileStreams[receivedCount].BackingBuffer, downloadItem.Bytes));

                    receivedCount++;
                    Assert.AreEqual(fileCount - receivedCount, args.DownloadQueueLength);
                }
                catch (Exception e)
                {
                    err = e;
                }
                finally
                {
                    if (err != null || receivedCount == fileCount)
                    {
                        mre.Set();
                    }
                }
            };

            for (var i = 0; i < fileCount; i++)
            {
                client.QueueDownload(new TestDownloadItem(fileIds[i], FileType.Asset));
            }

            Assert.AreEqual(fileCount, client.DownloadQueueLength);

            Assert.IsTrue(mre.WaitOne(2000));

            if (err != null)
            {
                throw err;
            }

            Assert.AreEqual(fileCount, receivedCount);
        }