@@ -13,6 +13,7 @@ import (
1313 "github.com/oracle/coherence-go-client/coherence/filters"
1414 "github.com/oracle/coherence-go-client/coherence/processors"
1515 "github.com/oracle/coherence-go-client/test/utils"
16+ "math"
1617 "testing"
1718 "time"
1819)
@@ -33,6 +34,7 @@ func TestNearCacheOperationsAgainstMapAndCache(t *testing.T) {
3334 nearCacheOptions120Seconds := coherence.NearCacheOptions {TTL : time .Duration (120 ) * time .Second }
3435 nearCacheOptionsHighUnits1 := coherence.NearCacheOptions {HighUnits : 100 }
3536 nearCacheOptionsHighUnits2 := coherence.NearCacheOptions {HighUnitsMemory : 50 * 1024 }
37+ nearCacheOptionsHighUnits3 := coherence.NearCacheOptions {HighUnits : 100 , PruneFactor : 0.2 }
3638
3739 testCases := []struct {
3840 testName string
@@ -55,6 +57,8 @@ func TestNearCacheOperationsAgainstMapAndCache(t *testing.T) {
5557 {"RunTestNearCacheGetAllNamedCache2" , GetNearCacheNamedCache [int , utils.Person ](g , session , "near-cache-get-all-cache2" , coherence .WithNearCache (& nearCacheOptions120Seconds )), RunTestNearCacheGetAll2 },
5658 {"RunTestNearCacheWithHighUnitsNamedMap" , GetNearCacheNamedMap [int , utils.Person ](g , session , "near-cache-high-units-map" , coherence .WithNearCache (& nearCacheOptionsHighUnits1 )), RunTestNearCacheWithHighUnits },
5759 {"RunTestNearCacheWithHighUnitsNamedCache" , GetNearCacheNamedCache [int , utils.Person ](g , session , "near-cache-high-units-cache" , coherence .WithNearCache (& nearCacheOptionsHighUnits1 )), RunTestNearCacheWithHighUnits },
60+ {"RunTestNearCacheWithHighUnitsNamedMapPruneFactor" , GetNearCacheNamedMap [int , utils.Person ](g , session , "near-cache-high-units-map" , coherence .WithNearCache (& nearCacheOptionsHighUnits3 )), RunTestNearCacheWithHighUnits },
61+ {"RunTestNearCacheWithHighUnitsNamedCachePruneFactor" , GetNearCacheNamedCache [int , utils.Person ](g , session , "near-cache-high-units-cache" , coherence .WithNearCache (& nearCacheOptionsHighUnits3 )), RunTestNearCacheWithHighUnits },
5862 {"RunTestNearCacheWithHighUnitsMemoryNamedMap" , GetNearCacheNamedMap [int , utils.Person ](g , session , "near-cache-high-units-mem-map" , coherence .WithNearCache (& nearCacheOptionsHighUnits2 )), RunTestNearCacheWithHighUnitsMemory },
5963 {"RunTestNearCacheWithHighUnitsMemoryNamedCache" , GetNearCacheNamedCache [int , utils.Person ](g , session , "near-cache-high-units-mem-cache" , coherence .WithNearCache (& nearCacheOptionsHighUnits2 )), RunTestNearCacheWithHighUnitsMemory },
6064 {"RunTestNearCacheWithHighUnitsAccessNamedMap" , GetNearCacheNamedMap [int , utils.Person ](g , session , "near-cache-high-units-access-mem-map" , coherence .WithNearCache (& nearCacheOptionsHighUnits1 )), RunTestNearCacheWithHighUnitsAccess },
@@ -342,12 +346,16 @@ func RunTestNearCacheWithHighUnits(t *testing.T, namedMap coherence.NamedMap[int
342346 // should have 100 entries in near cache
343347 g .Expect (namedMap .GetNearCacheStats ().Size ()).To (gomega .Equal (100 ))
344348
345- // issue a Get() for an entry not in the cache which will trigger the HighUnits and prune to 80 entries
349+ // issue a Get() for an entry not in the cache which will trigger the HighUnits and prune entries
346350 _ , err = namedMap .Get (ctx , 110 )
347351 g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
348352
349- g .Expect (namedMap .GetNearCacheStats ().Size ()).To (gomega .Equal (80 ))
350- g .Expect (namedMap .GetNearCacheStats ().GetCachePrunes ()).To (gomega .Equal (int64 (1 )))
353+ // get the current PuneFactor
354+ pruneFactor := coherence .GetNearCachePruneFactor (namedMap )
355+ expectedSize := int (math .Round (float64 (float32 (100 ) * pruneFactor )))
356+
357+ g .Expect (namedMap .GetNearCacheStats ().Size ()).To (gomega .Equal (expectedSize ))
358+ g .Expect (namedMap .GetNearCacheStats ().GetCachePrunes ()).ToNot (gomega .Equal (int64 (0 )))
351359}
352360
353361// RunTestNearWithClear tests a near cache that issues puts, get then clears..
@@ -438,8 +446,12 @@ func RunTestNearCacheWithHighUnitsAccess(t *testing.T, namedMap coherence.NamedM
438446 _ , err = namedMap .Get (ctx , 110 )
439447 g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
440448
441- g .Expect (namedMap .GetNearCacheStats ().Size ()).To (gomega .Equal (80 ))
442- g .Expect (namedMap .GetNearCacheStats ().GetCachePrunes ()).To (gomega .Equal (int64 (1 )))
449+ // get the current PuneFactor
450+ pruneFactor := coherence .GetNearCachePruneFactor (namedMap )
451+ expectedSize := int (math .Round (float64 (float32 (100 ) * pruneFactor )))
452+
453+ g .Expect (namedMap .GetNearCacheStats ().Size ()).To (gomega .Equal (expectedSize ))
454+ g .Expect (namedMap .GetNearCacheStats ().GetCachePrunes ()).ToNot (gomega .Equal (int64 (0 )))
443455
444456 t .Log ("near cache stats before get" , namedMap .GetNearCacheStats ())
445457
@@ -547,6 +559,30 @@ func TestDuplicateNamedCache(t *testing.T) {
547559 namedMap .Release ()
548560}
549561
562+ // TestNearCachePruneFactor runs tests to ensure we honor the pruneFactor.
563+ func TestNearCachePruneFactor (t * testing.T ) {
564+ g := gomega .NewWithT (t )
565+
566+ session , err := utils .GetSession ()
567+ g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
568+ defer session .Close ()
569+
570+ // test value of 0.2
571+ nearCacheOptions1 := coherence.NearCacheOptions {HighUnits : 100 , PruneFactor : 0.2 }
572+ namedCache , err := coherence .GetNamedCache [int , string ](session , nearCacheName , coherence .WithNearCache (& nearCacheOptions1 ))
573+ g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
574+
575+ // validate the factor
576+ g .Expect (coherence.GetNearCachePruneFactor [int , string ](namedCache )).To (gomega .Equal (nearCacheOptions1 .PruneFactor ))
577+ namedCache .Release ()
578+
579+ // test default value of 0.8
580+ nearCacheOptions2 := coherence.NearCacheOptions {HighUnits : 100 }
581+ namedCache , err = coherence .GetNamedCache [int , string ](session , nearCacheName , coherence .WithNearCache (& nearCacheOptions2 ))
582+ g .Expect (err ).ShouldNot (gomega .HaveOccurred ())
583+ g .Expect (coherence.GetNearCachePruneFactor [int , string ](namedCache )).To (gomega .Equal (float32 (0.8 )))
584+ }
585+
550586// TestInvalidNearCacheOptions runs tests to ensure that we can't create a named cache/map with invalid options.
551587func TestInvalidNearCacheOptions (t * testing.T ) {
552588 var (
@@ -591,6 +627,23 @@ func TestInvalidNearCacheOptions(t *testing.T) {
591627 _ , err = coherence .GetNamedMap [int , string ](session , nearMapName , coherence .WithNearCache (& nearCacheOptionsBad3 ))
592628 g .Expect (err ).Should (gomega .HaveOccurred ())
593629 g .Expect (err ).Should (gomega .Equal (coherence .ErrInvalidNearCacheWithNoTTL ))
630+
631+ // cannot have Invalid PruneFactor
632+ nearCacheOptionsBad4 := coherence.NearCacheOptions {HighUnits : 1000 , PruneFactor : - 1.0 }
633+ _ , err = coherence .GetNamedMap [int , string ](session , nearMapName , coherence .WithNearCache (& nearCacheOptionsBad4 ))
634+ g .Expect (err ).Should (gomega .HaveOccurred ())
635+ g .Expect (err ).Should (gomega .Equal (coherence .ErrInvalidPruneFactor ))
636+
637+ nearCacheOptionsBad4 .PruneFactor = 0.05
638+ _ , err = coherence .GetNamedMap [int , string ](session , nearMapName , coherence .WithNearCache (& nearCacheOptionsBad4 ))
639+ g .Expect (err ).Should (gomega .HaveOccurred ())
640+ g .Expect (err ).Should (gomega .Equal (coherence .ErrInvalidPruneFactor ))
641+
642+ nearCacheOptionsBad4 .PruneFactor = 1.01
643+ _ , err = coherence .GetNamedMap [int , string ](session , nearMapName , coherence .WithNearCache (& nearCacheOptionsBad4 ))
644+ g .Expect (err ).Should (gomega .HaveOccurred ())
645+ g .Expect (err ).Should (gomega .Equal (coherence .ErrInvalidPruneFactor ))
646+
594647}
595648
596649// TestIncompatibleNearCacheOptions runs tests to ensure that we can't create a named cache/map with incompatible.
0 commit comments