1%
2% Copyright (c) ZeroC, Inc. All rights reserved.
3%
4
5classdef AllTests
6    methods(Static)
7        function r = connect(prx)
8            nRetry = 10;
9            while nRetry > 0
10                nRetry = nRetry - 1;
11                try
12                    prx.ice_getConnection();
13                    break;
14                catch ex
15                    if isa(ex, 'Ice.ConnectTimeoutException')
16                        % Can sporadically occur with slow machines
17                    else
18                        rethrow(ex);
19                    end
20                end
21            end
22            r = prx.ice_getConnection(); % Establish connection
23        end
24        function allTests(helper)
25            import Test.*;
26
27            communicator = helper.communicator();
28
29            sref = ['timeout:', helper.getTestEndpoint()];
30            obj = communicator.stringToProxy(sref);
31            assert(~isempty(obj));
32
33            timeout = TimeoutPrx.checkedCast(obj);
34            assert(~isempty(timeout));
35
36            controller = ControllerPrx.checkedCast(...
37                communicator.stringToProxy(['controller:', helper.getTestEndpoint(1)]));
38            assert(~isempty(controller));
39
40            fprintf('testing connect timeout... ');
41
42            %
43            % Expect ConnectTimeoutException.
44            %
45            to = timeout.ice_timeout(100);
46            controller.holdAdapter(-1);
47            try
48                to.op();
49                assert(false);
50            catch ex
51                % Expected.
52                assert(isa(ex, 'Ice.ConnectTimeoutException'));
53            end
54            controller.resumeAdapter();
55            timeout.op(); % Ensure adapter is active.
56
57            %
58            % Expect success.
59            %
60            to = timeout.ice_timeout(-1);
61            controller.holdAdapter(100);
62            try
63                to.op();
64            catch ex
65                if isa(ex, 'Ice.ConnectTimeoutException')
66                    assert(false);
67                else
68                    rethrow(ex);
69                end
70            end
71
72            fprintf('ok\n');
73
74            % The sequence needs to be large enough to fill the write/recv buffers
75            bufSize = 2000000;
76            seq = zeros(1, bufSize, 'uint8');
77
78            fprintf('testing connection timeout... ');
79
80            %
81            % Expect TimeoutException.
82            %
83            to = timeout.ice_timeout(250);
84            AllTests.connect(to);
85            controller.holdAdapter(-1);
86            try
87                to.sendData(seq);
88                assert(false);
89            catch ex
90                % Expected.
91                assert(isa(ex, 'Ice.TimeoutException'));
92            end
93            controller.resumeAdapter();
94            timeout.op(); % Ensure adapter is active.
95
96            %
97            % Expect success.
98            %
99            to = timeout.ice_timeout(2000);
100            controller.holdAdapter(500);
101            try
102                to.sendData(zeros(1, 1000000, 'uint8'));
103            catch ex
104                if isa(ex, 'Ice.TimeoutException')
105                    assert(false);
106                else
107                    rethrow(ex);
108                end
109            end
110
111            fprintf('ok\n');
112
113            fprintf('testing invocation timeout... ');
114
115            connection = obj.ice_getConnection();
116            to = timeout.ice_invocationTimeout(100);
117            assert(connection == to.ice_getConnection());
118            try
119                to.sleep(750);
120                assert(false);
121            catch ex
122                assert(isa(ex, 'Ice.InvocationTimeoutException'));
123            end
124            obj.ice_ping();
125            to = TimeoutPrx.checkedCast(obj.ice_invocationTimeout(500));
126            assert(connection == to.ice_getConnection());
127            try
128                to.sleep(100);
129            catch ex
130                if isa(ex, 'Ice.InvocationTimeoutException')
131                    assert(false);
132                else
133                    rethrow(ex);
134                end
135            end
136            assert(connection == to.ice_getConnection());
137
138            %
139            % Expect InvocationTimeoutException.
140            %
141            to = timeout.ice_invocationTimeout(100);
142            f = to.sleepAsync(750);
143            try
144                f.fetchOutputs();
145            catch ex
146                assert(isa(ex, 'Ice.TimeoutException'));
147            end
148            obj.ice_ping();
149
150            %
151            % Expect success.
152            %
153            to = timeout.ice_invocationTimeout(1000);
154            f = to.sleepAsync(100);
155            f.fetchOutputs();
156
157            %
158            % Backward compatible connection timeouts
159            %
160            to = timeout.ice_invocationTimeout(-2).ice_timeout(250);
161            con = AllTests.connect(to);
162            try
163                to.sleep(750);
164                assert(false);
165            catch ex
166                if isa(ex, 'Ice.TimeoutException')
167                    assert(~isempty(con));
168                    try
169                        con.getInfo();
170                        assert(false);
171                    catch ex
172                        if isa(ex, 'Ice.TimeoutException')
173                            % Connection got closed as well.
174                        else
175                            rethrow(ex);
176                        end
177                    end
178                end
179            end
180            obj.ice_ping();
181
182            try
183                con = AllTests.connect(to);
184                to.sleepAsync(750).fetchOutputs();
185                assert(false);
186            catch ex
187                assert(isa(ex, 'Ice.TimeoutException'));
188                assert(~isempty(con));
189                try
190                    con.getInfo();
191                    assert(false);
192                catch ex
193                    if isa(ex, 'Ice.TimeoutException')
194                        % Connection got closed as well.
195                    else
196                        rethrow(ex);
197                    end
198                end
199            end
200            obj.ice_ping();
201
202            fprintf('ok\n');
203
204            fprintf('testing close timeout... ');
205
206            to = TimeoutPrx.uncheckedCast(obj.ice_timeout(250));
207            connection = AllTests.connect(to);
208            controller.holdAdapter(-1);
209            connection.close(Ice.ConnectionClose.GracefullyWithWait);
210            try
211                connection.getInfo(); % getInfo() doesn't throw in the closing state.
212            catch ex
213                assert(false);
214            end
215
216            pause(.650);
217
218            try
219                connection.getInfo();
220                assert(false);
221            catch ex
222                % Expected.
223                assert(isa(ex, 'Ice.ConnectionManuallyClosedException'));
224                assert(ex.graceful);
225            end
226            controller.resumeAdapter();
227            timeout.op(); % Ensure adapter is active.
228
229            fprintf('ok\n');
230
231            fprintf('testing timeout overrides... ');
232
233            %
234            % Test Ice.Override.Timeout. This property overrides all
235            % endpoint timeouts.
236            %
237            properties = helper.communicator().getProperties().clone();
238            properties.setProperty('Ice.Override.ConnectTimeout', '250');
239            properties.setProperty('Ice.Override.Timeout', '100');
240            comm = helper.initialize(properties);
241            to = TimeoutPrx.uncheckedCast(comm.stringToProxy(sref));
242            AllTests.connect(to);
243            controller.holdAdapter(-1);
244            try
245                to.sendData(seq);
246                assert(false);
247            catch ex
248                % Expected.
249                assert(isa(ex, 'Ice.TimeoutException'));
250            end
251            controller.resumeAdapter();
252            timeout.op(); % Ensure adapter is active.
253
254            %
255            % Calling ice_timeout() should have no effect.
256            %
257            to = TimeoutPrx.uncheckedCast(to.ice_timeout(1000));
258            AllTests.connect(to);
259            controller.holdAdapter(-1);
260            try
261                to.sendData(seq);
262                assert(false);
263            catch ex
264                % Expected.
265                assert(isa(ex, 'Ice.TimeoutException'));
266            end
267            controller.resumeAdapter();
268            timeout.op(); % Ensure adapter is active.
269            comm.destroy();
270
271            %
272            % Test Ice.Override.ConnectTimeout.
273            %
274            properties = helper.communicator().getProperties().clone();
275            properties.setProperty('Ice.Override.ConnectTimeout', '250');
276
277            comm = helper.initialize(properties);
278            to = TimeoutPrx.uncheckedCast(comm.stringToProxy(sref));
279            controller.holdAdapter(-1);
280            try
281                to.op();
282                assert(false);
283            catch ex
284                % Expected.
285                assert(isa(ex, 'Ice.ConnectTimeoutException'));
286            end
287            controller.resumeAdapter();
288            timeout.op(); % Ensure adapter is active.
289
290            %
291            % Calling ice_timeout() should have no effect on the connect timeout.
292            %
293            controller.holdAdapter(-1);
294            to = to.ice_timeout(1000);
295            try
296                to.op();
297                assert(false);
298            catch ex
299                % Expected.
300                assert(isa(ex, 'Ice.ConnectTimeoutException'));
301            end
302            controller.resumeAdapter();
303            timeout.op(); % Ensure adapter is active.
304
305            %
306            % Verify that timeout set via ice_timeout() is still used for requests.
307            %
308            to = to.ice_timeout(250);
309            AllTests.connect(to);
310            controller.holdAdapter(-1);
311            try
312                to.sendData(seq);
313                assert(false);
314            catch ex
315                % Expected.
316                assert(isa(ex, 'Ice.TimeoutException'));
317            end
318            controller.resumeAdapter();
319            timeout.op(); % Ensure adapter is active.
320            comm.destroy();
321
322            %
323            % Test Ice.Override.CloseTimeout.
324            %
325            properties = helper.communicator().getProperties().clone();
326            properties.setProperty('Ice.Override.CloseTimeout', '100');
327            comm = helper.initialize(properties);
328            comm.stringToProxy(sref).ice_getConnection();
329            controller.holdAdapter(-1);
330            tic();
331            comm.destroy();
332            assert(toc() < 1.0);
333            controller.resumeAdapter();
334            timeout.op(); % Ensure adapter is active.
335
336            fprintf('ok\n');
337
338            controller.shutdown();
339        end
340    end
341end
342