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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
|
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//
using System.Diagnostics;
using System.Threading;
namespace Ice
{
namespace timeout
{
public class AllTests : global::Test.AllTests
{
private class Callback
{
internal Callback()
{
_called = false;
}
public virtual void check()
{
lock(this)
{
while(!_called)
{
Monitor.Wait(this);
}
_called = false;
}
}
public virtual void called()
{
lock(this)
{
Debug.Assert(!_called);
_called = true;
Monitor.Pulse(this);
}
}
private bool _called;
}
private static Ice.Connection connect(Ice.ObjectPrx prx)
{
int nRetry = 10;
while(--nRetry > 0)
{
try
{
prx.ice_getConnection();
break;
}
catch(Ice.ConnectTimeoutException)
{
// Can sporadically occur with slow machines
}
}
return prx.ice_getConnection();
}
public static void allTests(global::Test.TestHelper helper)
{
Test.ControllerPrx controller =
Test.ControllerPrxHelper.checkedCast(
helper.communicator().stringToProxy("controller:" + helper.getTestEndpoint(1)));
test(controller != null);
try
{
allTestsWithController(helper, controller);
}
catch(Exception)
{
// Ensure the adapter is not in the holding state when an unexpected exception occurs to prevent
// the test from hanging on exit in case a connection which disables timeouts is still opened.
controller.resumeAdapter();
throw;
}
}
public static void allTestsWithController(global::Test.TestHelper helper, Test.ControllerPrx controller)
{
var communicator = helper.communicator();
string sref = "timeout:" + helper.getTestEndpoint(0);
var obj = communicator.stringToProxy(sref);
test(obj != null);
Test.TimeoutPrx timeout = Test.TimeoutPrxHelper.checkedCast(obj);
test(timeout != null);
var output = helper.getWriter();
output.Write("testing connect timeout... ");
output.Flush();
{
//
// Expect ConnectTimeoutException.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_timeout(100));
controller.holdAdapter(-1);
try
{
to.op();
test(false);
}
catch(Ice.ConnectTimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
}
{
//
// Expect success.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_timeout(-1));
controller.holdAdapter(100);
try
{
to.op();
}
catch(Ice.ConnectTimeoutException)
{
test(false);
}
}
output.WriteLine("ok");
// The sequence needs to be large enough to fill the write/recv buffers
byte[] seq = new byte[2000000];
output.Write("testing connection timeout... ");
output.Flush();
{
//
// Expect TimeoutException.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_timeout(250));
connect(to);
controller.holdAdapter(-1);
try
{
to.sendData(seq);
test(false);
}
catch(Ice.TimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
}
{
//
// Expect success.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_timeout(2000));
controller.holdAdapter(100);
try
{
to.sendData(new byte[1000000]);
}
catch(Ice.TimeoutException)
{
test(false);
}
}
output.WriteLine("ok");
output.Write("testing invocation timeout... ");
output.Flush();
{
var connection = obj.ice_getConnection();
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_invocationTimeout(100));
test(connection == to.ice_getConnection());
try
{
to.sleep(1000);
test(false);
}
catch(Ice.InvocationTimeoutException)
{
}
obj.ice_ping();
to = Test.TimeoutPrxHelper.checkedCast(obj.ice_invocationTimeout(1000));
test(connection == to.ice_getConnection());
try
{
to.sleep(100);
}
catch(Ice.InvocationTimeoutException)
{
test(false);
}
test(connection == to.ice_getConnection());
}
{
//
// Expect InvocationTimeoutException.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_invocationTimeout(100));
Callback cb = new Callback();
to.begin_sleep(1000).whenCompleted(
() =>
{
test(false);
},
(Ice.Exception ex) =>
{
test(ex is Ice.InvocationTimeoutException);
cb.called();
});
cb.check();
obj.ice_ping();
}
{
//
// Expect success.
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_invocationTimeout(1000));
Callback cb = new Callback();
to.begin_sleep(100).whenCompleted(
() =>
{
cb.called();
},
(Ice.Exception ex) =>
{
test(false);
});
cb.check();
}
{
//
// Backward compatible connection timeouts
//
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_invocationTimeout(-2).ice_timeout(250));
var con = connect(to);
try
{
to.sleep(750);
test(false);
}
catch(Ice.TimeoutException)
{
try
{
con.getInfo();
test(false);
}
catch(Ice.TimeoutException)
{
// Connection got closed as well.
}
}
obj.ice_ping();
try
{
con = connect(to);
to.end_sleep(to.begin_sleep(750));
test(false);
}
catch(Ice.TimeoutException)
{
try
{
con.getInfo();
test(false);
}
catch(Ice.TimeoutException)
{
// Connection got closed as well.
}
}
obj.ice_ping();
}
output.WriteLine("ok");
output.Write("testing close timeout... ");
output.Flush();
{
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(obj.ice_timeout(250));
var connection = connect(to);
controller.holdAdapter(-1);
connection.close(Ice.ConnectionClose.GracefullyWithWait);
try
{
connection.getInfo(); // getInfo() doesn't throw in the closing state.
}
catch(Ice.LocalException)
{
test(false);
}
while(true)
{
try
{
connection.getInfo();
Thread.Sleep(10);
}
catch(Ice.ConnectionManuallyClosedException ex)
{
// Expected.
test(ex.graceful);
break;
}
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
}
output.WriteLine("ok");
output.Write("testing timeout overrides... ");
output.Flush();
{
//
// Test Ice.Override.Timeout. This property overrides all
// endpoint timeouts.
//
var initData = new Ice.InitializationData();
initData.properties = communicator.getProperties().ice_clone_();
initData.properties.setProperty("Ice.Override.ConnectTimeout", "250");
initData.properties.setProperty("Ice.Override.Timeout", "100");
var comm = helper.initialize(initData);
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(comm.stringToProxy(sref));
connect(to);
controller.holdAdapter(-1);
try
{
to.sendData(seq);
test(false);
}
catch(Ice.TimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
//
// Calling ice_timeout() should have no effect.
//
to = Test.TimeoutPrxHelper.uncheckedCast(to.ice_timeout(1000));
connect(to);
controller.holdAdapter(-1);
try
{
to.sendData(seq);
test(false);
}
catch(Ice.TimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
comm.destroy();
}
{
//
// Test Ice.Override.ConnectTimeout.
//
var initData = new Ice.InitializationData();
initData.properties = communicator.getProperties().ice_clone_();
initData.properties.setProperty("Ice.Override.ConnectTimeout", "250");
var comm = helper.initialize(initData);
controller.holdAdapter(-1);
Test.TimeoutPrx to = Test.TimeoutPrxHelper.uncheckedCast(comm.stringToProxy(sref));
try
{
to.op();
test(false);
}
catch(Ice.ConnectTimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
//
// Calling ice_timeout() should have no effect on the connect timeout.
//
controller.holdAdapter(-1);
to = Test.TimeoutPrxHelper.uncheckedCast(to.ice_timeout(1000));
try
{
to.op();
test(false);
}
catch(Ice.ConnectTimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
//
// Verify that timeout set via ice_timeout() is still used for requests.
//
to = Test.TimeoutPrxHelper.uncheckedCast(to.ice_timeout(250));
connect(to);
controller.holdAdapter(-1);
try
{
to.sendData(seq);
test(false);
}
catch(Ice.TimeoutException)
{
// Expected.
}
controller.resumeAdapter();
timeout.op(); // Ensure adapter is active.
comm.destroy();
}
{
//
// Test Ice.Override.CloseTimeout.
//
var initData = new Ice.InitializationData();
initData.properties = communicator.getProperties().ice_clone_();
initData.properties.setProperty("Ice.Override.CloseTimeout", "100");
var comm = helper.initialize(initData);
comm.stringToProxy(sref).ice_getConnection();
controller.holdAdapter(-1);
long begin = System.DateTime.Now.Ticks;
comm.destroy();
test(((long)new System.TimeSpan(System.DateTime.Now.Ticks - begin).TotalMilliseconds - begin) < 1000);
controller.resumeAdapter();
}
output.WriteLine("ok");
output.Write("testing invocation timeouts with collocated calls... ");
output.Flush();
{
communicator.getProperties().setProperty("TimeoutCollocated.AdapterId", "timeoutAdapter");
var adapter = communicator.createObjectAdapter("TimeoutCollocated");
adapter.activate();
Test.TimeoutPrx proxy = Test.TimeoutPrxHelper.uncheckedCast(adapter.addWithUUID(new TimeoutI()));
proxy =(Test.TimeoutPrx)proxy.ice_invocationTimeout(100);
try
{
proxy.sleep(500);
test(false);
}
catch(Ice.InvocationTimeoutException)
{
}
try
{
proxy.end_sleep(proxy.begin_sleep(500));
test(false);
}
catch(Ice.InvocationTimeoutException)
{
}
try
{
((Test.TimeoutPrx)proxy.ice_invocationTimeout(-2)).ice_ping();
((Test.TimeoutPrx)proxy.ice_invocationTimeout(-2)).begin_ice_ping().waitForCompleted();
}
catch(Ice.Exception)
{
test(false);
}
Test.TimeoutPrx batchTimeout =(Test.TimeoutPrx)proxy.ice_batchOneway();
batchTimeout.ice_ping();
batchTimeout.ice_ping();
batchTimeout.ice_ping();
((Test.TimeoutPrx)proxy.ice_invocationTimeout(-1)).begin_sleep(500); // Keep the server thread pool busy.
try
{
batchTimeout.end_ice_flushBatchRequests(batchTimeout.begin_ice_flushBatchRequests());
test(false);
}
catch(Ice.InvocationTimeoutException)
{
}
adapter.destroy();
}
output.WriteLine("ok");
controller.shutdown();
}
}
}
}
|