상속: IAsyncDisposable
예제 #1
0
        private void FlushInternal()
        {
            using (Defer.Call(() => Reset())) // even in the presence of errors
            {
                try
                {
                    // add current cell if not empty
                    if (cell.size > 0)
                    {
                        if (endChar != 0)
                        {
                            // inside escape - terminate it even if incomplete
                            EndEscape();
                        }
                        TerminateCell(false);
                    }

                    // format contents of buffer
                    Format(0, 0, lines.Length);
                }
                catch (Exception ex)
                {
                    HandlePanic(ex, "Flush");
                }
            }
        }
예제 #2
0
        public ProductType(Defer <IProductReviewRepository> reviewRepository,
                           IDataLoaderContextAccessor dataLoaderAccessor)
        {
            _productReviewRepository = reviewRepository;

            Field(t => t.Id);
            Field(t => t.Name);
            Field(t => t.Description);
            Field(t => t.IntroducedAt).Description("When the product was first introduced in the catalog");
            Field(t => t.PhotoFileName).Description("The file name of the photo so the client can render it");
            Field(t => t.Price);
            Field(t => t.Rating).Description("The (max 5) star customer rating");
            Field(t => t.Stock);

            Field <ProductTypeEnumType>("Type", "The type of product");

            Field <ListGraphType <ProductReviewType> >(
                "reviews",
                resolve: context =>
            {
                //var user = (ClaimsPrincipal) context.UserContext;
                var loader =
                    dataLoaderAccessor.Context.GetOrAddCollectionBatchLoader <int, ProductReview>(
                        "GetReviewsByProductId", _productReviewRepository.Value.GetForProducts);
                return(loader.LoadAsync(context.Source.Id));
            });
        }
예제 #3
0
    public async Task RunTest()
    {
        var defer  = new Defer();
        var number = 0;

        async Task AsyncFunc()
        {
            number++;
            await Task.Delay(10);

            number++;
            if (number == 10)
            {
                defer.Resolve();
            }
        }

        var worker = new AsyncTaskWorker(3);
        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await worker.Run(AsyncFunc);

        await defer.Wait();

        Assert.AreEqual(number, 10);
    }
예제 #4
0
        /// <summary>
        /// Return a iterator block that executes the monad. See <see cref="IMonad{T}.Do"/>.
        /// </summary>
        public IEnumerator Do()
        {
            using (var defer = new Defer())
            {
                var thread = new System.Threading.Thread(() =>
                {
                    try
                    {
                        Result = _func();
                    }
                    catch (Exception e)
                    {
                        Error = e;
                    }
                });

                thread.Start();
                defer.Add(() =>
                {
                    if (thread.IsAlive)
                    {
                        thread.Abort();
                    }
                });

                while (thread.IsAlive)
                {
                    yield return(null);
                }
            }
        }
예제 #5
0
        private int DeferredReturn(int n)
        {
            using (Defer.Call(() => n++))
            {
                // We cannot place the return here as its arguments would
                // get evaluated BEFORE the deferred actions are run
            }

            return(n);
        }
예제 #6
0
        public void TestDeferArgEval()
        {
            var i = 0;

            using (Defer.Call(i, (x) => Assert.AreEqual(0, x)))
            {
                i++;
                return;
            }
        }
예제 #7
0
 public CarvedRockSubscription(Defer <ReviewMessageService> messageService)
 {
     Name = "Subscription";
     AddField(new EventStreamFieldType
     {
         Name       = "reviewAdded",
         Type       = typeof(ReviewAddedMessageType),
         Resolver   = new FuncFieldResolver <ReviewAddedMessage>(c => c.Source as ReviewAddedMessage),
         Subscriber = new EventStreamResolver <ReviewAddedMessage>(c => messageService.Value.GetMessages())
     });
 }
예제 #8
0
    public async Task DeferTest()
    {
        var defer = new Defer();

        Assert.AreEqual(false, defer.IsCompleted);

        defer.Resolve();

        await defer.Wait();

        Assert.AreEqual(true, defer.IsCompleted);
    }
예제 #9
0
 public EntryPoint(
     // Error while validating the service descriptor 'ServiceType: Example.IEntryPoint Lifetime: Singleton ImplementationType: Example.EntryPoint':
     // Cannot consume scoped service 'Example.IRepository' from singleton 'Example.IEntryPoint'.
     //IRepository repository
     Defer <IRepository> repository1,
     Func <IRepository> repository2,
     IRepositoryFactory repoFactory)
 {
     _repository1 = repository1;
     _repository2 = repository2;
     _repoFactory = repoFactory;
 }
