GEODE-9719: Start/Stop cluster in a test fixture (#881)

* Start/Stop cluster in a test fixture
* Create an authRegion in same cluster
* Turn off cluster output after all tests run
diff --git a/netcore/netcore-integration-test/NetCoreTestFixture.cs b/netcore/netcore-integration-test/NetCoreTestFixture.cs
new file mode 100644
index 0000000..992a7b3
--- /dev/null
+++ b/netcore/netcore-integration-test/NetCoreTestFixture.cs
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+using System;
+using Xunit;
+using Xunit.Abstractions;
+using Apache.Geode.Client.IntegrationTests;
+
+namespace Apache.Geode.Client.IntegrationTests
+{
+  public class NetCoreTestFixture : IDisposable
+  {
+    internal Cluster cluster;
+    public ITestOutputHelper Output { get; set; }
+
+    public NetCoreTestFixture SetOutPut(ITestOutputHelper output)
+    {
+      Output = output;
+      return this;
+    }
+
+    public void CreateCluster()
+    {
+      if (cluster == null)
+      {
+        cluster = new Cluster(Output, "NetCoreTests", 1, 1);
+
+        Assert.True(cluster.Start());
+        Assert.Equal(0, cluster.Gfsh
+            .create()
+            .region()
+            .withName("exampleRegion")
+            .withType("PARTITION")
+            .execute());
+
+        Assert.Equal(0, cluster.Gfsh
+            .create()
+            .region()
+            .withName("authExampleRegion")
+            .withType("PARTITION")
+            .execute());
+      }
+    }
+
+    public void Dispose()
+    {
+      // At this point, the last test has executed and the ITestOutputHelper is
+      // no longer available. Hence set the output to null in the cluster.
+      cluster.Gfsh.Output = null;
+      cluster.Dispose();
+      Output = null;
+    }
+  }
+}
diff --git a/netcore/netcore-integration-test/RegionFactoryTest.cs b/netcore/netcore-integration-test/RegionFactoryTest.cs
index 9f0e6d9..edac8ad 100644
--- a/netcore/netcore-integration-test/RegionFactoryTest.cs
+++ b/netcore/netcore-integration-test/RegionFactoryTest.cs
@@ -36,10 +36,15 @@
   }
 
   [Collection("Geode .Net Core Collection")]
-  public class RegionFactoryTest : TestBase
+  public class RegionFactoryTest : TestBase, IClassFixture<NetCoreTestFixture>
   {
-    public RegionFactoryTest(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
+    NetCoreTestFixture fixture;
+
+    public RegionFactoryTest(NetCoreTestFixture fixture, ITestOutputHelper testOutputHelper) : base(testOutputHelper)
     {
+      this.fixture = fixture;
+      this.fixture.Output = testOutputHelper;
+      this.fixture.CreateCluster();
     }
 
     private const string Username1 = "rtimmons";
@@ -88,41 +93,19 @@
 
     [Fact]
     public void RegionFactoryCreateProxyRegionStringPutGet() {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("exampleRegion")
-            .withType("PARTITION")
-            .execute());
-
         using var cacheFactory = CacheFactory.Create()
                                      .SetProperty("log-level", "debug")
                                      .SetProperty("log-file", "geode_native.log");
         using var cache = cacheFactory.CreateCache();
 
-        using var pool = cluster.ApplyLocators(cache.PoolFactory)
+        using var pool = fixture.cluster.ApplyLocators(cache.PoolFactory)
                     .CreatePool("myPool");
 
-        //int port = cluster.
-        //createPool(cache, 10334);
         CreateRegionAndDoWork(cache, "exampleRegion", RegionShortcut.Proxy);
-      }
     }
 
     [Fact]
     public void RegionFactoryCreateRegionStringPutGetWithAuthentication() {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("authExampleRegion")
-            .withType("PARTITION")
-            .execute());
 
         using var cacheFactory = CacheFactory.Create()
                                    .SetProperty("log-level", "debug")
