summaryrefslogtreecommitdiff
path: root/csharp/test/Ice/interceptor/InterceptorI.cs
blob: c3f88efd13a16732fa7d6b6c4f9655fc8c2c6ba9 (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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

using System;
using System.Threading.Tasks;

namespace Ice
{
    namespace interceptor
    {
        class InterceptorI : Ice.DispatchInterceptor
        {
            internal InterceptorI(Ice.Object servant)
            {
                servant_ = servant;
            }

            protected static void
            test(bool b)
            {
                if(!b)
                {
                    throw new System.Exception();
                }
            }

            public override Task<Ice.OutputStream>
            dispatch(Ice.Request request)
            {
                Ice.Current current = request.getCurrent();

                string context;
                if(current.ctx.TryGetValue("raiseBeforeDispatch", out context))
                {
                    if(context.Equals("user"))
                    {
                        throw new Test.InvalidInputException();
                    }
                    else if(context.Equals("notExist"))
                    {
                        throw new Ice.ObjectNotExistException();
                    }
                    else if(context.Equals("system"))
                    {
                        throw new MySystemException();
                    }
                }

                lastOperation_ = current.operation;

                if(lastOperation_.Equals("addWithRetry") || lastOperation_.Equals("amdAddWithRetry"))
                {
                    for(int i = 0; i < 10; ++i)
                    {
                        try
                        {
                            var t = servant_.ice_dispatch(request);
                            if(t != null && t.IsFaulted)
                            {
                                throw t.Exception.InnerException;
                            }
                            else
                            {
                                test(false);
                            }
                        }
                        catch(Test.RetryException)
                        {
                            //
                            // Expected, retry
                            //
                        }
                    }

                    current.ctx["retry"] = "no";
                }
                else if(current.ctx.TryGetValue("retry", out context) && context.Equals("yes"))
                {
                    //
                    // Retry the dispatch to ensure that abandoning the result of the dispatch
                    // works fine and is thread-safe
                    //
                    servant_.ice_dispatch(request);
                    servant_.ice_dispatch(request);
                }

                var task = servant_.ice_dispatch(request);
                lastStatus_ = task != null;

                if(current.ctx.TryGetValue("raiseAfterDispatch", out context))
                {
                    if(context.Equals("user"))
                    {
                        throw new Test.InvalidInputException();
                    }
                    else if(context.Equals("notExist"))
                    {
                        throw new Ice.ObjectNotExistException();
                    }
                    else if(context.Equals("system"))
                    {
                        throw new MySystemException();
                    }
                }

                return task;
            }

            internal bool
            getLastStatus()
            {
                return lastStatus_;
            }

            internal String
            getLastOperation()
            {
                return lastOperation_;
            }

            internal virtual void
            clear()
            {
                lastOperation_ = null;
                lastStatus_ = false;
            }

            protected readonly Ice.Object servant_;
            protected string lastOperation_;
            protected bool lastStatus_ = false;
        }
    }
}