Ejemplo n.º 1
0
        public async Task <FileSystemInfoContract> RenameItemAsync(RootName root, FileSystemId target, string newName, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var item = await AsyncFunc.RetryAsync <Item, ServiceException>(async() => await context.Client.Drive.Items[target.Value].Request().UpdateAsync(new Item()
            {
                Name = newName
            }), RETRIES);

            return(item.ToFileSystemInfoContract());
        }
Ejemplo n.º 2
0
        public async Task <DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContextAsync(root);

            var file = new GoogleFile()
            {
                Name = name, MimeType = MIME_TYPE_DIRECTORY, Parents = new[] { parent.Value }
            };
            var item = await AsyncFunc.RetryAsync <GoogleFile, GoogleApiException>(async() => await context.Service.Files.Create(file).AsDirectory().ExecuteAsync(), RETRIES);

            return(new DirectoryInfoContract(item.Id, item.Name, new DateTimeOffset(item.CreatedTime.Value), new DateTimeOffset(item.ModifiedTime.Value)));
        }
Ejemplo n.º 3
0
        public async Task <FileSystemInfoContract> RenameItemAsync(RootName root, FileSystemId target, string newName, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var request = context.Service.Files.Update(new GoogleFile()
            {
                Name = newName
            }, target.Value).AsFileSystem();
            var item = await AsyncFunc.RetryAsync <GoogleFile, GoogleApiException>(async() => await request.ExecuteAsync(), RETRIES);

            return(item.ToFileSystemInfoContract());
        }
Ejemplo n.º 4
0
        public FrmMain()
        {
            InitializeComponent();

            answer                  = new AsyncFunc <string[], MessageInfo>(RunProgerss);
            answer.Completed       += answer_Completed;
            answer.ProgressChanged += answer_ProgressChanged;
            BarTime.Step            = 1;
            BarTime.Value           = 0;
            BarCount.Value          = 0;
            CmdStop.Enabled         = false;
        }
Ejemplo n.º 5
0
        public async Task <FileSystemInfoContract> RenameItemAsync(RootName root, FileSystemId target, string newName, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var itemReference = ODConnection.ItemReferenceForItemId(target.Value, context.Drive.Id);
            var item          = await AsyncFunc.Retry <ODItem, ODException>(async() => await context.Connection.PatchItemAsync(itemReference, new ODItem()
            {
                Name = newName
            }), RETRIES);

            return(item.ToFileSystemInfoContract());
        }
Ejemplo n.º 6
0
        public async Task <DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContextAsync(root);

            var folder = new Item()
            {
                Folder = new Folder()
            };
            var item = await AsyncFunc.RetryAsync <Item, ServiceException>(async() => await context.Client.Drive.Items[parent.Value].ItemWithPath(name).Request().CreateAsync(folder), RETRIES);

            return(new DirectoryInfoContract(item.Id, item.Name, item.CreatedDateTime ?? DateTimeOffset.FromFileTime(0), item.LastModifiedDateTime ?? DateTimeOffset.FromFileTime(0)));
        }
Ejemplo n.º 7
0
        public FrmMain()
        {
            InitializeComponent();

            answer = new AsyncFunc<string[], MessageInfo>(RunProgerss);
            answer.Completed += answer_Completed;
            answer.ProgressChanged += answer_ProgressChanged;
            BarTime.Step = 1;
            BarTime.Value = 0;
            BarCount.Value = 0;
            CmdStop.Enabled = false;
        }
Ejemplo n.º 8
0
        public async Task <IEnumerable <FileSystemInfoContract> > GetChildItemAsync(RootName root, DirectoryId parent)
        {
            var context = await RequireContext(root);

            var childReferences = await AsyncFunc.Retry <ChildList, GoogleApiException>(async() => await context.Service.Children.List(parent.Value).ExecuteAsync(), RETRIES);

            var items = childReferences.Items.Select(async c => await AsyncFunc.Retry <GoogleFile, GoogleApiException>(async() => await context.Service.Files.Get(c.Id).ExecuteAsync(), RETRIES)).ToArray();

            Task.WaitAll(items);

            return(items.Select(i => i.Result.ToFileSystemInfoContract()));
        }