@@ -130,11 +113,10 @@
       cacheFactory.AuthInitialize = new SimpleAuthInitialize();
       using var cache = cacheFactory.CreateCache();
 
-      using var pool = cluster.ApplyLocators(cache.PoolFactory)
+      using var pool = fixture.cluster.ApplyLocators(cache.PoolFactory)
                     .CreatePool("myPool");
 
         CreateRegionAndDoWork(cache, "authExampleRegion", RegionShortcut.CachingProxy);
-      }
     }
   }
 }
diff --git a/netcore/netcore-session-integration-test/SessionStateIntegrationTests.cs b/netcore/netcore-session-integration-test/SessionStateIntegrationTests.cs
index 6930c7e..ad2d867 100644
--- a/netcore/netcore-session-integration-test/SessionStateIntegrationTests.cs
+++ b/netcore/netcore-session-integration-test/SessionStateIntegrationTests.cs
@@ -22,20 +22,27 @@
 using Xunit;
 using Xunit.Abstractions;
 using Apache.Geode.Client.IntegrationTests;
+using Xunit.Sdk;
+using System.Reflection;
 
 namespace Apache.Geode.Session.IntegrationTests {
-  [Collection("Geode .Net Core Collection")]
-  public class SessionStateIntegrationTests : TestBase {
+  public class NetCoreSessionTestFixture : IDisposable
+  {
+    internal Cluster cluster;
+    public ITestOutputHelper Output { get; set; }
 
-    public SessionStateIntegrationTests(ITestOutputHelper testOutputHelper) : base(testOutputHelper)
+    public NetCoreSessionTestFixture SetOutPut(ITestOutputHelper output)
     {
+      Output = output;
+      return this;
     }
 
-    [Fact]
-    public void SetGet()
+    public void CreateCluster()
     {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
+      if (cluster == null)
       {
+        cluster = new Cluster(Output, "GeodeSessionStateTests", 1, 1);
+
         Assert.True(cluster.Start());
         Assert.Equal(0, cluster.Gfsh
             .create()
@@ -43,10 +50,37 @@
             .withName("exampleRegion")
             .withType("PARTITION")
             .execute());
+      }
+    }
 
+    public void Dispose()
+    {
+      // At this point, the last test has executed and the ITestOutputHelper is
+      // no longer available. Hence set the output to null in the cluster.
+      cluster.Gfsh.Output = null;
+      cluster.Dispose();
+      Output = null;
+    }
+  }
+
+  [Collection("Geode .Net Core Collection")]
+  public class SessionStateIntegrationTests : TestBase,  IClassFixture<NetCoreSessionTestFixture>
+  {
+    NetCoreSessionTestFixture fixture;
+
+    public SessionStateIntegrationTests(NetCoreSessionTestFixture fixture, ITestOutputHelper testOutputHelper) : base(testOutputHelper)
+    {
+      this.fixture = fixture;
+      this.fixture.Output = testOutputHelper;
+      this.fixture.CreateCluster();
+    }
+
+    [Fact]
+    public void SetGet()
+    {
         var ssCacheOptions = new GeodeSessionStateCacheOptions();
         ssCacheOptions.Host = "localhost";
-        ssCacheOptions.Port = cluster.Locators[0].Address.port;
+        ssCacheOptions.Port = fixture.cluster.Locators[0].Address.port;
         ssCacheOptions.RegionName = "exampleRegion";
 
         using var ssCache = new GeodeSessionStateCache(ssCacheOptions);
@@ -61,24 +95,13 @@
         var value = ssCache.Get("testKey");
         Assert.True(testValue.SequenceEqual(value));
       }
-    }
 
     [Fact]
     public void Refresh()
     {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("exampleRegion")
-            .withType("PARTITION")
-            .execute());
-
         var ssCacheOptions = new GeodeSessionStateCacheOptions();
         ssCacheOptions.Host = "localhost";
-        ssCacheOptions.Port = cluster.Locators[0].Address.port;
+        ssCacheOptions.Port = fixture.cluster.Locators[0].Address.port;
         ssCacheOptions.RegionName = "exampleRegion";
 
         using var ssCache = new GeodeSessionStateCache(ssCacheOptions);
@@ -101,25 +124,14 @@
         // Ensure it's not expired
         var value = ssCache.Get("testKey");
         Assert.True(testValue.SequenceEqual(value));
-      }
     }
 
     [Fact]
     public void SetWithAbsoluteExpiration()
     {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("exampleRegion")
-            .withType("PARTITION")
-            .execute());
-
         var ssCacheOptions = new GeodeSessionStateCacheOptions();
         ssCacheOptions.Host = "localhost";
