Skip to content

Commit

Permalink
CACHEBOX-87
Browse files Browse the repository at this point in the history
BoxLang Providers
  • Loading branch information
lmajano committed Jan 11, 2025
1 parent a75b8ee commit b6e778d
Show file tree
Hide file tree
Showing 2 changed files with 264 additions and 0 deletions.
58 changes: 58 additions & 0 deletions tests/specs/cache/providers/BoxLangColdBoxProviderTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
component
extends="BoxLangProviderTest"
output ="false"
skip ="notBoxLang"
{

function setup(){
super.setup();
// Mock Controller
mockController = createEmptyMock( "coldbox.system.web.Controller" );

// Create Provider
cache = createMock( "coldbox.system.cache.providers.BoxLangColdBoxProvider" ).init();

// Decorate it
cache.setConfiguration( config );
cache.setCacheFactory( mockFactory );
cache.setEventManager( mockEventManager );
cache.setColdbox( mockController );

// Configure the provider
cache.configure();
// Clear everything first
cache.clearAll();
}

function testPrefixes(){
assertTrue( len( cache.getEventCacheKeyPrefix() ) );
assertTrue( len( cache.getViewCacheKeyPrefix() ) );
}

function testgetEventURLFacade(){
assertEquals( true, isInstanceOf( cache.getEventURLFacade(), "coldbox.system.cache.util.EventURLFacade" ) );
}

function testClearAllEvents(){
cache.clearAllEvents();
}
function testClearAllViews(){
cache.ClearAllViews();
}
function testclearByKeySnippet(){
cache.clearByKeySnippet( "test", false );
}
function testclearEvent(){
cache.clearEvent( "test" );
}
function testclearEventMulti(){
cache.clearEventMulti( "test" );
}
function testclearViewMulti(){
cache.clearViewMulti( "test" );
}
function testclearView(){
cache.clearView( "test" );
}

}
206 changes: 206 additions & 0 deletions tests/specs/cache/providers/BoxLangProviderTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
component extends="tests.resources.BaseIntegrationTest" skip="notBoxlang" {

this.loadColdBox = false;

boolean function notBoxlang(){
return !isBoxLang();
}

function setup(){
super.setup();

// Mocks
mockFactory = createMock( "coldbox.system.cache.CacheFactory" );
mockEventManager = createMock( "coldbox.system.core.events.EventPoolManager" );
mockLogBox = createMock( "coldbox.system.logging.LogBox" );
mockLogger = createMock( "coldbox.system.logging.Logger" );

// Mock Methods
mockFactory.setLogBox( mockLogBox );
mockLogBox.$( "getLogger", mockLogger );
mockLogger
.$( "error" )
.$( "debug" )
.$( "info" )
.$( "canDebug", false );
mockEventManager.$( "announce" );

// Config
config = {};

// Create Provider
cache = createMock( "coldbox.system.cache.providers.BoxLangProvider" ).init();

// Decorate it
cache.setConfiguration( config );
cache.setCacheFactory( mockFactory );
cache.setEventManager( mockEventManager );

// Configure the provider
cache.configure();
// Clear everything first
cache.clearAll();
}

function teardown(){
cache.clearAll();
}

function testShutdown(){
cache.shutdown();
}

function testLookup(){
cache.set( "test", now(), 20 );
cache.clearStatistics();

assertEquals( false, cache.lookup( "invalid" ) );
assertEquals( true, cache.lookup( "test" ) );
}

function testLookupQuiet(){
cache.set( "test", now(), 20 );
cache.clearStatistics();

assertEquals( false, cache.lookupQuiet( "invalid" ) );
assertEquals( true, cache.lookupQuiet( "test" ) );
}

function testgetKeys(){
cacheClear();
cache.set( "test", now() );
cache.set( "test2", now() );
assertEquals( 2, arrayLen( cache.getKeys() ) );
}

function testgetCachedObjectMetadata(){
cache.set( "test", now() );
md = cache.getCachedObjectMetadata( "test" );
// debug(md);
assertEquals( false, structIsEmpty( md ) );
}

function testGet(){
testVal = { name : "luis", age : 32 };

cache.set( "test", testVal, 20 );
cache.clearStatistics();

results = cache.get( "test" );
assertEquals( results, testval );
// assertEquals( 0, cache.getStats().getMisses() );
// assertEquals( 1, cache.getStats().getHits() );

results = cache.get( "test2" );
assertFalse( isDefined( "results" ) );
// assertEquals( 1, cache.getStats().getMisses() );
}

function testGetOrSet(){
cache.clearStatistics();

results = cache.getOrSet( objectKey = "test", produce = cacheProducer );
assertTrue( structKeyExists( results, "name" ) );

results = cache.getOrSet( objectKey = "test", produce = cacheProducer );
assertTrue( structKeyExists( results, "name" ) );
}

function testGetQuiet(){
testVal = { name : "luis", age : 32 };

cache.clearStatistics();
cache.set( "test", testVal, 20 );

results = cache.getQuiet( "test" );
// debug(results);
assertEquals( testVal, results );
// assertEquals( 0, cache.getStats().getMisses() );
// assertEquals( 0, cache.getStats().getHits() );
}

function testSet(){
testVal = { name : "luis", age : 32 };
cache.clearAll();

cache.set(
"test",
testVal,
createTimespan( 0, 0, 2, 0 ),
createTimespan( 0, 0, 1, 0 )
);
assertEquals( testVal, cache.get( "test" ) );
md = cache.getCachedObjectMetadata( "test" );
assertEquals( 60 * 1000, md.idleTime );
assertEquals( 120 * 1000, md.timespan );
// debug(md);
}

function testSetQuiet(){
testVal = { name : "luis", age : 32 };
cache.clearAll();

cache.setQuiet(
"test",
testVal,
createTimespan( 0, 0, 2, 0 ),
createTimespan( 0, 0, 1, 0 )
);
assertEquals( testVal, cache.get( "test" ) );
md = cache.getCachedObjectMetadata( "test" );
assertEquals( 60 * 1000, md.idleTime );
assertEquals( 120 * 1000, md.timespan );
// debug(md);
}

function testGetSize(){
testVal = { name : "luis", age : 32 };
cache.clearAll();

cache.setQuiet(
"test",
testVal,
createTimespan( 0, 0, 2, 0 ),
createTimespan( 0, 0, 1, 0 )
);

assertEquals( 1, cache.getSize() );
}

function testClear(){
testVal = { name : "luis", age : 32 };
cache.clearAll();

cache.set(
"test",
testVal,
createTimespan( 0, 0, 2, 0 ),
createTimespan( 0, 0, 1, 0 )
);

assertEquals( 1, cache.getSize() );
cache.clear( "test" );
assertEquals( 0, cache.getSize() );
}

function testClearQuiet(){
testVal = { name : "luis", age : 32 };
cache.clearAll();

cache.set(
"test",
testVal,
createTimespan( 0, 0, 2, 0 ),
createTimespan( 0, 0, 1, 0 )
);

assertEquals( 1, cache.getSize() );
cache.clearQuiet( "test" );
assertEquals( 0, cache.getSize() );
}

private function cacheProducer(){
return { date : now(), name : "luis majano", id : createUUID() };
}

}

0 comments on commit b6e778d

Please sign in to comment.