07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

254<br />

<strong>Enterprise</strong> <strong>Library</strong> <strong>Test</strong> <strong>Guide</strong><br />

<strong>Test</strong>ing the Caching Application Block<br />

The test case checked to see that the Caching Application Block did not try to scavenge<br />

items in the cache when the number of items to scavenge was set to zero.<br />

<strong>Test</strong> Case<br />

The test case checked to see that the ScavengerTask class looked at the number of<br />

times to scavenge before it began scavenging.<br />

Problem<br />

The test case failed because the DoScavenging method did not check the number of<br />

items to be scavenged before it began to scavenge. As a result, each time an item was<br />

added, the method would unnecessarily scavenge the cache. The following is the<br />

code that caused the problem.<br />

public void DoScavenging()<br />

{<br />

Hashtable liveCacheRepresentation = cacheOperations.CurrentCacheState;<br />

int currentNumberItemsInCache = liveCacheRepresentation.Count;<br />

if (scavengingPolicy.IsScavengingNeeded(currentNumberItemsInCache))<br />

{<br />

ResetScavengingFlagInCacheItems(liveCacheRepresentation);<br />

SortedList scavengableItems = SortItemsForScavenging(liveCacheRepresentation);<br />

RemoveScavengableItems(scavengableItems);<br />

}<br />

}<br />

Solution<br />

The ScavengerTask.DoScavenging method was modified so that it first checked to<br />

see if the number of items to be scavenged was set to zero. If it was, it did not scavenge<br />

the cache. The following is the modified code.<br />

public void DoScavenging()<br />

{<br />

if (NumberOfItemsToBeScavenged == 0) return;<br />

Hashtable liveCacheRepresentation = cacheOperations.CurrentCacheState;<br />

int currentNumberItemsInCache = liveCacheRepresentation.Count;<br />

if (scavengingPolicy.IsScavengingNeeded(currentNumberItemsInCache))<br />

{<br />

ResetScavengingFlagInCacheItems(liveCacheRepresentation);<br />

SortedList scavengableItems = SortItemsForScavenging(liveCacheRepresentation);<br />

RemoveScavengableItems(scavengableItems);<br />

}<br />

}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!