[REEF-1473] Fix CanRunClrBridgeExampleOnLocalRuntime failures in AppVeyor

* Add retry in getting driverurl before sending http request
* Not to closet the context until receives client's request and all tasks are
  completed to ensure the web request can be received by the driver before the
  driver is shut down.

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

Pull Request:
  This closes #1269
diff --git a/lang/cs/Org.Apache.REEF.Examples.AllHandlers/AllHandlers.cs b/lang/cs/Org.Apache.REEF.Examples.AllHandlers/AllHandlers.cs
index 3e07cf3..97b3603 100644
--- a/lang/cs/Org.Apache.REEF.Examples.AllHandlers/AllHandlers.cs
+++ b/lang/cs/Org.Apache.REEF.Examples.AllHandlers/AllHandlers.cs
@@ -59,6 +59,7 @@
                 .Set(DriverConfiguration.OnTaskCompleted, GenericType<HelloTaskCompletedHandler>.Class)
                 .Set(DriverConfiguration.OnDriverStarted, GenericType<HelloDriverStartHandler>.Class)
                 .Set(DriverConfiguration.OnHttpEvent, GenericType<HelloHttpHandler>.Class)
+                .Set(DriverConfiguration.OnHttpEvent, GenericType<HelloTaskCompletedHandler>.Class)
                 .Set(DriverConfiguration.OnEvaluatorCompleted, GenericType<HelloCompletedEvaluatorHandler>.Class)
                 .Set(DriverConfiguration.CustomTraceListeners, GenericType<DefaultCustomTraceListener>.Class)
                 .Set(DriverConfiguration.CustomTraceLevel, Level.Info.ToString())
diff --git a/lang/cs/Org.Apache.REEF.Examples.AllHandlers/HelloTaskCompletedHandler.cs b/lang/cs/Org.Apache.REEF.Examples.AllHandlers/HelloTaskCompletedHandler.cs
index 4fc14a6..e2d0969 100644
--- a/lang/cs/Org.Apache.REEF.Examples.AllHandlers/HelloTaskCompletedHandler.cs
+++ b/lang/cs/Org.Apache.REEF.Examples.AllHandlers/HelloTaskCompletedHandler.cs
@@ -16,16 +16,27 @@
 // under the License.
 
 using System;
+using System.Collections.Generic;
+using System.Globalization;
+using System.Net;
+using System.Threading;
+using Org.Apache.REEF.Driver.Bridge;
 using Org.Apache.REEF.Driver.Task;
 using Org.Apache.REEF.Tang.Annotations;
+using Org.Apache.REEF.Utilities;
+using Org.Apache.REEF.Utilities.Logging;
 
 namespace Org.Apache.REEF.Examples.AllHandlers
 {
     /// <summary>
     /// A sample implementation of TaskCompletedHandler
     /// </summary>
-    public sealed class HelloTaskCompletedHandler : IObserver<ICompletedTask>
+    public sealed class HelloTaskCompletedHandler : IObserver<ICompletedTask>, IHttpHandler
     {
+        private static readonly Logger Logger = Logger.GetLogger(typeof(HelloTaskCompletedHandler));
+
+        private readonly IList<ICompletedTask> _completedTasks = new List<ICompletedTask>();
+
         [Inject]
         private HelloTaskCompletedHandler()
         {
@@ -38,9 +49,7 @@
         public void OnNext(ICompletedTask completedTask)
         {
             Console.WriteLine("Received CompletedTask: {0}", completedTask.Id);
-            using (completedTask.ActiveContext)
-            {
-            }
+            _completedTasks.Add(completedTask);
         }
 
         public void OnError(Exception error)
@@ -52,5 +61,35 @@
         {
             throw new NotImplementedException();
         }
+
+        public string GetSpecification()
+        {
+            return "CMD";
+        }
+
+        /// <summary>
+        /// It gets HTTP request and close the contexts after receiving all the completed tasks
+        /// </summary>
+        /// <param name="requet"></param>
+        /// <param name="resonse"></param>
+        public void OnHttpRequest(ReefHttpRequest requet, ReefHttpResponse resonse)
+        {
+            var msg = string.Format(CultureInfo.CurrentCulture,
+                      "HelloTaskCompletedHandler OnHttpRequest: URL: {0}, QueryString: {1}, inputStream: {2}.",
+                      requet.Url, requet.Querystring, ByteUtilities.ByteArraysToString(requet.InputStream));
+            Logger.Log(Level.Info, msg);
+            resonse.Status = HttpStatusCode.OK;
+            while (_completedTasks.Count != 2)
+            {
+                Thread.Sleep(1000);
+                Logger.Log(Level.Info, "_completedTasks received:" + _completedTasks.Count);
+            }
+            resonse.OutputStream = ByteUtilities.StringToByteArrays("Stopped!!!");
+            foreach (var t in _completedTasks)
+            {
+                t.ActiveContext.Dispose();
+            }
+            _completedTasks.Clear();
+        }
     }
 }
\ No newline at end of file
diff --git a/lang/cs/Org.Apache.REEF.Tests/Functional/Bridge/TestBridgeClient.cs b/lang/cs/Org.Apache.REEF.Tests/Functional/Bridge/TestBridgeClient.cs
index be6bfc1..a591334 100644
--- a/lang/cs/Org.Apache.REEF.Tests/Functional/Bridge/TestBridgeClient.cs
+++ b/lang/cs/Org.Apache.REEF.Tests/Functional/Bridge/TestBridgeClient.cs
@@ -40,7 +40,6 @@
         [Trait("Priority", "1")]
         [Trait("Category", "FunctionalGated")]
         [Trait("Description", "Run CLR Bridge on local runtime")]
-        //// TODO[JIRA REEF-1184]: add timeout 180 sec
         public async Task CanRunClrBridgeExampleOnLocalRuntime()
         {
             string testRuntimeFolder = DefaultRuntimeFolder + TestId;
@@ -54,12 +53,30 @@
             string[] a = { runOnYarn ? "yarn" : "local", testRuntimeFolder };
             IJobSubmissionResult driverHttpEndpoint = AllHandlers.Run(a);
 
-            var uri = driverHttpEndpoint.DriverUrl + "NRT/status?a=1&b=2";
-            var strStatus = driverHttpEndpoint.GetUrlResult(uri);
-            Assert.NotNull(strStatus);
-            Assert.True(strStatus.Equals("Byte array returned from HelloHttpHandler in CLR!!!\r\n"));
+            var driverUrl = driverHttpEndpoint.DriverUrl;
 
-            await((JobSubmissionResult)driverHttpEndpoint).TryUntilNoConnection(uri);
+            int retryCount = 1;
+            while (string.IsNullOrEmpty(driverUrl) && retryCount < 10)
+            {
+                driverUrl = driverHttpEndpoint.DriverUrl;
+                retryCount++;
+            }
+
+            if (driverUrl != null)
+            {
+                var uri = driverUrl + "NRT/status?a=1&b=2";
+                var strStatus = driverHttpEndpoint.GetUrlResult(uri);
+                Assert.NotNull(strStatus);
+                Assert.True(strStatus.Equals("Byte array returned from HelloHttpHandler in CLR!!!\r\n"));
+
+                var uri1 = driverUrl + "CMD/Stop?c=1";
+                var strStatus1 = driverHttpEndpoint.GetUrlResult(uri1);
+
+                Assert.NotNull(strStatus1);
+                Assert.True(strStatus1.Equals("Stopped!!!\r\n"));
+
+                await((JobSubmissionResult)driverHttpEndpoint).TryUntilNoConnection(uri);
+            }
         }
     }
 }