context: // **********************************************************************
  //
  // Copyright (c) 2003-2015 ZeroC, Inc. All rights reserved.
  //
  // This copy of Ice is licensed to you under the terms described in the
  // ICE_LICENSE file included in this distribution.
  //
  // **********************************************************************
  
  using Test;
  using System;
  using System.Collections.Generic;
  using System.Diagnostics;
  using System.Threading;
  
  public class AllTests
  {
      private static void test(bool b)
      {
          if(!b)
          {
              throw new System.Exception();
          }
      }
  
      private class Callback
      {
          internal Callback()
          {
              _called = false;
          }
  
          public virtual void check()
          {
              lock(this)
              {
                  while(!_called)
                  {
                      System.Threading.Monitor.Wait(this);
                  }
  
                  _called = false;
              }
          }
  
          public virtual void called()
          {
              lock(this)
              {
                  Debug.Assert(!_called);
                  _called = true;
                  System.Threading.Monitor.Pulse(this);
              }
          }
  
          public virtual bool isCalled()
          {
              lock(this)
              {
                  return _called;
              }
          }
  
          private bool _called;
      }
  
      private class OpAMICallback
      {
          public void response()
          {
              _response.called();
          }
  
          public void responseNoOp()
          {
          }
  
          public void noResponse()
          {
              test(false);
          }
  
          public void exception(Ice.Exception ex)
          {
              _response.called();
          }
  
          public void noException(Ice.Exception ex)
          {
              Console.Error.WriteLine(ex);
              test(false);
          }
  
          public void sent(bool ss)
          {
              _sent.called();
          }
  
          public bool checkException(bool wait)
          {
              if(wait)
              {
                  _response.check();
                  return true;
              }
              else
              {
                  return _response.isCalled();
              }
          }
  
          public bool checkResponse(bool wait)
          {
              if(wait)
              {
                  _response.check();
                  return true;
              }
              else
              {
                  return _response.isCalled();
              }
          }
  
          public void checkResponseAndSent()
          {
              _sent.check();
              _response.check();
          }
  
          private Callback _response = new Callback();
          private Callback _sent = new Callback();
      }
  
      private class OpThread
      {
          internal OpThread(BackgroundPrx background)
          {
              _background = BackgroundPrxHelper.uncheckedCast(background.ice_oneway());
              Start();
          }
  
          public void Join()
          {
              _thread.Join();
          }
  
          public void Start()
          {
              _thread = new Thread(new ThreadStart(Run));
              _thread.Start();
          }
  
          public void Run()
          {
              int count = 0;
              while(true)
              {
                  lock(this)
                  {
                      if(_destroyed)
                      {
                          return;
                      }
                  }
  
                  try
                  {
                      if(++count == 10) // Don't blast the connection with only oneway's
                      {
                          count = 0;
                          _background.ice_twoway().ice_ping();
                      }
                      _background.begin_op();
                      Thread.Sleep(1);
                  }
                  catch(Ice.LocalException)
                  {
                  }
              }
          }
  
          public void destroy()
          {
              lock(this)
              {
                  _destroyed = true;
              }
          }
  
          private bool _destroyed = false;
          private BackgroundPrx _background = null;
          private Thread _thread;
      }
  
      public static Test.BackgroundPrx allTests(Ice.Communicator communicator)
      {
          string sref = "background:default -p 12010";
          Ice.ObjectPrx obj = communicator.stringToProxy(sref);
          test(obj != null);
  
          BackgroundPrx background = BackgroundPrxHelper.uncheckedCast(obj);
  
          sref = "backgroundController:tcp -p 12011";
          obj = communicator.stringToProxy(sref);
          test(obj != null);
  
          BackgroundControllerPrx backgroundController = BackgroundControllerPrxHelper.uncheckedCast(obj);
  
          Configuration configuration = Configuration.getInstance();
  
          Console.Write("testing connect... ");
          Console.Out.Flush();
          {
              connectTests(configuration, background);
          }
          Console.Out.WriteLine("ok");
  
          Console.Write("testing initialization... ");
          Console.Out.Flush();
          {
              initializeTests(configuration, background, backgroundController);
          }
          Console.Out.WriteLine("ok");
  
          Console.Write("testing connection validation... ");
          Console.Out.Flush();
          {
              validationTests(configuration, background, backgroundController);
          }
          Console.Out.WriteLine("ok");
  
          Console.Write("testing read/write... ");
          Console.Out.Flush();
          {
              readWriteTests(configuration, background, backgroundController);
          }
          Console.Out.WriteLine("ok");
  
          Console.Write("testing locator... ");
          Console.Out.Flush();
          {
              Ice.LocatorPrx locator;
              obj = communicator.stringToProxy("locator:default -p 12010").ice_invocationTimeout(250);
              locator = Ice.LocatorPrxHelper.uncheckedCast(obj);
              obj = communicator.stringToProxy("background@Test").ice_locator(locator).ice_oneway();
  
              backgroundController.pauseCall("findAdapterById");
              try
              {
                  obj.ice_ping();
                  test(false);
              }
              catch(Ice.TimeoutException)
              {
              }
              backgroundController.resumeCall("findAdapterById");
  
              obj = communicator.stringToProxy("locator:default -p 12010");
              locator = Ice.LocatorPrxHelper.uncheckedCast(obj);
              obj = obj.ice_locator(locator);
              obj.ice_ping();
  
              obj = communicator.stringToProxy("background@Test").ice_locator(locator);
              BackgroundPrx bg = BackgroundPrxHelper.uncheckedCast(obj);
  
              backgroundController.pauseCall("findAdapterById");
              Ice.AsyncResult r1 = bg.begin_op();
              Ice.AsyncResult r2 = bg.begin_op();
              test(!r1.IsCompleted);
              test(!r2.IsCompleted);
              backgroundController.resumeCall("findAdapterById");
              bg.end_op(r1);
              bg.end_op(r2);
              test(r1.IsCompleted);
              test(r2.IsCompleted);
          }
          Console.Out.WriteLine("ok");
  
          Console.Write("testing router... ");
          Console.Out.Flush();
          {
              Ice.RouterPrx router;
  
              obj = communicator.stringToProxy("router:default -p 12010").ice_invocationTimeout(250);
              router = Ice.RouterPrxHelper.uncheckedCast(obj);
              obj = communicator.stringToProxy("background@Test").ice_router(router).ice_oneway();
  
              backgroundController.pauseCall("getClientProxy");
              try
              {
                  obj.ice_ping();
                  test(false);
              }
              catch(Ice.TimeoutException)
              {
              }
              backgroundController.resumeCall("getClientProxy");
  
              obj = communicator.stringToProxy("router:default -p 12010");
              router = Ice.RouterPrxHelper.uncheckedCast(obj);
              obj = communicator.stringToProxy("background@Test").ice_router(router);
              BackgroundPrx bg = BackgroundPrxHelper.uncheckedCast(obj);
              test(bg.ice_getRouter() != null);
  
              backgroundController.pauseCall("getClientProxy");
              Ice.AsyncResult r1 = bg.begin_op();
              Ice.AsyncResult r2 = bg.begin_op();
              test(!r1.IsCompleted);
              test(!r2.IsCompleted);
              backgroundController.resumeCall("getClientProxy");
              bg.end_op(r1);
              bg.end_op(r2);
              test(r1.IsCompleted);
              test(r2.IsCompleted);
          }
          Console.Out.WriteLine("ok");
  
          bool ws = communicator.getProperties().getProperty("Ice.Default.Protocol").Equals("test-ws");
          bool wss = communicator.getProperties().getProperty("Ice.Default.Protocol").Equals("test-wss");
  
gt:         if(!ws && !wss)