예제 #10
0
        public void TestSimpleDefer()
        {
            var x = 100;

            Assert.AreEqual(100, x);

            using (Defer.Call(() => x--))
            {
                Assert.AreEqual(100, x);
            }

            Assert.AreEqual(99, x);
        }
예제 #11
0
        public void TestDeferLIFO()
        {
            var s = "";

            using (var d = Defer.Call())
            {
                for (int i = 0; i < 4; i++)
                {
                    d.Add(i, (x) => s += x);
                }
            }

            Assert.AreEqual("3210", s);
        }
예제 #12
0
파일: TagScript.cs 프로젝트: bzure/BSA.Net
        /// <summary>Assigns all needed attributes to the tag</summary>
        /// <returns>This instance downcasted to base class</returns>
        public virtual IndexedTag attr(
            Charset charset = null,
            MimeType type = null,
            string src = null,
            Defer? defer = null,
            string xmlspace = null
        )
        {
            Charset = charset;
            Type = type;
            Src = src;
            Defer = defer;
            XmlSpace = xmlspace;

            return this;
        }
예제 #13
0
        public CustomerQuery(Defer <IStore> store, Defer <ApplicationDbContext> appDb)
        {
            var again = appDb.Value.Customers.ToList();

            using var session = store.Value.CreateSession();

            var test = session.Query <Customer>().ListAsync().Result.ToList();

            Name = "Query";
            Field <ListGraphType <CustomerGraphType> >("customers", "Returns a list of Customer", resolve: context => test);
            Field <CustomerGraphType>("customer", "Returns a Single Customer",
                                      new QueryArguments(new QueryArgument <NonNullGraphType <IntGraphType> > {
                Name = "id", Description = "Customer Id"
            }),
                                      context => test.Single(x => x.Id == context.Arguments["id"].GetPropertyValue <int>()));
        }
예제 #14
0
    public async Task DisposeAsyncCreatorTest()
    {
        var step1defer = new Defer();
        var step2defer = new Defer();

        using var pool = new ObjectPool <TestObject>(
                  1,
                  async() =>
        {
            await Task.Delay(10);
            return(new TestObject());
        });

        var thrown = false;

        var taskA = Task.Run(
            async() =>
        {
            using (await pool.GetRefAsync())
            {
                step1defer.Resolve();
                try
                {
                    using (await pool.GetRefAsync())
                    {
                        Assert.Fail("Should not be run.");
                    }
                }
                catch (OperationCanceledException)
                {
                    thrown = true;
                    step2defer.Resolve();
                }
            }
        });

        var taskB = Task.Run(
            async() =>
        {
            await step1defer.Wait();
            pool.Dispose();
            await step2defer.Wait();
            Assert.IsTrue(thrown);
        });

        await Task.WhenAll(taskA, taskB);
    }
예제 #15
0
        protected override IDisposable SubscribeCore(IObserver <T> observer, IDisposable cancel)
        {
            observer = new Defer(observer, cancel);

            IObservable <T> source;

            try
            {
                source = observableFactory();
            }
            catch (Exception ex)
            {
                source = Observable.Throw <T>(ex);
            }

            return(source.Subscribe(observer));
        }
예제 #16
0
        /// <summary>Executes a function with error handling and contextual logging</summary>
        public static async Task <T> Run <T>(this Defer <FuncCtx, ExecutionContext> ctx, ExecutionContext exec, Func <FuncCtx, Task <T> > func)
        {
            var c = await ctx.GetOrCreate(exec);

            c = c.WithLog(c.Log.ForContext("Function", exec.FunctionName));
            try {
                c.Log.Information("{Function} function - started", exec.FunctionName);
                var res = await func(c).WithDuration();

                c.Log.Information("{Function} function - completed in {Duration}", exec.FunctionName, res.Duration.HumanizeShort());
                return(res.Result);
            }
            catch (Exception ex) {
                c.Log.Error(ex, "{Function} function - unhandled exception: {Message}", exec.FunctionName, ex.Message);
                throw;
            }
        }
예제 #17
0
        public CarvedRockQuery(Defer <ICustomerRepository> customerRepository,
                               Defer <IProductRepository> productRepository,
                               Defer <IProductReviewRepository> reviewRepository)
        {
            _productRepository  = productRepository;
            _customerRepository = customerRepository;
            _reviewRepository   = reviewRepository;

            ProductGetAll();

            ProductById();

            ProductReviewByProductId();

            CustomerGetAll();

            CustomerById();
        }
