Ejemplo n.º 1
0
        public static Habit AddLog(Habit h)
        {
            HabitResultHandler success = new SuccessHandler(new StreakSuccess());
            HabitResultHandler fail    = new FailHandler(new StreakFail());

            h.Attach(success);
            h.Attach(fail);
            h.AddLogs();
            return(h);
        }
Ejemplo n.º 2
0
        protected Looper(Handler handler, FailHandler failHandler, Object lockObj)
        {
            if (lockObj == null)
            {
                lockObj = this;
            }

            mLock        = lockObj;
            mHandler     = handler;
            mFailHandler = failHandler;

            lock (mLock) {
                mState = STATE_NONE;
            }
        }
Ejemplo n.º 3
0
    public async Task Authorize_ShouldInvokeAllHandlersByDefault()
    {
        // Arrange
        var handler1             = new FailHandler();
        var handler2             = new FailHandler();
        var authorizationService = BuildAuthorizationService(services =>
        {
            services.AddSingleton <IAuthorizationHandler>(handler1);
            services.AddSingleton <IAuthorizationHandler>(handler2);
            services.AddAuthorizationBuilder().AddPolicy("Custom", policy => policy.Requirements.Add(new CustomRequirement()));
        });

        // Act
        var allowed = await authorizationService.AuthorizeAsync(new ClaimsPrincipal(), "Custom");

        // Assert
        Assert.False(allowed.Succeeded);
        Assert.True(allowed.Failure.FailCalled);
        Assert.True(handler1.Invoked);
        Assert.True(handler2.Invoked);
    }
Ejemplo n.º 4
0
        public async Task Authorize_ShouldInvokeAllHandlersDependingOnSetting(bool invokeAllHandlers)
        {
            // Arrange
            var handler1             = new FailHandler();
            var handler2             = new FailHandler();
            var authorizationService = BuildAuthorizationService(services =>
            {
                services.AddSingleton <IAuthorizationHandler>(handler1);
                services.AddSingleton <IAuthorizationHandler>(handler2);
                services.AddAuthorization(options =>
                {
                    options.InvokeHandlersAfterFailure = invokeAllHandlers;
                    options.AddPolicy("Custom", policy => policy.Requirements.Add(new CustomRequirement()));
                });
            });

            // Act
            var allowed = await authorizationService.AuthorizeAsync(new ClaimsPrincipal(), "Custom");

            // Assert
            Assert.False(allowed.Succeeded);
            Assert.True(handler1.Invoked);
            Assert.Equal(invokeAllHandlers, handler2.Invoked);
        }
Ejemplo n.º 5
0
        public virtual bool Handle(long timeout)
        {
            Handler handler = mHandler;

            lock (mLock) {
                int state = mState;
                if ((state & STATE_READY) != STATE_READY)
                {
                    return(false);
                }
                state &= ~STATE_READY;
                if (handler != null && !handler.IsClosed())
                {
                    state |= STATE_STARTED;
                }
                mState = state;
                Monitor.PulseAll(mLock);
                if ((state & STATE_STARTED) != STATE_STARTED)
                {
                    return(false);
                }
            }

            Request   request;
            Exception exception = null;

            try {
                request = handler.Next(timeout);
                if (request == null)
                {
                    return(false);
                }

                lock (mLock) {
                    if ((mState & STATE_STARTED) != STATE_STARTED)
                    {
                        goto failed;
                    }
                }

                try {
                    if (request.Execute())
                    {
                        return(true);
                    }
                } catch (Exception ex) {
                    exception = ex;
                }
            } finally {
                handler = mHandler;
                lock (mLock) {
                    int state = mState;
                    if ((state & STATE_STARTED) == STATE_STARTED)
                    {
                        if (handler == null || handler.IsClosed())
                        {
                            state &= ~STATE_READY;
                        }
                        else
                        {
                            state |= STATE_READY;
                        }
                        mState = state;
                        Monitor.PulseAll(mLock);
                    }
                }
            }

failed:
            FailHandler failHandler = mFailHandler;

            if (failHandler != null)
            {
                try {
                    failHandler(request, exception);
                } catch {}
            }
            return(true);
        }
Ejemplo n.º 6
0
 public Looper(Handler handler, FailHandler failHandler) : this(handler, failHandler, null)
 {
 }
Ejemplo n.º 7
0
            public void Start()
            {
                Console.WriteLine($"{FirstTeam.Name} против {SecondTeam.Name}");
                int t1 = 0;
                int t2 = 0;

                for (int i = 1; i < 30; i++)
                {
                    switch (random.Next(1, 10))
                    {
                    case 1:
                        if (Referi.Preference != Referi.Preferences.ForFirst)
                        {
                            FailHandler.Invoke(FirstTeam, " игра рукой");
                        }
                        break;

                    case 2:
                        if (Referi.Preference != Referi.Preferences.ForSecond)
                        {
                            FailHandler.Invoke(SecondTeam, " игра рукой");
                        }
                        break;

                    case 3:
                        if (Winner() == SecondTeam.Name)
                        {
                            GoalHandler.Invoke(SecondTeam);
                            t2++;
                        }
                        else
                        {
                            if (Winner() == FirstTeam.Name)
                            {
                                GoalHandler.Invoke(FirstTeam);
                                t1++;
                            }
                            else
                            {
                                if (random.Next(1, 100) % 2 == 0)
                                {
                                    GoalHandler.Invoke(SecondTeam);
                                    t2++;
                                }
                                else
                                {
                                    GoalHandler.Invoke(FirstTeam);
                                    t1++;
                                }
                            }
                        }
                        break;

                    case 4:
                        try
                        {
                            switch (random.Next(1, 5))
                            {
                            case 1:
                                throw new GameExeption("На стадионе бомба", 12);

                            case 2:
                                throw new GameExeption("Зависла система", 3);
                            }
                        }
                        catch (GameExeption ex)
                        {
                            Console.WriteLine($"Чп: {ex.Message}, матч перенесен на {ex.Overtime} ч. Данные счета были потеряны");
                            t1 = 0;
                            t2 = 0;
                        }
                        break;
                    }
                }
                if (random.Next(0, 100) % 2 == 0)
                {
                    AdditionalHandler.Invoke(random.Next(1, 11));
                }
                Console.WriteLine($"Матч завершился со счетом {t1}:{t2}");
                using (StreamWriter sr = new StreamWriter("Games.txt", true))
                {
                    sr.WriteLine($"{FirstTeam.Name}|{SecondTeam.Name}|{t1}:{t2}");
                }
            }