Ejemplo n.º 1
0
        public void CacheCapacity_ChangesApplied()
        {
            // A configuration service is instantiated here because it will
            // subscribe to the ConfigurationDeserializedEvent fired in SetCacheCapcity.
            var configurationService = new ConfigurationService(
                Mock.Create <IEnvironment>(),
                Mock.Create <IProcessStatic>(),
                Mock.Create <IHttpRuntimeStatic>(),
                Mock.Create <IConfigurationManagerStatic>(),
                Mock.Create <IDnsStatic>());

            const string sql1 = "select * from table1";
            const string sql2 = "select * from table2";
            const string sql3 = "select * from table3";

            //Set initial capacity of cache to 2
            SetCacheCapacity(2);

            _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql1);
            _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql2);
            var stmtA = _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql3);

            Thread.Sleep(1000);//Allow cache to periodically clean

            var stmtB = _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql3);

            //stmtA and stmtB are the same SQL, but stmtA was ejected from the cache because of the cache periodically cleanup, so they cannot be the same object reference
            //This tests our original capacity is being honored.
            Assert.AreNotSame(stmtA, stmtB);

            //Resize the cache
            SetCacheCapacity(3);

            _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql1);
            _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql2);

            Thread.Sleep(1000);//Allow cache to periodically clean

            //stmtB and stmtC are the same SQL, but this time nothing was ejected because of the cache size is withing its capacity, so they are the same object reference
            var stmtC = _databaseStatementParser.ParseDatabaseStatement(DatastoreVendor.MSSQL, CommandType.Text, sql3);

            Assert.AreSame(stmtB, stmtC);

            configurationService.Dispose();
        }
 public void TearDown()
 {
     _configurationService.Dispose();
 }