예제 #18
0
    public async Task BlockTest()
    {
        var step1defer = new Defer();
        var step2defer = new Defer();

        using var pool = new ObjectPool <TestObject>(3, () => { return(new TestObject()); });

        var flag = false;

        var taskA = Task.Run(
            async() =>
        {
            using (await pool.GetRefAsync())
                using (await pool.GetRefAsync())
                {
                    using (await pool.GetRefAsync())
                    {
                        // the pool is empty now
                        step1defer.Resolve();
                        await Task.Delay(50);
                        Assert.IsFalse(flag);
                    } // relese once

                    await step2defer.Wait();
                    Assert.IsTrue(flag);
                }
        });

        var taskB = Task.Run(
            async() =>
        {
            await step1defer.Wait();

            // should block here
            using (await pool.GetRefAsync())
            {
                flag = true;
                step2defer.Resolve();
            }
        });

        await Task.WhenAll(taskA, taskB);
    }
예제 #19
0
        public CarvedRockMutation(Defer <IProductReviewRepository> reviewRepository, Defer <IReviewMessageService> messageService)
        {
            _reviewRepository = reviewRepository;
            _messageService   = messageService;
            CreateProductReview();

            //Field<UserGraphType>(
            //  "userLogin",
            //  arguments: new QueryArguments(
            //    new QueryArgument<NonNullGraphType<LoginInputType>> { Name = "credentials" }
            //  ),
            //  resolve: context =>
            //  {
            //      using (var session = store.Value.CreateSession())
            //      {
            //          //session.Save(customer);

            //          var test = session.Query<Customer>().FirstOrDefaultAsync().Result;
            //      }
            //      return new User { Id = 1, Username = "******" };
            //  });
        }
예제 #20
0
        public Controller(
            IScopeFactory scopeFactory,
            IMegaFactory factory,
            IGenericFactory <IBuilder, INotifier> generic,
            Func <ScopedService> scopedFunc,
            Func <TransientService> transientFunc,
            Defer <ScopedService> scopedDefer,
            Defer <TransientService> transientDefer)
        {
            Factory        = factory;
            Generic        = generic;
            ScopedFunc     = scopedFunc;
            ScopedDefer    = scopedDefer;
            TransientFunc  = transientFunc;
            TransientDefer = transientDefer;

            Console.WriteLine("Controller created");

            using (new Scoped <Controller>(scopeFactory))
            {
            }
        }
예제 #21
0
        /// <summary>
        /// Return a iterator block that executes the monad. See <see cref="IMonad{T}.Do"/>.
        /// </summary>
        public IEnumerator Do()
        {
            Executor executor = new Executor();

            using (var defer = new Defer())
            {
                defer.Add(() =>
                {
                    foreach (Coroutine c in executor)
                    {
                        c.Dispose();
                    }
                    executor.Clear();
                });

                for (int i = 0; i < _ms.Length; ++i)
                {
                    executor.Add(_Do(_ms[i]));
                }

                executor.Resume(Coroutine.Delta);
                while (!executor.Finished)
                {
                    if (Error != null)
                    {
                        yield break;
                    }
                    yield return(null);

                    executor.Resume(Coroutine.Delta);
                }

                if (Error != null)
                {
                    yield break;
                }
                Result = System.Array.ConvertAll(_ms, m => m.Result);
            }
        }
예제 #22
0
        public LoginMutation(Defer <IStore> store)
        {
            Field <UserGraphType>(
                "userLogin",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <LoginInputType> > {
                Name = "credentials"
            }
                    ),
                resolve: context =>
            {
                using (var session = store.Value.CreateSession())
                {
                    //session.Save(customer);

                    var test = session.Query <Customer>().FirstOrDefaultAsync().Result;
                }
                return(new User {
                    Id = 1, Username = "******"
                });
            });
        }
예제 #23
0
    public async Task CancelTest()
    {
        var flag = false;

        using var cancellationTokenSource = new CancellationTokenSource();
        var cancellationToken = cancellationTokenSource.Token;

        var worker = new AsyncTaskWorker(1);

        // blocked task
        var blockedDefer = new Defer();
        await worker.Run(async() =>
        {
            await blockedDefer.Wait();
        });

        // Task will be suspended
        var suspendedTask = worker.Run(
            () =>
        {
            flag = true;
            return(Task.CompletedTask);
        },
            cancellationToken);

        Assert.IsFalse(suspendedTask.IsCompleted);
        Assert.IsFalse(flag);

        // Cancel pending suspension
        cancellationTokenSource.Cancel();

        Assert.ThrowsAsync <OperationCanceledException>(() => suspendedTask.AsTask());

        Assert.IsTrue(suspendedTask.IsCanceled);
        Assert.IsFalse(flag);
        blockedDefer.Resolve();
    }
