Ejemplo n.º 1
0
        public void CarDoor_WhenDoorIsClosedAndProximityKeyIsNearBy_OnOpenDoor_ShouldOpen()
        {
            //ARRANGE
            var mockedSensor = Substitute.For <IProximitySensor>();

            mockedSensor.IsTurnedOn = true;
            mockedSensor.CheckNearbyKeys().Returns(true);
            var lockmechanism = new LockingMechanism(mockedSensor);
            var Doors         = new Dictionary <string, Door>()
            {
                { "FrontLeft", new Door(lockmechanism) },
                { "FrontRight", new Door(lockmechanism) },
                { "RearLeft", new Door(lockmechanism) },
                { "RearRight", new Door(lockmechanism) }
            };
            var picanto = new Car(Doors);

            // ACT
            picanto.Doors["FrontLeft"].Open();


            // ASSERT
            Assert.AreEqual(picanto.Doors["FrontLeft"].CurrentState, "Opened");
            picanto.Doors["FrontLeft"].CurrentState.ShouldBe("Opened");
        }
        protected virtual AuthenticatedSession OnAuthenticate(string defaultSchema, string username, string password)
        {
            var user = Database.AuthenticateUser(username, password, RemoteEndPoint);

            if (user == null)
            {
                return(null);
            }

            IDatabaseConnection connection = Database.CreateNewConnection(user, OnTriggerFired);

            // Put the connection in exclusive mode
            LockingMechanism locker = connection.LockingMechanism;

            locker.SetMode(LockingMode.Exclusive);

            try {
                // By default, connections are auto-commit
                connection.AutoCommit = true;

                // Set the default schema for this connection if it exists
                if (connection.SchemaExists(defaultSchema))
                {
                    connection.SetDefaultSchema(defaultSchema);
                }
                else
                {
                    Logger.WarningFormat(this, "Couldn't change to '{0}' schema.", defaultSchema);

                    // If we can't change to the schema then change to the APP schema
                    connection.SetDefaultSchema(ConfigDefaultValues.DefaultSchema);
                }
            } finally {
                try {
                    connection.Commit();
                } catch (TransactionException e) {
                    // Just issue a warning...
                    Logger.Warning(this, e);
                } finally {
                    // Guarentee that we unluck from EXCLUSIVE
                    locker.FinishMode(LockingMode.Exclusive);
                }
            }

            return(new AuthenticatedSession(user, connection));
        }
Ejemplo n.º 3
0
        public void CarDoorWhenDoorIsClosedAndNoProximityKeyPresent_OnOpenDoor_ShouldNotOpen()
        {
            //ARRANGE
            var mockedSensor = Substitute.For <IProximitySensor>();

            mockedSensor.IsTurnedOn = true;
            var lockmechanism = new LockingMechanism(mockedSensor);
            var Doors         = new Dictionary <string, Door>()
            {
                { "FrontLeft", new Door(lockmechanism) },
            };
            var picanto = new Car(Doors);

            // ACT
            picanto.Doors["FrontLeft"].Open();


            // ASSERT
            Assert.AreEqual(picanto.Doors["FrontLeft"].CurrentState, "Closed");
            picanto.Doors["FrontLeft"].CurrentState.ShouldBe("Closed");
        }
        protected virtual IQueryResponse[] ExecuteQuery(string text, IEnumerable <SqlQueryParameter> parameters)
        {
            // Log this Query if Query logging is enabled
            if (Logger.IsInterestedIn(LogLevel.Debug))
            {
                // Output the instruction to the _queries log.
                Logger.DebugFormat(this, "[CLIENT] [{0}] - Query: {1}", Session.User.UserName, text);
            }

            // Write debug message (Info level)
            if (Logger.IsInterestedIn(LogLevel.Debug))
            {
                Logger.DebugFormat(this, "Query From User: {0}", Session.User.UserName);
                Logger.DebugFormat(this, "Query: {0}", text.Trim());
            }

            // Get the locking mechanism.
            LockingMechanism locker   = Session.Connection.LockingMechanism;
            LockingMode      lockMode = LockingMode.None;

            IQueryResponse[] response = null;

            try {
                try {
                    // For simplicity - all database locking is now exclusive inside
                    // a transaction.  This means it is not possible to execute
                    // queries concurrently inside a transaction.  However, we are
                    // still able to execute queries concurrently from different
                    // connections.
                    //
                    // It's debatable whether we even need to perform this Lock anymore
                    // because we could change the contract of this method so that
                    // it is not thread safe.  This would require that the callee ensures
                    // more than one thread can not execute queries on the connection.
                    lockMode = LockingMode.Exclusive;
                    locker.SetMode(lockMode);

                    // Execute the Query (behaviour for this comes from super).
                    response = CoreExecuteQuery(text, parameters);

                    // Return the result.
                    return(response);
                } finally {
                    try {
                        // This is executed no matter what happens.  Very important we
                        // unlock the tables.
                        if (lockMode != LockingMode.None)
                        {
                            locker.FinishMode(lockMode);
                        }
                    } catch (Exception e) {
                        // If this throws an exception, we should output it to the debug
                        // log and screen.
                        Logger.Error(this, "Exception finishing locks");
                        Logger.Error(this, e);
                        // Note, we can't throw an error here because we may already be in
                        // an exception that happened in the above 'try' block.
                    }
                }
            } finally {
                // This always happens after tables are unlocked.
                // Also guarenteed to happen even if something fails.

                // If we are in auto-commit mode then commit the Query here.
                // Do we auto-commit?
                if (Session.Connection.AutoCommit)
                {
                    // Yes, so grab an exclusive Lock and auto-commit.
                    try {
                        // Lock into exclusive mode.
                        locker.SetMode(LockingMode.Exclusive);
                        // If an error occured then roll-back
                        if (response == null)
                        {
                            // Rollback.
                            Session.Connection.Rollback();
                        }
                        else
                        {
                            try {
                                // Otherwise commit.
                                Session.Connection.Commit();
                            } catch (Exception e) {
                                foreach (IQueryResponse queryResponse in response)
                                {
                                    // Dispose this response if the commit failed.
                                    DisposeResult(queryResponse.ResultId);
                                }

                                // And throw the SQL Exception
                                throw;
                            }
                        }
                    } finally {
                        locker.FinishMode(LockingMode.Exclusive);
                    }
                }
            }
        }