Пример #1
0
    public void UploadDownloadOne()
    {
        var fileId     = GenerateFileId();
        var readStream = new ByteArrayStream(16 * 1024);

        var client = new Client(KTestHost, KTestPort);

        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));
    }
Пример #2
0
    public void ResetDownloadFinishedEventHandler()
    {
        var fileId     = GenerateFileId();
        var readStream = new ByteArrayStream(16 * 1024);

        var client = new Client(KTestHost, KTestPort);

        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));
    }
Пример #3
0
    public void DonwloadFileNotFound()
    {
        var client = new Client(KTestHost, KTestPort);

        client.Connect();

        var fileId = FileId.From(new byte[16], new byte[16]);

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

        client.QueueDownload(downloadItem);

        Exception err = null;

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

        mre.WaitOne(500);

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