You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by mm...@apache.org on 2021/05/27 23:50:58 UTC

[geode-dotnet-core-client] 03/04: Switch to using statements

This is an automated email from the ASF dual-hosted git repository.

mmartell pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-dotnet-core-client.git

commit edf9a349c1360ed327b9d69129f4f6560caef3eb
Author: Mike Martell <mm...@pivotal.io>
AuthorDate: Thu May 27 16:49:44 2021 -0700

    Switch to using statements
---
 .../SessionStateIntegrationTests.cs                | 364 +++++++++++----------
 Session.Tests/SessionStateCacheTests.cs            |  11 +-
 2 files changed, 202 insertions(+), 173 deletions(-)

diff --git a/Session.IntegrationTests/SessionStateIntegrationTests.cs b/Session.IntegrationTests/SessionStateIntegrationTests.cs
index 42250b7..7a533f0 100644
--- a/Session.IntegrationTests/SessionStateIntegrationTests.cs
+++ b/Session.IntegrationTests/SessionStateIntegrationTests.cs
@@ -8,175 +8,201 @@ using System.Threading.Tasks;
 
 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"));
+                }
+            }
+        }
     }
-  }
 }
diff --git a/Session.Tests/SessionStateCacheTests.cs b/Session.Tests/SessionStateCacheTests.cs
index 2d49d55..ac64254 100644
--- a/Session.Tests/SessionStateCacheTests.cs
+++ b/Session.Tests/SessionStateCacheTests.cs
@@ -35,13 +35,16 @@ namespace Apache.Geode.Session.Tests
     [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));
+      }
     }
   }
 }