예제 #24
0
    public async Task MaxConcurrencyTest()
    {
        var defer    = new Defer();
        var endDefer = new Defer();

        var worker = new AsyncTaskWorker(3);

        // Three blocked tasks
        await worker.Run(() => defer.Wait());

        await worker.Run(() => defer.Wait());

        await worker.Run(() => defer.Wait());

        // Task will be suspended
        var step4Task = worker.Run(
            () =>
        {
            endDefer.Resolve();
            return(Task.CompletedTask);
        });

        await Task.Delay(100);

        Assert.IsFalse(step4Task.IsCompleted);
        Assert.IsFalse(endDefer.IsCompleted);

        // End the blocked tasks
        defer.Resolve();

        // Wait for step4 end
        await step4Task;
        await endDefer.Wait();

        Assert.IsTrue(step4Task.IsCompleted);
    }
예제 #25
0
 public static TagScript defer(this TagScript tag, Defer value) { tag.Defer = value; return tag; }
예제 #26
0
        /// output prints creates b printable HCL output and returns it.
        private slice <byte> Output(object n)
        {
            var buf = new GoBuffer();

            using (var defer = Defer.Call())
            {
                switch (n)
                {
                case File t:
                    // File doesn't trace so we add the tracing here
                    //defer.Add(() => un(trace(this, "File")));
                    return(Output(t.Node));

                case ObjectList t:
                    //defer.Add(() => un(trace(p, "ObjectList")));

                    int index = 0;
                    for (;;)
                    {
                        // Determine the location of the next actual non-comment
                        // item. If we're at the end, the next item is at "infinity"
                        Pos nextItem;
                        if (index != t.Items.Length)
                        {
                            nextItem = t.Items[index].Pos();
                        }
                        else
                        {
                            nextItem = new Pos {
                                Offset = infinity, Line = infinity
                            };
                        }

                        // Go through the standalone comments in the file and print out
                        // the comments that we should be for this object item.
                        foreach (var c in this.standaloneComments)
                        {
                            // Go through all the comments in the group. The group
                            // should be printed together, not separated by double newlines.
                            var printed        = false;
                            var newlinePrinted = false;
                            foreach (var comment in c.List)
                            {
                                // We only care about comments after the previous item
                                // we've printed so that comments are printed in the
                                // correct locations (between two objects for example).
                                // And before the next item.
                                if (comment.Pos().After(this.prev) && comment.Pos().Before(nextItem))
                                {
                                    // if we hit the end add newlines so we can print the comment
                                    // we don't do this if prev is invalid which means the
                                    // beginning of the file since the first comment should
                                    // be at the first line.
                                    if (!newlinePrinted && this.prev.IsValid && index == t.Items.Length)
                                    {
                                        buf.Write(slice.From(newline, newline));
                                        newlinePrinted = true;
                                    }

                                    // Write the actual comment.
                                    buf.WriteString(comment.Text);
                                    buf.WriteByte(newline);

                                    // Set printed to true to note that we printed something
                                    printed = true;
                                }
                            }

                            // If we're not at the last item, write a new line so
                            // that there is a newline separating this comment from
                            // the next object.
                            if (printed && index != t.Items.Length)
                            {
                                buf.WriteByte(newline);
                            }
                        }

                        if (index == t.Items.Length)
                        {
                            break;
                        }

                        buf.Write(this.Output(t.Items[index]));
                        if (index != t.Items.Length - 1)
                        {
                            // Always write a newline to separate us from the next item
                            buf.WriteByte(newline);

                            // Need to determine if we're going to separate the next item
                            // with a blank line. The logic here is simple, though there
                            // are a few conditions:
                            //
                            //   1. The next object is more than one line away anyways,
                            //      so we need an empty line.
                            //
                            //   2. The next object is not a "single line" object, so
                            //      we need an empty line.
                            //
                            //   3. This current object is not a single line object,
                            //      so we need an empty line.
                            var current = t.Items[index];
                            var next    = t.Items[index + 1];
                            if ((next.Pos().Line != t.Items[index].Pos().Line + 1) ||
                                !this.IsSingleLineObject(next) ||
                                !this.IsSingleLineObject(current))
                            {
                                buf.WriteByte(newline);
                            }
                        }
                        index++;
                    }
                    break;

                case ObjectKey t:
                    buf.WriteString(t.Token.Text);
                    break;

                case ObjectItem t:
                    this.prev = t.Pos();
                    buf.Write(this.ObjectItem(t));
                    break;

                case LiteralType t:
                    buf.Write(this.LiteralType(t));
                    break;

                case ListType t:
                    buf.Write(this.List(t));
                    break;

                case ObjectType t:
                    buf.Write(this.ObjectType(t));
                    break;

                default:
                    Console.WriteLine($"unknown type {n}");
                    break;
                }

                return(buf.Bytes());
            }
        }
 public Service(Defer <ScopedAsSingleton> scoped)
 {
     Scoped = scoped;
 }