Ejemplo n.º 9
0
        public async Task From_05_ThenInvokeAsync_ExpectResultOfSourceFunc(
            StructType sourceFuncResult)
        {
            var actual = AsyncFunc.From <int?, RecordType, RefType?, string, object, StructType>(
                (_, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: false);

            var actualResult = await actual.InvokeAsync(
                MinusFifteen, PlusFifteenIdSomeStringNameRecord, null, SomeString, new(), cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 10
0
        public async Task <DriveInfoContract> GetDriveAsync(RootName root, string apiKey, IDictionary <string, string> parameters)
        {
            var context = await RequireContextAsync(root, apiKey);

            var item = await AsyncFunc.RetryAsync <About, GoogleApiException>(async() => await context.Service.About.Get().ExecuteAsync(), RETRIES);

            var usedSpace = item.QuotaBytesUsed.HasValue || item.QuotaBytesUsedInTrash.HasValue
                ? (item.QuotaBytesUsed ?? 0) + (item.QuotaBytesUsedInTrash ?? 0)
                : (long?)null;
            var freeSpace = item.QuotaBytesTotal.HasValue ? item.QuotaBytesTotal.Value - usedSpace : (long?)null;

            return(new DriveInfoContract(item.Name, freeSpace, usedSpace));
        }
Ejemplo n.º 11
0
        public async Task From_06_ThenInvokeAsync_ExpectResultOfSourceFunc(
            string?sourceFuncResult)
        {
            var actual = AsyncFunc.From <RefType, string, StructType?, object, RecordType, int, string?>(
                (_, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken();

            var actualResult = await actual.InvokeAsync(
                MinusFifteenIdRefType, null !, new(), new(), PlusFifteenIdLowerSomeStringNameRecord, int.MaxValue, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 12
0
        public async Task <FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var destinationPathReference = new ItemReference {
                Id = destination.Value
            };
            var item = await AsyncFunc.RetryAsync <Item, ServiceException>(async() => await context.Client.Drive.Items[source.Value].Request().UpdateAsync(new Item {
                ParentReference = destinationPathReference, Name = moveName
            }), RETRIES);

            return(item.ToFileSystemInfoContract());
        }
Ejemplo n.º 13
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (content.Length == 0)
            {
                return(new ProxyFileInfoContract(name));
            }

            var context = await RequireContextAsync(root);

            var item = await AsyncFunc.RetryAsync <Item, OneDriveException>(async() => await context.Client.Drive.Items[parent.Value].ItemWithPath(name).Content.Request().PutAsync <Item>(content), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime ?? DateTimeOffset.FromFileTime(0), item.LastModifiedDateTime ?? DateTimeOffset.FromFileTime(0), item.Size ?? -1, item.File.Hashes.Sha1Hash.ToLowerInvariant()));
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Executes provided function as critical section, acquiring the specified lock, executing a statement, and then
        /// releasing the lock.
        /// </summary>
        /// <param name="lock">Synchronization object to use.</param>
        /// <param name="func">Asynchronous function to execute.</param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>
        /// Task which is completed after the operation has been performed and the lock has been released.
        /// </returns>
        public static async Task <T> SynchronizedAsync <T>(this ILockAsync @lock, AsyncFunc <T> func, CancellationToken cancellationToken)
        {
            await @lock.LockAsync(cancellationToken);

            try
            {
                return(await func(cancellationToken));
            }
            finally
            {
                @lock.Release();
            }
        }
Ejemplo n.º 15
0
        public async Task From_14_ThenInvokeAsync_ExpectResultOfSourceFunc(
            StructType sourceFuncResult)
        {
            var actual = AsyncFunc.From <int?, RefType, object?, decimal, RecordType?, StructType, long, RefType, int, DateTimeKind, object, long, RefType, int, StructType>(
                (_, _, _, _, _, _, _, _, _, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: false);

            var actualResult = await actual.InvokeAsync(
                PlusFifteen, MinusFifteenIdRefType, new(), decimal.MinusOne, MinusFifteenIdSomeStringNameRecord, SomeTextStructType, long.MinValue, PlusFifteenIdRefType, int.MaxValue, DateTimeKind.Utc, null !, long.MinValue, MinusFifteenIdRefType, int.MinValue, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 16
0
        public async Task From_10_ThenInvokeAsync_ExpectResultOfSourceFunc(
            int?sourceFuncResult)
        {
            var actual = AsyncFunc.From <object?, StructType?, RefType?, RecordType?, int, string?, long, int?, RefType, StructType?, int?>(
                (_, _, _, _, _, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = default(CancellationToken);

            var actualResult = await actual.InvokeAsync(
                null, SomeTextStructType, PlusFifteenIdRefType, MinusFifteenIdSomeStringNameRecord, MinusFifteen, TabString, long.MinValue, Zero, MinusFifteenIdRefType, LowerSomeTextStructType, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 17
0
        public Task <T> ExecuteAsync <T>(AsyncFunc <T> action)
        {
            if (_logger.IsEnabled(LogLevel.Information))
            {
                var sw     = Stopwatch.StartNew();
                var result = action.Invoke();
                sw.Stop();

                return(result);
            }

            return(action.Invoke());
        }
Ejemplo n.º 18
0
        public async Task <bool> SetContentAsync(RootName root, FileId target, Stream content, IProgress <ProgressValue> progress, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var itemReference = await AsyncFunc.Retry <GoogleFile, GoogleApiException>(async() => await context.Service.Files.Get(target.Value).ExecuteAsync(), RETRIES);

            var update = context.Service.Files.Update(itemReference, target.Value, content, itemReference.MimeType);

            update.ProgressChanged += p => progress.Report(new ProgressValue((int)p.BytesSent, (int)content.Length));
            await AsyncFunc.Retry <IUploadProgress, GoogleApiException>(async() => await update.UploadAsync(), RETRIES);

            return(true);
        }
Ejemplo n.º 19
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var itemReference = ODConnection.ItemReferenceForItemId(parent.Value, context.Drive.Id);
            var uploadOptions = progress != null ? new ItemUploadOptions()
            {
                ProgressReporter = (complete, transfered, total) => progress.Report(new ProgressValue(complete, (int)transfered, (int)total))
            } : ItemUploadOptions.Default;
            var item = await AsyncFunc.Retry <ODItem, ODException>(async() => await context.Connection.PutNewFileToParentItemAsync(itemReference, name, content, uploadOptions), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.CreatedDateTime, item.LastModifiedDateTime, item.Size, item.File.Hashes.Sha1.ToLowerInvariant()));
        }
Ejemplo n.º 20
0
        public async Task From_15_ThenInvokeAsync_ExpectResultOfSourceFunc(
            RefType?sourceFuncResult)
        {
            var actual = AsyncFunc.From <StructType?, string, long, object?, int, RefType, RecordType, decimal, string?, byte, object, RefType?, object?, decimal?, byte?, RefType?>(
                (_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: true);

            var actualResult = await actual.InvokeAsync(
                LowerSomeTextStructType, SomeString, long.MaxValue, null, MinusFifteen, PlusFifteenIdRefType, PlusFifteenIdLowerSomeStringNameRecord, decimal.One, ThreeWhiteSpacesString, byte.MaxValue, new { Name = SomeString }, ZeroIdRefType, new(), decimal.MaxValue, null, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 21
0
        public async Task <bool> SetContentAsync(RootName root, FileId target, Stream content, IProgress <ProgressValue> progress, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var itemReference = ODConnection.ItemReferenceForItemId(target.Value, context.Drive.Id);
            var uploadOptions = progress != null ? new ItemUploadOptions()
            {
                ProgressReporter = (complete, transfered, total) => progress.Report(new ProgressValue(complete, (int)transfered, (int)total))
            } : ItemUploadOptions.Default;
            var item = await AsyncFunc.Retry <ODItem, ODException>(async() => await context.Connection.PutContentsAsync(itemReference, content, uploadOptions), RETRIES);

            return(true);
        }
Ejemplo n.º 22
0
        public async Task From_12_ThenInvokeAsync_ExpectResultOfSourceFunc(
            RecordType?sourceFuncResult)
        {
            var actual = AsyncFunc.From <int, object, RecordType?, RefType, object, string?, string, decimal, byte, RecordType, StructType, object, RecordType?>(
                (_, _, _, _, _, _, _, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: true);

            var actualResult = await actual.InvokeAsync(
                PlusFifteen, new { Name = UpperSomeString }, null, PlusFifteenIdRefType, null !, SomeString, EmptyString, decimal.MinusOne, byte.MaxValue, MinusFifteenIdSomeStringNameRecord, LowerSomeTextStructType, SomeTextStructType, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 23
0
        public async Task From_07_ThenInvokeAsync_ExpectResultOfSourceFunc(
            RecordType?sourceFuncResult)
        {
            var actual = AsyncFunc.From <string?, int, RefType?, object, RecordType, string, StructType, RecordType?>(
                (_, _, _, _, _, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: true);

            var actualResult = await actual.InvokeAsync(
                SomeString, PlusFifteen, MinusFifteenIdRefType, SomeTextStructType, null !, EmptyString, LowerSomeTextStructType, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 24
0
        public async Task From_03_ThenInvokeAsync_ExpectResultOfSourceFunc(
            StructType sourceFuncResult)
        {
            var actual = AsyncFunc.From <string?, RefType, RecordType?, StructType>(
                (_, _, _, _) => Task.FromResult(sourceFuncResult));

            var cancellationToken = new CancellationToken(canceled: false);

            var actualResult = await actual.InvokeAsync(
                LowerSomeString, ZeroIdRefType, PlusFifteenIdLowerSomeStringNameRecord, cancellationToken);

            Assert.Equal(sourceFuncResult, actualResult);
        }
Ejemplo n.º 25
0
        public async Task <FileSystemInfoContract> MoveItemAsync(RootName root, FileSystemId source, string moveName, DirectoryId destination, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContext(root);

            var itemReference            = ODConnection.ItemReferenceForItemId(source.Value, context.Drive.Id);
            var destinationPathReference = ODConnection.ItemReferenceForItemId(destination.Value, context.Drive.Id);
            var item = await AsyncFunc.Retry <ODItem, ODException>(async() => await context.Connection.PatchItemAsync(itemReference, new ODItem()
            {
                ParentReference = destinationPathReference, Name = moveName
            }), RETRIES);

            return(item.ToFileSystemInfoContract());
        }
Ejemplo n.º 26
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (content.Length == 0)
            {
                return(new ProxyFileInfoContract(name));
            }

            var context = await RequireContextAsync(root);

            var stream = progress != null ? new ProgressStream(content, progress) : content;
            var item   = await AsyncFunc.RetryAsync <FileSystem, ServerException>(async() => await context.Client.FileSystemManager.UploadNewFileStreamAsync(parent.Value, name, stream, true), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.DateLastSynced, FileSystemExtensions.Later(item.DateLastSynced, item.ModifiedTime), item.Size, null));
        }
Ejemplo n.º 27
0
        public async Task <bool> ClearContentAsync(RootName root, FileId target, Func <FileSystemInfoLocator> locatorResolver)
        {
            if (locatorResolver == null)
            {
                throw new ArgumentNullException(nameof(locatorResolver));
            }

            var context = await RequireContextAsync(root);

            var locator = locatorResolver();
            await AsyncFunc.RetryAsync <BoxFile, BoxException>(async() => await context.Client.FilesManager.UploadNewVersionAsync(locator.Name, target.Value, Stream.Null), RETRIES);

            return(true);
        }
Ejemplo n.º 28
0
        public async Task <bool> SetContentAsync(RootName root, FileId target, Stream content, IProgress <ProgressValue> progress, Func <FileSystemInfoLocator> locatorResolver)
        {
            var context = await RequireContextAsync(root);

            var update = context.Service.Files.Update(new GoogleFile(), target.Value, content, MIME_TYPE_FILE);

            if (progress != null)
            {
                update.ProgressChanged += p => progress.Report(new ProgressValue((int)p.BytesSent, (int)content.Length));
            }
            await AsyncFunc.RetryAsync <IUploadProgress, GoogleApiException>(async() => await update.UploadAsync(), RETRIES);

            return(true);
        }
Ejemplo n.º 29
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            if (content.Length == 0)
            {
                return(new ProxyFileInfoContract(name));
            }

            var context = await RequireContextAsync(root);

            var stream = progress != null ? new ProgressStream(content, progress) : content;
            var item   = await AsyncFunc.RetryAsync <pCloudFile, pCloudException>(async() => await context.Client.UploadFileAsync(stream, ToId(parent), WebUtility.UrlEncode(name), CancellationToken.None), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.Created, item.Modified, item.Size, null));
        }
Ejemplo n.º 30
0
        public async Task <FileInfoContract> NewFileItemAsync(RootName root, DirectoryId parent, string name, Stream content, IProgress <ProgressValue> progress)
        {
            var context = await RequireContext(root);

            var request = new BoxFileRequest()
            {
                Name = name, Parent = new BoxRequestEntity()
                {
                    Id = parent.Value
                }
            };
            var item = await AsyncFunc.Retry <BoxFile, BoxException>(async() => await context.Client.FilesManager.UploadAsync(request, new ProgressStream(content, progress), boxFileFields), RETRIES);

            return(new FileInfoContract(item.Id, item.Name, item.CreatedAt.Value, item.ModifiedAt.Value, item.Size.Value, item.Sha1.ToLowerInvariant()));
        }
Ejemplo n.º 31
0
        public async Task <DirectoryInfoContract> NewDirectoryItemAsync(RootName root, DirectoryId parent, string name)
        {
            var context = await RequireContext(root);

            var request = new BoxFolderRequest()
            {
                Name = name, Parent = new BoxRequestEntity()
                {
                    Id = parent.Value
                }
            };
            var item = await AsyncFunc.Retry <BoxFolder, BoxException>(async() => await context.Client.FoldersManager.CreateAsync(request, boxFolderFields), RETRIES);

            return(new DirectoryInfoContract(item.Id, item.Name, item.CreatedAt.Value, item.ModifiedAt.Value));
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Method that creates the various task using the FRomAsync methods
        /// </summary>
        /// <returns></returns>
        private AsyncWork RunAPMTest()
        {
            switch (_api)
            {
                case API.APM:
                    if (_sourceType == TaskType.Task)
                    {
                        if (_fromAsyncType == TaskType.Task)
                        {
                            AsyncAction action = new AsyncAction(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync((Func<AsyncCallback, object, IAsyncResult>)null, action.EndInvoke, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync(action.BeginInvoke, null, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync(action.BeginInvoke, action.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync(action.BeginInvoke, action.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM");
                                }
                            }

                            return action;
                        }
                        else // must be FromAsync type of TaskType_FromAsync.TaskT
                        {
                            AsyncFunc func = new AsyncFunc(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync<ReadOnlyCollection<object>>((Func<AsyncCallback, object, IAsyncResult>)null, func.EndInvoke, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke, null, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke, func.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke, func.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM");
                                }
                            }

                            return func;
                        }
                    }
                    else // must be TaskType_FromAsync.TaskT
                    {
                        AsyncFunc func = new AsyncFunc(_errorCase == ErrorCase.Throwing);

                        if (_errorCase == ErrorCase.NullBegin)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync((Func<AsyncCallback, object, IAsyncResult>)null, func.EndInvoke, null);
                        else if (_errorCase == ErrorCase.NullEnd)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke, null, null);
                        else
                        {
                            switch (_overloadChoice)
                            {
                                case OverloadChoice.None:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke, func.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                    break;

                                case OverloadChoice.WithTaskOption:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke, func.EndInvoke, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException("invalid overloadChoice for APM");
                            }
                        }

                        return func;
                    }

                case API.APM_T:

                    _expectedInputs.Add(TestInteger);

                    if (_sourceType == TaskType.Task)
                    {
                        if (_fromAsyncType == TaskType.Task)
                        {
                            AsyncAction<int> action1 = new AsyncAction<int>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync((Func<int, AsyncCallback, object, IAsyncResult>)null, action1.EndInvoke, TestInteger, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync(action1.BeginInvoke, null, TestInteger, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync(action1.BeginInvoke, action1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync(action1.BeginInvoke, action1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T");
                                }
                            }

                            return action1;
                        }
                        else // must be FromAsync type of TaskType_FromAsync.TaskT
                        {
                            AsyncFunc<int> func1 = new AsyncFunc<int>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync<int, ReadOnlyCollection<object>>((Func<int, AsyncCallback, object, IAsyncResult>)null, func1.EndInvoke, TestInteger, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync<int, ReadOnlyCollection<object>>(func1.BeginInvoke, null, TestInteger, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync<int, ReadOnlyCollection<object>>(func1.BeginInvoke, func1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync<int, ReadOnlyCollection<object>>(func1.BeginInvoke, func1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T");
                                }
                            }

                            return func1;
                        }
                    }
                    else // must be TaskType_FromAsync.TaskT
                    {
                        AsyncFunc<int> func1 = new AsyncFunc<int>(_errorCase == ErrorCase.Throwing);

                        if (_errorCase == ErrorCase.NullBegin)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync((Func<int, AsyncCallback, object, IAsyncResult>)null, func1.EndInvoke, TestInteger, null);
                        else if (_errorCase == ErrorCase.NullEnd)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync(func1.BeginInvoke, null, TestInteger, null);
                        else
                        {
                            switch (_overloadChoice)
                            {
                                case OverloadChoice.None:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func1.BeginInvoke, func1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                    break;

                                case OverloadChoice.WithTaskOption:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func1.BeginInvoke, func1.EndInvoke, TestInteger, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T");
                            }
                        }

                        return func1;
                    }


                case API.APM_T2: //Two variables

                    _expectedInputs.Add(TestInteger);
                    _expectedInputs.Add(TestDouble);

                    if (_sourceType == TaskType.Task)
                    {
                        if (_fromAsyncType == TaskType.Task)
                        {
                            AsyncAction<int, double> action2 = new AsyncAction<int, double>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync((Func<int, double, AsyncCallback, object, IAsyncResult>)null, action2.EndInvoke, TestInteger, TestDouble, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync(action2.BeginInvoke, null, TestInteger, TestDouble, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync(action2.BeginInvoke, action2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync(action2.BeginInvoke, action2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T2");
                                }
                            }

                            return action2;
                        }
                        else // must be FromAsync type of TaskType_FromAsync.TaskT
                        {
                            AsyncFunc<int, double> func2 = new AsyncFunc<int, double>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync<int, double, ReadOnlyCollection<object>>((Func<int, double, AsyncCallback, object, IAsyncResult>)null, func2.EndInvoke, TestInteger, TestDouble, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync<int, double, ReadOnlyCollection<object>>(func2.BeginInvoke, null, TestInteger, TestDouble, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync<int, double, ReadOnlyCollection<object>>(func2.BeginInvoke, func2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync<int, double, ReadOnlyCollection<object>>(func2.BeginInvoke, func2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T2");
                                }
                            }

                            return func2;
                        }
                    }
                    else // must be TaskType_FromAsync.TaskT
                    {
                        AsyncFunc<int, double> func2 = new AsyncFunc<int, double>(_errorCase == ErrorCase.Throwing);

                        if (_errorCase == ErrorCase.NullBegin)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync((Func<int, double, AsyncCallback, object, IAsyncResult>)null, func2.EndInvoke, TestInteger, TestDouble, null);
                        else if (_errorCase == ErrorCase.NullEnd)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync(func2.BeginInvoke, null, TestInteger, TestDouble, null);
                        else
                        {
                            switch (_overloadChoice)
                            {
                                case OverloadChoice.None:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func2.BeginInvoke, func2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                    break;

                                case OverloadChoice.WithTaskOption:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func2.BeginInvoke, func2.EndInvoke, TestInteger, TestDouble, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T2");
                            }
                        }

                        return func2;
                    }


                case API.APM_T3:

                    _expectedInputs.Add(TestInteger);
                    _expectedInputs.Add(TestDouble);
                    _expectedInputs.Add(TestBoolean);

                    if (_sourceType == TaskType.Task)
                    {
                        if (_fromAsyncType == TaskType.Task)
                        {
                            AsyncAction<int, double, bool> action3 = new AsyncAction<int, double, bool>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync((Func<int, double, bool, AsyncCallback, object, IAsyncResult>)null, action3.EndInvoke, TestInteger, TestDouble, TestBoolean, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync(action3.BeginInvoke, null, TestInteger, TestDouble, TestBoolean, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync(action3.BeginInvoke, action3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync(action3.BeginInvoke, action3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T3");
                                }
                            }

                            return action3;
                        }
                        else // must be FromAsync type of TaskType_FromAsync.TaskT
                        {
                            AsyncFunc<int, double, bool> func3 = new AsyncFunc<int, double, bool>(_errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync<int, double, bool, ReadOnlyCollection<object>>((Func<int, double, bool, AsyncCallback, object, IAsyncResult>)null, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, null);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync<int, double, bool, ReadOnlyCollection<object>>(func3.BeginInvoke, null, TestInteger, TestDouble, TestBoolean, null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync<int, double, bool, ReadOnlyCollection<object>>(func3.BeginInvoke, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync<int, double, bool, ReadOnlyCollection<object>>(func3.BeginInvoke, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T3");
                                }
                            }

                            return func3;
                        }
                    }
                    else // must be TaskType_FromAsync.TaskT
                    {
                        AsyncFunc<int, double, bool> func3 = new AsyncFunc<int, double, bool>(_errorCase == ErrorCase.Throwing);

                        if (_errorCase == ErrorCase.NullBegin)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync((Func<int, double, bool, AsyncCallback, object, IAsyncResult>)null, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, null);
                        else if (_errorCase == ErrorCase.NullEnd)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync(func3.BeginInvoke, null, TestInteger, TestDouble, TestBoolean, null);
                        else
                        {
                            switch (_overloadChoice)
                            {
                                case OverloadChoice.None:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func3.BeginInvoke, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None });
                                    break;

                                case OverloadChoice.WithTaskOption:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func3.BeginInvoke, func3.EndInvoke, TestInteger, TestDouble, TestBoolean, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }, TestOption);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException("invalid overloadChoice for APM_T3");
                            }
                        }

                        return func3;
                    }

                case API.IAsyncResult:

                    // put the current params into the expectedInputs
                    object[] inputs = new object[] { _api, _sourceType, _errorCase };
                    _expectedInputs.AddRange(inputs);

                    if (_sourceType == TaskType.Task)
                    {
                        if (_fromAsyncType == TaskType.Task)
                        {
                            AsyncAction action = new AsyncAction(inputs, _errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync((IAsyncResult)null, action.EndInvoke);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync(action.BeginInvoke(null, null), null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync(action.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None }), action.EndInvoke);
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync(action.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }), action.EndInvoke, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for IAsyncResult");
                                }
                            }

                            return action;
                        }
                        else // must be FromAsync type of TaskType_FromAsync.TaskT
                        {
                            AsyncFunc func = new AsyncFunc(inputs, _errorCase == ErrorCase.Throwing);

                            if (_errorCase == ErrorCase.NullBegin)
                                Task.Factory.FromAsync<ReadOnlyCollection<object>>((IAsyncResult)null, func.EndInvoke);
                            else if (_errorCase == ErrorCase.NullEnd)
                                Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke(null, null), null);
                            else
                            {
                                switch (_overloadChoice)
                                {
                                    case OverloadChoice.None:
                                        _task = Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None }), func.EndInvoke);
                                        break;

                                    case OverloadChoice.WithTaskOption:
                                        _task = Task.Factory.FromAsync<ReadOnlyCollection<object>>(func.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }), func.EndInvoke, TestOption);
                                        break;

                                    default:
                                        throw new ArgumentOutOfRangeException("invalid overloadChoice for IAsyncResult");
                                }
                            }

                            return func;
                        }
                    }
                    else // must be TaskType_FromAsync.TaskT
                    {
                        AsyncFunc func = new AsyncFunc(inputs, _errorCase == ErrorCase.Throwing);

                        if (_errorCase == ErrorCase.NullBegin)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync((IAsyncResult)null, func.EndInvoke);
                        else if (_errorCase == ErrorCase.NullEnd)
                            Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke(null, null), null);
                        else
                        {
                            switch (_overloadChoice)
                            {
                                case OverloadChoice.None:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TaskCreationOptions.None }), func.EndInvoke);
                                    break;

                                case OverloadChoice.WithTaskOption:
                                    _task = Task<ReadOnlyCollection<object>>.Factory.FromAsync(func.BeginInvoke(null, new TaskOptionAndScheduler { Scheduler = TaskScheduler.Default, Option = TestOption }), func.EndInvoke, TestOption);
                                    break;

                                default:
                                    throw new ArgumentOutOfRangeException("invalid overloadChoice for IAsyncResult");
                            }
                        }

                        return func;
                    }


                default:
                    throw new ArgumentException("unknown api to test");
            }
        }
Ejemplo n.º 33
0
 internal static extern int m_CallFunctionAsync(AsyncFunc callback, IntPtr arg);
Ejemplo n.º 34
0
 protected int CallFunctionAsync(AsyncFunc callback, IntPtr arg)
 {
     return Plugin.m_CallFunctionAsync(callback, arg);
 }