-        ssCacheOptions.Port = cluster.Locators[0].Address.port;
+        ssCacheOptions.Port = fixture.cluster.Locators[0].Address.port;
         ssCacheOptions.RegionName = "exampleRegion";
 
         using var ssCache = new GeodeSessionStateCache(ssCacheOptions);
@@ -130,25 +142,14 @@
         System.Threading.Thread.Sleep(6000);
         var value = ssCache.Get("testKey");
         Assert.Null(value);
-      }
     }
 
     [Fact]
     public void Remove()
     {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("exampleRegion")
-            .withType("PARTITION")
-            .execute());
-
         var ssCacheOptions = new GeodeSessionStateCacheOptions();
         ssCacheOptions.Host = "localhost";
-        ssCacheOptions.Port = cluster.Locators[0].Address.port;
+        ssCacheOptions.Port = fixture.cluster.Locators[0].Address.port;
         ssCacheOptions.RegionName = "exampleRegion";
 
         using var ssCache = new GeodeSessionStateCache(ssCacheOptions);
@@ -165,25 +166,14 @@
         ssCache.Remove("testKey");
         value = ssCache.Get("testKey");
         Assert.Null(value);
-      }
     }
 
     [Fact]
     public void SetGetRemoveAsync()
     {
-      using (var cluster = new Cluster(output, CreateTestCaseDirectoryName(), 1, 1))
-      {
-        Assert.True(cluster.Start());
-        Assert.Equal(0, cluster.Gfsh
-            .create()
-            .region()
-            .withName("exampleRegion")
-            .withType("PARTITION")
-            .execute());
-
         var ssCacheOptions = new GeodeSessionStateCacheOptions();
         ssCacheOptions.Host = "localhost";
-        ssCacheOptions.Port = cluster.Locators[0].Address.port;
+        ssCacheOptions.Port = fixture.cluster.Locators[0].Address.port;
         ssCacheOptions.RegionName = "exampleRegion";
 
         using var ssCache = new GeodeSessionStateCache(ssCacheOptions);
@@ -237,5 +227,4 @@
         Assert.Null(ssCache.Get("testKey5"));
       }
     }
-  }
 }
diff --git a/netcore/shared/Cluster.cs b/netcore/shared/Cluster.cs
index c3d0004..cd0e2be 100644
--- a/netcore/shared/Cluster.cs
+++ b/netcore/shared/Cluster.cs
@@ -35,7 +35,7 @@
         internal string trustStore_ = Config.SslServerKeyPath + "/server_truststore_chained_root.jks";
         internal string trustStorePassword_ = "apachegeode";
 
-        public Gfsh Gfsh { get; private set; }
+        public GfshExecute Gfsh { get; private set; }
 
         public bool UseSSL { get; set; }
 
diff --git a/netcore/shared/GfshExecute.cs b/netcore/shared/GfshExecute.cs
index 6171783..99603d0 100644
--- a/netcore/shared/GfshExecute.cs
+++ b/netcore/shared/GfshExecute.cs
@@ -28,6 +28,13 @@
         private String connectionCommand_ = null;
         private ITestOutputHelper output;
 
+        public ITestOutputHelper Output
+        {
+          get { return output; }
+          set { output = value; }
+        }
+
+
         public GfshExecute(ITestOutputHelper output)
         {
             this.output = output;