예제 #28
0
 public static async Task Run(this Defer <FuncCtx, ExecutionContext> ctx, ExecutionContext exec, Func <FuncCtx, Task> action) =>
 await Run <object>(ctx, exec, async c => {
     await action(c);
     return(null);
 });
예제 #29
0
        public async Task <ProcessChannelResults> ProcessChannels(IReadOnlyCollection <ChannelUpdatePlan> channels,
                                                                  CollectPart[] parts, ILogger log = null, CancellationToken cancel = default)
        {
            log ??= Logger.None;
            var workSw = Stopwatch.StartNew();

            // to save on db costs, get anything we need in advance of collection
            var channelChromeVideos = new MultiValueDictionary <string, string>();
            var channelDeadVideos   = new MultiValueDictionary <string, string>();

            if (parts.ShouldRun(VidExtra))
            {
                using var db = await Sf.OpenConnection(log);

                var forChromeUpdate = await channels.Select(c => c.Channel).Batch(1000)
                                      .BlockFunc(c => VideosForChromeUpdate(c, db, log));

                channelChromeVideos = forChromeUpdate.SelectMany()
                                      .Randomize().Take(RCfg.ChromeUpdateMax)
                                      .ToMultiValueDictionary(c => c.ChannelId, c => c.VideoId);

                channelDeadVideos = (await channels.Select(c => c.Channel).Batch(1000)
                                     .BlockFunc(c => DeadVideosForExtraUpdate(c, db, log)))
                                    .SelectMany().ToMultiValueDictionary(v => v.ChannelId, v => v.VideoId);
            }

            var channelResults = await channels
                                 .Select((c, i) => (c, i))
                                 .BlockFunc(async item => {
                var(plan, i) = item;
                var c        = plan.Channel;
                var sw       = Stopwatch.StartNew();
                var cLog     = log
                               .ForContext("ChannelId", c.ChannelId)
                               .ForContext("Channel", c.ChannelTitle);
                try {
                    await using var conn = new Defer <ILoggedConnection <IDbConnection> >(async() => await Sf.OpenConnection(cLog));
                    await UpdateAllInChannel(plan, parts, channelChromeVideos.TryGet(c.ChannelId), channelDeadVideos.TryGet(c.ChannelId), cLog);
                    cLog.Information("Collect - {Channel} - Completed videos/recs/captions in {Duration}. Progress: channel {Count}/{BatchTotal}",
                                     c.ChannelTitle, sw.Elapsed.HumanizeShort(), i + 1, channels.Count);
                    return(c, Success: true);
                }
                catch (Exception ex) {
                    ex.ThrowIfUnrecoverable();
                    cLog.Error(ex, "Collect - Error updating channel {Channel}: {Error}", c.ChannelTitle, ex.Message);
                    return(c, Success: false);
                }
            }, RCfg.ParallelChannels, cancel: cancel);

            var res = new ProcessChannelResults {
                Channels = channelResults.Select(r => new ProcessChannelResult {
                    ChannelId = r.c.ChannelId, Success = r.Success
                }).ToArray(),
                Duration = workSw.Elapsed
            };

            log.Information(
                "Collect - {Pipe} complete - {ChannelsComplete} channel videos/captions/recs, {ChannelsFailed} failed {Duration}",
                nameof(ProcessChannels), channelResults.Count(c => c.Success), channelResults.Count(c => !c.Success), res.Duration);

            return(res);
        }
예제 #30
0
 public ApiBackend(Defer <FuncCtx, ExecutionContext> ctx) => Ctx = ctx;
예제 #31
0
 internal DeferredBehavior(Defer <T> deferred)
 {
     Deferred = deferred;
 }
예제 #32
0
 public static TagScript defer(this TagScript tag, Defer value)
 {
     tag.Defer = value; return(tag);
 }