Ejemplo n.º 1
0
    private static void initializeTests(Configuration configuration, BackgroundPrx background,
                                        BackgroundControllerPrx ctl)
    {
        try
        {
            background.op();
        }
        catch (LocalException)
        {
            test(false);
        }
        background.GetConnection().close(ConnectionClose.GracefullyWithWait);

        for (int i = 0; i < 4; ++i)
        {
            if (i == 0 || i == 2)
            {
                configuration.initializeException(new SocketException());
            }
            else
            {
                continue;
            }
            BackgroundPrx prx = (i == 1 || i == 3) ? background : background.Clone(oneway: true);

            try
            {
                prx.op();
                test(false);
            }
            catch (SocketException)
            {
            }

            bool sentSynchronously = false;
            var  t = prx.opAsync(progress: new Progress <bool>(value =>
            {
                sentSynchronously = value;
            }));
            test(!sentSynchronously);
            try
            {
                t.Wait();
                test(false);
            }
            catch (AggregateException ex) when(ex.InnerException is Ice.Exception)
            {
            }
            test(t.IsCompleted);

            OpAMICallback cbEx = new OpAMICallback();
            t = prx.opAsync(progress: new Progress <bool>(value =>
            {
                sentSynchronously = false;
            }));
            test(!sentSynchronously);
            try
            {
                t.Wait();
            }
            catch (AggregateException ex) when(ex.InnerException is Ice.Exception)
            {
                cbEx.exception((Ice.Exception)ex.InnerException);
            }
            cbEx.checkException(true);
            test(t.IsCompleted);

            if (i == 0 || i == 2)
            {
                configuration.initializeException(null);
            }
        }

        //
        // Now run the same tests with the server side.
        //

        try
        {
            ctl.initializeException(true);
            background.op();
            test(false);
        }
        catch (ConnectionLostException)
        {
            ctl.initializeException(false);
        }
        catch (SecurityException)
        {
            ctl.initializeException(false);
        }

        OpThread thread1 = new OpThread(background);
        OpThread thread2 = new OpThread(background);

        for (int i = 0; i < 5; i++)
        {
            try
            {
                background.IcePing();
            }
            catch (LocalException)
            {
                test(false);
            }

            configuration.initializeException(new SocketException());
            background.GetCachedConnection() !.close(ConnectionClose.Forcefully);
            Thread.Sleep(10);
            configuration.initializeException(null);
            try
            {
                background.IcePing();
            }
            catch (LocalException)
            {
            }
            try
            {
                background.IcePing();
            }
            catch (LocalException)
            {
                test(false);
            }

            background.GetCachedConnection() !.close(ConnectionClose.Forcefully);
            background.IcePing();

            ctl.initializeException(true);
            background.GetCachedConnection() !.close(ConnectionClose.Forcefully);
            Thread.Sleep(10);
            ctl.initializeException(false);
            try
            {
                background.IcePing();
            }
            catch (LocalException)
            {
            }
            try
            {
                background.IcePing();
            }
            catch (LocalException)
            {
                test(false);
            }

            try
            {
                background.GetCachedConnection() !.close(ConnectionClose.Forcefully);
                background.op();
            }
            catch (LocalException)
            {
                test(false);
            }
        }

        thread1.destroy();
        thread2.destroy();

        thread1.Join();
        thread2.Join();
    }