summaryrefslogtreecommitdiff
path: root/csharp/test/Ice/protocolBridging/AllTests.cs
blob: 42a23f02e5d7b54be0ddb2985ddd942dc5e7df33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) ZeroC, Inc. All rights reserved.

using System.Collections.Generic;
using System.Threading.Tasks;
using ZeroC.Test;

namespace ZeroC.Ice.Test.ProtocolBridging
{
    public static class AllTests
    {
        public static async Task RunAsync(TestHelper helper)
        {
            Communicator communicator = helper.Communicator!;

            var forwardSamePrx = ITestIntfPrx.Parse(helper.GetTestProxy("ForwardSame", 0), communicator);
            var forwardOtherPrx = ITestIntfPrx.Parse(helper.GetTestProxy("ForwardOther", 0), communicator);

            // The proxies have the same protocol and encoding; the difference is what they forward to.
            TestHelper.Assert(forwardSamePrx.Protocol == forwardOtherPrx.Protocol);
            TestHelper.Assert(forwardSamePrx.Encoding == forwardOtherPrx.Encoding);

            ITestIntfPrx newPrx;

            System.IO.TextWriter output = helper.Output;
            output.Write("testing forwarding with same protocol... ");
            output.Flush();
            newPrx = TestProxy(forwardSamePrx);
            TestHelper.Assert(newPrx.Protocol == forwardSamePrx.Protocol);
            TestHelper.Assert(newPrx.Encoding == forwardSamePrx.Encoding);
            _ = TestProxy(newPrx);
            output.WriteLine("ok");

            output.Write("testing forwarding with other protocol... ");
            output.Flush();
            newPrx = TestProxy(forwardOtherPrx);
            TestHelper.Assert(newPrx.Protocol != forwardOtherPrx.Protocol);
            TestHelper.Assert(newPrx.Encoding == forwardOtherPrx.Encoding); // encoding must remain the same
            _ = TestProxy(newPrx);
            output.WriteLine("ok");

            output.Write("testing forwarding with other protocol and other encoding... ");
            output.Flush();
            Encoding encoding =
                forwardOtherPrx.Encoding == Encoding.V11 ? Encoding.V20 : Encoding.V11;
            newPrx = TestProxy(forwardOtherPrx.Clone(encoding: encoding));
            TestHelper.Assert(newPrx.Protocol != forwardOtherPrx.Protocol);
            TestHelper.Assert(newPrx.Encoding == encoding);
            _ = TestProxy(newPrx);
            output.WriteLine("ok");

            await forwardSamePrx.ShutdownAsync();
        }

        private static ITestIntfPrx TestProxy(ITestIntfPrx prx)
        {
            var ctx = new Dictionary<string, string>(prx.Context);
            ctx.Add("MyCtx", "hello");

            TestHelper.Assert(prx.Op(13, ctx) == 13);
            prx.OpVoid(ctx);

            (int v, string s) = prx.OpReturnOut(34);
            TestHelper.Assert(v == 34 && s == "value=34");

            prx.OpOneway(42);

            try
            {
                prx.OpMyError();
                TestHelper.Assert(false);
            }
            catch (MyError ex)
            {
                TestHelper.Assert(ex.Number == 42);
            }

            try
            {
                prx.OpObjectNotExistException();
                TestHelper.Assert(false);
            }
            catch (ObjectNotExistException)
            {
            }

            return prx.OpNewProxy().Clone(context: new Dictionary<string, string> { { "Direct", "1" } });
        }
    }
}