|
| 1 | +component extends="org.lucee.cfml.test.LuceeTestCase" labels="ram" { |
| 2 | + |
| 3 | + function afterAll() { |
| 4 | + if ( directoryExists( "ram://LDEV1109" ) ) { |
| 5 | + directoryDelete( "ram://LDEV1109", true ); |
| 6 | + } |
| 7 | + } |
| 8 | + |
| 9 | + function run( testResults, testBox ) { |
| 10 | + describe( "LDEV-1109 RAM drive should survive cache eviction", function() { |
| 11 | + |
| 12 | + it( title="ram:// root should exist after cache is cleared", body=function() { |
| 13 | + // verify root exists before clear |
| 14 | + expect( directoryExists( "ram://" ) ).toBeTrue(); |
| 15 | + |
| 16 | + // clear the cache, simulating GC reclaiming all references |
| 17 | + var ramCache = _getRamCache(); |
| 18 | + ramCache.clear(); |
| 19 | + |
| 20 | + // root should be auto-recreated on next access |
| 21 | + expect( directoryExists( "ram://" ) ).toBeTrue(); |
| 22 | + }); |
| 23 | + |
| 24 | + it( title="ram:// directories should be stored with HardRef not SoftRef", body=function() { |
| 25 | + var dir = "ram://LDEV1109"; |
| 26 | + // cleanup from previous run |
| 27 | + if ( directoryExists( dir ) ) directoryDelete( dir, true ); |
| 28 | + |
| 29 | + // create directory structure with a file |
| 30 | + directoryCreate( dir ); |
| 31 | + directoryCreate( dir & "/subdir" ); |
| 32 | + fileWrite( dir & "/subdir/file.txt", "data" ); |
| 33 | + |
| 34 | + // use reflection to access the private entries map in RamCache |
| 35 | + var ramCache = _getRamCache(); |
| 36 | + var entriesField = ramCache.getClass().getDeclaredField( "entries" ); |
| 37 | + entriesField.setAccessible( true ); |
| 38 | + var entries = entriesField.get( ramCache ); |
| 39 | + |
| 40 | + var HardRef = createObject( "java", "lucee.runtime.cache.ram.ref.HardRef" ); |
| 41 | + var dirCount = 0; |
| 42 | + var fileCount = 0; |
| 43 | + |
| 44 | + var iter = entries.values().iterator(); |
| 45 | + while ( iter.hasNext() ) { |
| 46 | + var ref = iter.next(); |
| 47 | + var rce = ref.get(); |
| 48 | + if ( isNull( rce ) ) continue; |
| 49 | + var val = rce.getValue(); |
| 50 | + if ( !isInstanceOf( val, "lucee.commons.io.res.type.cache.CacheResourceCore" ) ) continue; |
| 51 | + |
| 52 | + if ( val.getType() == 1 ) { |
| 53 | + // TYPE_DIRECTORY = 1: must be pinned with HardRef |
| 54 | + expect( ref ).toBeInstanceOf( "lucee.runtime.cache.ram.ref.HardRef", |
| 55 | + "directory entry should use HardRef, not SoftRef" ); |
| 56 | + dirCount++; |
| 57 | + } |
| 58 | + else if ( val.getType() == 2 ) { |
| 59 | + // TYPE_FILE = 2: should use SoftRef (reclaimable under memory pressure) |
| 60 | + expect( ref ).toBeInstanceOf( "lucee.runtime.cache.ram.ref.SoftRef", |
| 61 | + "file entry should use SoftRef, not HardRef" ); |
| 62 | + fileCount++; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + // root + LDEV1109 + subdir = at least 3 directories |
| 67 | + expect( dirCount ).toBeGTE( 3, "should have at least 3 directory entries" ); |
| 68 | + // at least 1 file |
| 69 | + expect( fileCount ).toBeGTE( 1, "should have at least 1 file entry" ); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + private function _getRamCache() { |
| 75 | + var providers = getPageContext().getConfig().getResourceProviders(); |
| 76 | + for ( var p in providers ) { |
| 77 | + if ( p.getScheme() == "ram" ) { |
| 78 | + return p.getCache(); |
| 79 | + } |
| 80 | + } |
| 81 | + throw( message="RAM resource provider not found" ); |
| 82 | + } |
| 83 | +} |
0 commit comments