Repository: reef
Updated Branches:
  refs/heads/master be442aedc -> cc98b2fcd


[REEF-1324] Fix TcpConnectionRetryTest.TestConnectionRetries

This addressed the issue by
  * introducing customized `TcpClientConnectionException` class that has
    retries as field inside
  * catching this exception in test and comparing retry values

JIRA:
  [REEF-1324](https://issues.apache.org/jira/browse/REEF-1324)

Pull Request:
  This closes #935


Project: http://git-wip-us.apache.org/repos/asf/reef/repo
Commit: http://git-wip-us.apache.org/repos/asf/reef/commit/cc98b2fc
Tree: http://git-wip-us.apache.org/repos/asf/reef/tree/cc98b2fc
Diff: http://git-wip-us.apache.org/repos/asf/reef/diff/cc98b2fc

Branch: refs/heads/master
Commit: cc98b2fcd0ea632053414a6dc28edf1a2a2fa5e6
Parents: be442ae
Author: Dhruv Mahajan <[email protected]>
Authored: Thu Apr 7 19:25:12 2016 -0700
Committer: Markus Weimer <[email protected]>
Committed: Mon Apr 11 12:27:02 2016 -0700

----------------------------------------------------------------------
 .../TcpConnectionRetryTest.cs                   | 29 ++++-----
 .../Org.Apache.REEF.Wake.csproj                 |  1 +
 .../Remote/Impl/TcpClientConnectionException.cs | 68 ++++++++++++++++++++
 .../Remote/Impl/TcpClientConnectionFactory.cs   | 13 +++-
 4 files changed, 93 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/reef/blob/cc98b2fc/lang/cs/Org.Apache.REEF.Wake.Tests/TcpConnectionRetryTest.cs
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.Wake.Tests/TcpConnectionRetryTest.cs 
b/lang/cs/Org.Apache.REEF.Wake.Tests/TcpConnectionRetryTest.cs
index e314894..2fd1c39 100644
--- a/lang/cs/Org.Apache.REEF.Wake.Tests/TcpConnectionRetryTest.cs
+++ b/lang/cs/Org.Apache.REEF.Wake.Tests/TcpConnectionRetryTest.cs
@@ -20,6 +20,7 @@ using System.IO;
 using System.Net;
 using Org.Apache.REEF.Tang.Implementations.Tang;
 using Org.Apache.REEF.Wake.Remote;
+using Org.Apache.REEF.Wake.Remote.Impl;
 using Org.Apache.REEF.Wake.Remote.Parameters;
 using Xunit;
 
@@ -38,7 +39,6 @@ namespace Org.Apache.REEF.Wake.Tests
             IPAddress localIpAddress = IPAddress.Parse("127.0.0.1");
             const int retryCount = 5;
             const int sleepTimeInMs = 500;
-            const string message = "Retry - Count:";
             IPEndPoint remoteEndpoint = new IPEndPoint(localIpAddress, 8900);
 
             var memStream = new MemoryStream();
@@ -55,23 +55,22 @@ namespace Org.Apache.REEF.Wake.Tests
             try
             {
                 tmp.Connect(remoteEndpoint);
-                Assert.False(true);
+                Assert.False(true, "The connection is supposed to be 
unsuccessful since server is not present.");
             }
-            catch
+            catch (Exception e)
             {
-                memStream.Position = 0;
-                using (var reader = new StreamReader(memStream))
+                var exception = e as TcpClientConnectionException;
+                if (exception != null)
                 {
-                    string line;
-                    int counter = 0;
-                    while ((line = reader.ReadLine()) != null)
-                    {
-                        if (line.Contains(message))
-                        {
-                            counter++;
-                        }
-                    }
-                    Assert.Equal(counter, retryCount);
+                    bool areEqual = exception.RetriesDone == retryCount;
+                    string failureMsg = string.Format("Expected {0} retries 
but only {1} were done",
+                        retryCount,
+                        exception.RetriesDone);
+                    Assert.True(areEqual, failureMsg);
+                }
+                else
+                {
+                    throw;
                 }
             }
         }

http://git-wip-us.apache.org/repos/asf/reef/blob/cc98b2fc/lang/cs/Org.Apache.REEF.Wake/Org.Apache.REEF.Wake.csproj
----------------------------------------------------------------------
diff --git a/lang/cs/Org.Apache.REEF.Wake/Org.Apache.REEF.Wake.csproj 
b/lang/cs/Org.Apache.REEF.Wake/Org.Apache.REEF.Wake.csproj
index f25c6bf..b027c45 100644
--- a/lang/cs/Org.Apache.REEF.Wake/Org.Apache.REEF.Wake.csproj
+++ b/lang/cs/Org.Apache.REEF.Wake/Org.Apache.REEF.Wake.csproj
@@ -70,6 +70,7 @@ under the License.
     <Compile Include="IObserverFactory.cs" />
     <Compile Include="IStage.cs" />
     <Compile Include="Properties\AssemblyInfo.cs" />
+    <Compile Include="Remote\Impl\TcpClientConnectionException.cs" />
     <Compile Include="Remote\IRemoteObserver.cs" />
     <Compile Include="Remote\Impl\TcpClientConnectionFactory.cs" />
     <Compile Include="Remote\ITcpClientConnectionFactory.cs" />

http://git-wip-us.apache.org/repos/asf/reef/blob/cc98b2fc/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionException.cs
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionException.cs 
b/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionException.cs
new file mode 100644
index 0000000..1f1bf86
--- /dev/null
+++ b/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionException.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;
+
+namespace Org.Apache.REEF.Wake.Remote.Impl
+{
+    /// <summary>
+    /// Exception class for the case when Tcp Client connection fails 
+    /// after trying for some finite number of times.
+    /// For example, see TcpClientConnectionFactory.cs
+    /// </summary>
+    public sealed class TcpClientConnectionException : Exception
+    {
+        /// <summary>
+        /// Number of retries done before exception was thrown
+        /// </summary>
+        public int RetriesDone { get; private set; }
+
+        /// <summary>
+        /// Constructor
+        /// </summary>
+        /// <param name="message">The user message for exception</param>
+        /// <param name="retriesDone">Number of retries</param>
+        public TcpClientConnectionException(string message, int retriesDone)
+            : base(message)
+        {
+            RetriesDone = retriesDone;
+        }
+
+        /// <summary>
+        /// Constructor
+        /// </summary>
+        /// <param name="message">The user message for exception</param>
+        /// <param name="inner">The actual exception message due to which 
connection failed</param>
+        /// <param name="retriesDone">Number of retries</param>
+        public TcpClientConnectionException(string message, Exception inner, 
int retriesDone)
+            : base(message, inner)
+        {
+            RetriesDone = retriesDone;
+        }
+
+        /// <summary>
+        /// Appends the number of retries to the exception message.
+        /// </summary>
+        public override string Message
+        {
+            get
+            {
+                return base.Message + string.Format(", RetriesDone={0}", 
RetriesDone);
+            }
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/reef/blob/cc98b2fc/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionFactory.cs
----------------------------------------------------------------------
diff --git 
a/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionFactory.cs 
b/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionFactory.cs
index 82a4445..e6cc0a1 100644
--- a/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionFactory.cs
+++ b/lang/cs/Org.Apache.REEF.Wake/Remote/Impl/TcpClientConnectionFactory.cs
@@ -49,18 +49,25 @@ namespace Org.Apache.REEF.Wake.Remote.Impl
         public TcpClient Connect(IPEndPoint endPoint)
         {
             TcpClient client = new TcpClient();
+            int tryCounter = 0;
 
             try
             {
-                _retryHandler.Policy.ExecuteAction(() => 
client.Connect(endPoint));
+                _retryHandler.Policy.ExecuteAction(() =>
+                {
+                    tryCounter++;
+                    client.Connect(endPoint);
+                });
                 var msg = string.Format("Connection to endpoint {0} 
established", endPoint);
                 Logger.Log(Level.Info, msg);
                 return client;
             }
             catch (Exception e)
             {
-                var msg = string.Format("Connection to endpoint {0} failed", 
endPoint);
-                Exceptions.CaughtAndThrow(e, Level.Error, msg, Logger);
+                var msg = string.Format("Retried {0} times but connection to 
endpoint {1} failed",
+                    tryCounter - 1,
+                    endPoint);
+                Exceptions.CaughtAndThrow(new 
TcpClientConnectionException(msg, e, tryCounter - 1), Level.Error, Logger);
                 return null;
             }
         }

Reply via email to