Skip to content
This repository has been archived by the owner. It is now read-only.
Permalink
Browse files
Switch to using statements
  • Loading branch information
mmartell committed May 27, 2021
1 parent 6f5668d commit edf9a349c1360ed327b9d69129f4f6560caef3eb
Showing 2 changed files with 202 additions and 173 deletions.
@@ -8,175 +8,201 @@

namespace Apache.Geode.Session.IntegrationTests
{
public class SessionStateIntegrationTests
{
private static string _regionName = "testRegion";

[Fact]
public void SetGet()
{
var cacheFactory = CacheFactory.Create()
.SetProperty("log-level", "none")
.SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;
var testValue = new byte[] { 1, 2, 3, 4, 5 };
ssCache.Set("testKey", testValue, options);
byte[] value = ssCache.Get("testKey");
Assert.True(testValue.SequenceEqual(value));
}

[Fact]
public void Refresh()
{
var cacheFactory = CacheFactory.Create()
.SetProperty("log-level", "none")
.SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
int numSeconds = 20;
options.SlidingExpiration = new TimeSpan(0, 0, numSeconds);
var testValue = new byte[] { 1, 2, 3, 4, 5 };

// Set a value
ssCache.Set("testKey", testValue, options);

// Wait half a timeout then refresh
System.Threading.Thread.Sleep(numSeconds/2 * 1000);
ssCache.Refresh("testKey");

// Wait beyond the original expiration
System.Threading.Thread.Sleep(numSeconds / 2 * 1000 + 1);

// Ensure it's not expired
byte[] value = ssCache.Get("testKey");
Assert.True(testValue.SequenceEqual(value));
}

[Fact]
public void SetWithAbsoluteExpiration()
{
var cacheFactory = CacheFactory.Create()
.SetProperty("log-level", "none")
.SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory;

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
options.AbsoluteExpiration = DateTime.Now.AddSeconds(5);
ssCache.Set("testKey", Encoding.UTF8.GetBytes("testValue"), options);
System.Threading.Thread.Sleep(6000);
byte[] value = ssCache.Get("testKey");
Assert.Null(value);
}

[Fact]
public void Remove()
{
var cacheFactory = CacheFactory.Create()
.SetProperty("log-level", "none")
.SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;
var testValue = new byte[] { 1, 2, 3, 4, 5 };
ssCache.Set("testKey", testValue, options);
byte[] value = ssCache.Get("testKey");

ssCache.Remove("testKey");
value = ssCache.Get("testKey");
Assert.Null(value);
}

[Fact]
public void SetGetRemoveAsync()
public class SessionStateIntegrationTests
{
var cacheFactory = CacheFactory.Create()
.SetProperty("log-level", "none")
.SetProperty("log-file", "SessionStateCacheIntegrationTests.log");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;

var testValue1 = new byte[] { 1, 2, 3, 4, 5 };
var testValue2 = new byte[] { 11, 12, 13, 14, 15 };
var testValue3 = new byte[] { 21, 22, 23, 24, 25 };
var testValue4 = new byte[] { 31, 32, 33, 34, 35 };
var testValue5 = new byte[] { 41, 42, 43, 44, 45 };

Task set1 = ssCache.SetAsync("testKey1", testValue1, options);
Task set2 = ssCache.SetAsync("testKey2", testValue2, options);
Task set3 = ssCache.SetAsync("testKey3", testValue3, options);
Task set4 = ssCache.SetAsync("testKey4", testValue4, options);
Task set5 = ssCache.SetAsync("testKey5", testValue5, options);

Task.WaitAll(set1, set2, set3, set4, set5);

Task<byte[]> value1 = ssCache.GetAsync("testKey1");
Task<byte[]> value2 = ssCache.GetAsync("testKey2");
Task<byte[]> value3 = ssCache.GetAsync("testKey3");
Task<byte[]> value4 = ssCache.GetAsync("testKey4");
Task<byte[]> value5 = ssCache.GetAsync("testKey5");

Task.WaitAll(value1, value2, value3, value4, value5);

Assert.True(testValue1.SequenceEqual(value1.Result));
Assert.True(testValue2.SequenceEqual(value2.Result));
Assert.True(testValue3.SequenceEqual(value3.Result));
Assert.True(testValue4.SequenceEqual(value4.Result));
Assert.True(testValue5.SequenceEqual(value5.Result));

Task rm1 = ssCache.RemoveAsync("testKey1");
Task rm2 = ssCache.RemoveAsync("testKey2");
Task rm3 = ssCache.RemoveAsync("testKey3");
Task rm4 = ssCache.RemoveAsync("testKey4");
Task rm5 = ssCache.RemoveAsync("testKey5");

Task.WaitAll(rm1, rm2, rm3, rm4, rm5);

Assert.Null(ssCache.Get("testKey1"));
Assert.Null(ssCache.Get("testKey2"));
Assert.Null(ssCache.Get("testKey3"));
Assert.Null(ssCache.Get("testKey4"));
Assert.Null(ssCache.Get("testKey5"));

private static string _regionName = "testRegion";

[Fact]
public void SetGet()
{
using (var client = new Client())
{
using (var cacheFactory = CacheFactory.Create(client))
{
cacheFactory.SetProperty("log-level", "none");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;
var testValue = new byte[] { 1, 2, 3, 4, 5 };
ssCache.Set("testKey", testValue, options);
byte[] value = ssCache.Get("testKey");
Assert.True(testValue.SequenceEqual(value));
pool.Dispose();
poolFactory.Dispose();
cache.Dispose();
cacheFactory.Dispose();
ssCache.Dispose();

}

}
}

[Fact]
public void Refresh()
{
using (var client = new Client())
{
using (var cacheFactory = CacheFactory.Create(client))
{
cacheFactory.SetProperty("log-level", "none");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
int numSeconds = 20;
options.SlidingExpiration = new TimeSpan(0, 0, numSeconds);
var testValue = new byte[] { 1, 2, 3, 4, 5 };

// Set a value
ssCache.Set("testKey", testValue, options);

// Wait half a timeout then refresh
System.Threading.Thread.Sleep(numSeconds / 2 * 1000);
ssCache.Refresh("testKey");

// Wait beyond the original expiration
System.Threading.Thread.Sleep(numSeconds / 2 * 1000 + 1);

// Ensure it's not expired
byte[] value = ssCache.Get("testKey");
Assert.True(testValue.SequenceEqual(value));
}
}
}

[Fact]
public void SetWithAbsoluteExpiration()
{
using (var client = new Client())
{
using (var cacheFactory = CacheFactory.Create(client))
{
cacheFactory.SetProperty("log-level", "none");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory;

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
options.AbsoluteExpiration = DateTime.Now.AddSeconds(5);
ssCache.Set("testKey", Encoding.UTF8.GetBytes("testValue"), options);
System.Threading.Thread.Sleep(6000);
byte[] value = ssCache.Get("testKey");
Assert.Null(value);
}
}
}

[Fact]
public void Remove()
{
using (var client = new Client())
{
using (var cacheFactory = CacheFactory.Create(client))
{
cacheFactory.SetProperty("log-level", "none");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;
var testValue = new byte[] { 1, 2, 3, 4, 5 };
ssCache.Set("testKey", testValue, options);
byte[] value = ssCache.Get("testKey");

ssCache.Remove("testKey");
value = ssCache.Get("testKey");
Assert.Null(value);
}
}
}

[Fact]
public void SetGetRemoveAsync()
{
using (var client = new Client())
{
using (var cacheFactory = CacheFactory.Create(client))
{
cacheFactory.SetProperty("log-level", "none");

var cache = (Cache)cacheFactory.CreateCache();
PoolFactory poolFactory = cache.PoolFactory.AddLocator("localhost", 10334);
var pool = poolFactory.CreatePool("myPool");

var ssCache = new SessionStateCache(cache, _regionName);

var options = new DistributedCacheEntryOptions();
DateTime localTime = DateTime.Now.AddDays(1);
DateTimeOffset dateAndOffset = new DateTimeOffset(localTime,
TimeZoneInfo.Local.GetUtcOffset(localTime));
options.AbsoluteExpiration = dateAndOffset;

var testValue1 = new byte[] { 1, 2, 3, 4, 5 };
var testValue2 = new byte[] { 11, 12, 13, 14, 15 };
var testValue3 = new byte[] { 21, 22, 23, 24, 25 };
var testValue4 = new byte[] { 31, 32, 33, 34, 35 };
var testValue5 = new byte[] { 41, 42, 43, 44, 45 };

Task set1 = ssCache.SetAsync("testKey1", testValue1, options);
Task set2 = ssCache.SetAsync("testKey2", testValue2, options);
Task set3 = ssCache.SetAsync("testKey3", testValue3, options);
Task set4 = ssCache.SetAsync("testKey4", testValue4, options);
Task set5 = ssCache.SetAsync("testKey5", testValue5, options);

Task.WaitAll(set1, set2, set3, set4, set5);

Task<byte[]> value1 = ssCache.GetAsync("testKey1");
Task<byte[]> value2 = ssCache.GetAsync("testKey2");
Task<byte[]> value3 = ssCache.GetAsync("testKey3");
Task<byte[]> value4 = ssCache.GetAsync("testKey4");
Task<byte[]> value5 = ssCache.GetAsync("testKey5");

Task.WaitAll(value1, value2, value3, value4, value5);

Assert.True(testValue1.SequenceEqual(value1.Result));
Assert.True(testValue2.SequenceEqual(value2.Result));
Assert.True(testValue3.SequenceEqual(value3.Result));
Assert.True(testValue4.SequenceEqual(value4.Result));
Assert.True(testValue5.SequenceEqual(value5.Result));

Task rm1 = ssCache.RemoveAsync("testKey1");
Task rm2 = ssCache.RemoveAsync("testKey2");
Task rm3 = ssCache.RemoveAsync("testKey3");
Task rm4 = ssCache.RemoveAsync("testKey4");
Task rm5 = ssCache.RemoveAsync("testKey5");

Task.WaitAll(rm1, rm2, rm3, rm4, rm5);

Assert.Null(ssCache.Get("testKey1"));
Assert.Null(ssCache.Get("testKey2"));
Assert.Null(ssCache.Get("testKey3"));
Assert.Null(ssCache.Get("testKey4"));
Assert.Null(ssCache.Get("testKey5"));
}
}
}
}
}
}
@@ -35,13 +35,16 @@ public void Dispose()
[Fact]
public void NullPoolFactory_Throws()
{
PoolFactory poolFactory = null;
var cacheFactory = CacheFactory.Create()
using (var client = new Client())
{
PoolFactory poolFactory = null;
var cacheFactory = CacheFactory.Create(client)
.SetProperty("log-level", "debug")
.SetProperty("log-file", "SessionStateCacheTests.log");

var cache = (Cache)cacheFactory.CreateCache();
Assert.Throws<ArgumentNullException>(() => new SessionStateCache(cache, _regionName));
var cache = (Cache)cacheFactory.CreateCache();
Assert.Throws<ArgumentNullException>(() => new SessionStateCache(cache, _regionName));
}
}
}
}

0 comments on commit edf9a34

Please sign in to comment.