summaryrefslogtreecommitdiff
path: root/csharp/test/Ice/protocolBridging/TestI.cs
blob: f9462588c3e79c699bffd695d5c9acc5efa5cb91 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
// Copyright (c) ZeroC, Inc. All rights reserved.

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

namespace ZeroC.Ice.Test.ProtocolBridging
{
    public sealed class Forwarder : IObject
    {
        private IObjectPrx _target;

        ValueTask<OutgoingResponseFrame> IObject.DispatchAsync(
            IncomingRequestFrame request,
            Current current,
            CancellationToken cancel)
        {
            if (current.Operation == "op" || current.Operation == "opVoid")
            {
                TestHelper.Assert(request.Context.Count == 1);
                TestHelper.Assert(request.Context["MyCtx"] == "hello");

                if (current.Operation == "opVoid")
                {
                    request.Context.Clear();
                }
            }
            else
            {
                TestHelper.Assert(request.Context.Count == 0);
            }

            if (current.Operation != "opVoid" && current.Operation != "shutdown")
            {
                request.Context["Intercepted"] = "1";
            }

            return _target.ForwardAsync(request, current.IsOneway, cancel: cancel);
        }

        internal Forwarder(IObjectPrx target) => _target = target;
    }

    public sealed class TestI : ITestIntf
    {
        public int Op(int x, Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context["MyCtx"] == "hello");
            TestHelper.Assert(current.Context.Count == 2);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            return x;
        }

        public void OpVoid(Current current, CancellationToken cancel)
        {
            if (current.Context.Count == 2)
            {
                TestHelper.Assert(current.Context["MyCtx"] == "hello");
                TestHelper.Assert(current.Context.ContainsKey("Direct"));
            }
            else
            {
                TestHelper.Assert(current.Context.Count == 0);
            }
        }

        public (int, string) OpReturnOut(int x, Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context.Count == 1);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            return (x, $"value={x}");
        }

        public void OpOneway(int x, Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context.Count == 1);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            TestHelper.Assert(x == 42);
        }

        public void OpMyError(Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context.Count == 1);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            throw new MyError(42);
        }

        public void OpObjectNotExistException(Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context.Count == 1);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            throw new ObjectNotExistException();
        }
        public ITestIntfPrx OpNewProxy(Current current, CancellationToken cancel)
        {
            TestHelper.Assert(current.Context.Count == 1);
            TestHelper.Assert(current.Context.ContainsKey("Intercepted") || current.Context.ContainsKey("Direct"));
            return current.Adapter.CreateProxy(current.Identity, ITestIntfPrx.Factory).Clone(
                encoding: current.Encoding);
        }

        public void Shutdown(Current current, CancellationToken cancel) => current.Communicator.ShutdownAsync();
    }
}