public Task <TReturnValue> InvokeAsync(IObjectPrx prx, TParamList paramList, IReadOnlyDictionary <string, string>?context, IProgress <bool>?progress, CancellationToken cancel) => InvokeAsync(prx, OutgoingRequestFrame.WithParamList( prx, _operationName, _idempotent, _format, context, paramList, _writer), progress, cancel);
public TReturnValue Invoke(IObjectPrx prx, TParamList paramList, IReadOnlyDictionary <string, string>?context) => Invoke(prx, OutgoingRequestFrame.WithParamList(prx, _operationName, _idempotent, _compress, _format, context, paramList, _writer));
public static IMyClassPrx Run(TestHelper helper) { Communicator?communicator = helper.Communicator(); TestHelper.Assert(communicator != null); bool ice1 = communicator.DefaultProtocol == Protocol.Ice1; var cl = IMyClassPrx.Parse(helper.GetTestProxy("test", 0), communicator); IMyClassPrx oneway = cl.Clone(oneway: true); System.IO.TextWriter output = helper.GetWriter(); output.Write("testing Invoke... "); output.Flush(); { var request = OutgoingRequestFrame.WithEmptyParamList(oneway, "opOneway", idempotent: false); // Whether the proxy is oneway or not does not matter for Invoke's oneway parameter. IncomingResponseFrame response = cl.Invoke(request, oneway: true); if (ice1) { TestHelper.Assert(response.ResultType == ResultType.Success); } response = cl.Invoke(request, oneway: false); if (ice1) { TestHelper.Assert(response.ResultType == ResultType.Failure); } response = oneway.Invoke(request, oneway: true); if (ice1) { TestHelper.Assert(response.ResultType == ResultType.Success); } response = oneway.Invoke(request, oneway: false); if (ice1) { TestHelper.Assert(response.ResultType == ResultType.Failure); } request = OutgoingRequestFrame.WithParamList(cl, "opString", idempotent: false, compress: false, format: default,
public static IMyClassPrx allTests(TestHelper helper) { Communicator?communicator = helper.Communicator(); TestHelper.Assert(communicator != null); var cl = IMyClassPrx.Parse($"test:{helper.GetTestEndpoint(0)}", communicator); IMyClassPrx oneway = cl.Clone(oneway: true); System.IO.TextWriter output = helper.GetWriter(); output.Write("testing Invoke... "); output.Flush(); { var request = OutgoingRequestFrame.WithEmptyParamList(oneway, "opOneway", idempotent: false); // Whether the proxy is oneway or not does not matter for Invoke's oneway parameter. IncomingResponseFrame response = cl.Invoke(request, oneway: true); TestHelper.Assert(response.ReplyStatus == ReplyStatus.OK); response = cl.Invoke(request, oneway: false); TestHelper.Assert(response.ReplyStatus == ReplyStatus.UserException); response = oneway.Invoke(request, oneway: true); TestHelper.Assert(response.ReplyStatus == ReplyStatus.OK); response = oneway.Invoke(request, oneway: false); TestHelper.Assert(response.ReplyStatus == ReplyStatus.UserException); request = OutgoingRequestFrame.WithParamList(cl, "opString", idempotent: false, format: null, context: null, _testString, OutputStream.IceWriterFromString); response = cl.Invoke(request); (string s1, string s2) = response.ReadReturnValue(communicator, istr => { string s1 = istr.ReadString(); string s2 = istr.ReadString(); return(s1, s2); }); TestHelper.Assert(s1.Equals(_testString) && s2.Equals(_testString)); } for (int i = 0; i < 2; ++i) { Dictionary <string, string>?ctx = null; if (i == 1) { ctx = new Dictionary <string, string> { ["raise"] = "" }; } var request = OutgoingRequestFrame.WithEmptyParamList(cl, "opException", idempotent: false, context: ctx); IncomingResponseFrame response = cl.Invoke(request); try { response.ReadVoidReturnValue(communicator); } catch (MyException) { // expected } catch (System.Exception) { TestHelper.Assert(false); } } output.WriteLine("ok"); output.Write("testing InvokeAsync... "); output.Flush(); { var request = OutgoingRequestFrame.WithEmptyParamList(oneway, "opOneway", idempotent: false); IncomingResponseFrame response; try { response = oneway.InvokeAsync(request, oneway: true).AsTask().Result; } catch (System.Exception) { TestHelper.Assert(false); } request = OutgoingRequestFrame.WithParamList(cl, "opString", idempotent: false, format: null, context: null, _testString, OutputStream.IceWriterFromString); response = cl.InvokeAsync(request).AsTask().Result; (string s1, string s2) = response.ReadReturnValue(communicator, istr => { string s1 = istr.ReadString(); string s2 = istr.ReadString(); return(s1, s2); }); TestHelper.Assert(s1.Equals(_testString)); TestHelper.Assert(s2.Equals(_testString)); } { var request = OutgoingRequestFrame.WithEmptyParamList(cl, "opException", idempotent: false); IncomingResponseFrame response = cl.InvokeAsync(request).AsTask().Result; try { response.ReadVoidReturnValue(communicator); TestHelper.Assert(false); } catch (MyException) { } catch (System.Exception) { TestHelper.Assert(false); } } output.WriteLine("ok"); return(cl); }
public static IMyObjectPrx Run(TestHelper helper) { bool ice2 = helper.Protocol == Protocol.Ice2; var prx = IMyObjectPrx.Parse(helper.GetTestProxy("test"), helper.Communicator !); System.IO.TextWriter output = helper.Output; output.Write("testing retry... "); output.Flush(); TestHelper.Assert(prx.AddWithRetry(33, 12) == 45); output.WriteLine("ok"); output.Write("testing remote exception... "); output.Flush(); try { prx.BadAdd(33, 12); TestHelper.Assert(false); } catch (InvalidInputException) { // expected } output.WriteLine("ok"); output.Write("testing ONE... "); output.Flush(); try { prx.NotExistAdd(33, 12); TestHelper.Assert(false); } catch (ObjectNotExistException) { // expected } output.WriteLine("ok"); output.Write("testing exceptions raised by the interceptor... "); output.Flush(); var exceptions = new List <(string operation, string kind)> { ("raiseBeforeDispatch", "invalidInput"), ("raiseBeforeDispatch", "notExist"), ("raiseAfterDispatch", "invalidInput"), ("raiseAfterDispatch", "notExist") }; foreach ((string operation, string kind) in exceptions) { var ctx = new Dictionary <string, string> { { operation, kind } }; try { prx.IcePing(ctx); TestHelper.Assert(false); } catch (InvalidInputException) when(kind == "invalidInput") { } catch (ObjectNotExistException) when(kind == "notExist") { } } output.WriteLine("ok"); output.Write("testing invocation interceptors... "); output.Flush(); { var tasks = new List <Task>(); var invocationContext = new AsyncLocal <int>(); using var communicator = new Communicator( prx.Communicator.GetProperties(), invocationInterceptors: new InvocationInterceptor[] { (target, request, next) => { if (ice2) { request.ContextOverride["interceptor-1"] = "interceptor-1"; request.AddBinaryContextEntry(110, 110, (ostr, v) => ostr.WriteInt(v)); } return(next(target, request)); }, async(target, request, next) => { if (ice2) { TestHelper.Assert(request.Context["interceptor-1"] == "interceptor-1"); request.ContextOverride["interceptor-2"] = "interceptor-2"; request.AddBinaryContextEntry(120, 120, (ostr, v) => ostr.WriteInt(v)); } IncomingResponseFrame response = await next(target, request); TestHelper.Assert(invocationContext.Value == int.Parse(request.Context["local-user"])); if (ice2) { TestHelper.Assert(response.BinaryContext.ContainsKey(110)); TestHelper.Assert(response.BinaryContext[110].Read(istr => istr.ReadInt()) == 110); TestHelper.Assert(response.BinaryContext.ContainsKey(120)); TestHelper.Assert(response.BinaryContext[120].Read(istr => istr.ReadInt()) == 120); } return(response); } }); for (int i = 0; i < 10; ++i) { invocationContext.Value = i; var prx1 = IMyObjectPrx.Parse(prx.ToString() !, communicator); Task t = prx1.Op1Async(new Dictionary <string, string> { { "local-user", $"{i}" } }); tasks.Add(t); } Task.WaitAll(tasks.ToArray()); } { IncomingResponseFrame?response = null; int invocations = 0; // An interceptor can stop the chain and directly return a response without calling next, // the first invocation calls next and subsequent invocations reuse the first response. using var communicator = new Communicator( prx.Communicator.GetProperties(), invocationInterceptors: new InvocationInterceptor[] { (target, request, next) => { if (ice2) { request.ContextOverride["interceptor-1"] = "interceptor-1"; } return(next(target, request)); }, async(target, request, next) => { if (response == null) { response = await next(target, request); } return(response); }, (target, request, next) => { invocations++; TestHelper.Assert(response == null); return(next(target, request)); } }); var prx1 = IMyObjectPrx.Parse(prx.ToString() !, communicator); prx1.Op1(new Dictionary <string, string> { { "local-user", "10" } }); prx1.Op1(new Dictionary <string, string> { { "local-user", "11" } }); prx1.Op1(new Dictionary <string, string> { { "local-user", "12" } }); TestHelper.Assert(invocations == 1); } { // throwing from an interceptor stops the interceptor chain using var communicator = new Communicator( prx.Communicator.GetProperties(), invocationInterceptors: new InvocationInterceptor[] { (target, request, next) => { request.ContextOverride["interceptor-1"] = "interceptor-1"; return(next(target, request)); }, (target, request, next) => { TestHelper.Assert(request.Context["interceptor-1"] == "interceptor-1"); throw new InvalidOperationException("stop interceptor chain"); }, (target, request, next) => { TestHelper.Assert(false); return(next(target, request)); } }); var prx1 = IMyObjectPrx.Parse(prx.ToString() !, communicator); try { prx1.Op1(); TestHelper.Assert(false); } catch (InvalidOperationException) { } } output.WriteLine("ok"); output.Write("testing binary context... "); output.Flush(); if (ice2) { for (int size = 128; size < 4096; size *= 2) { var token = new Token(1, "mytoken", Enumerable.Range(0, size).Select(i => (byte)2).ToArray()); var request = OutgoingRequestFrame.WithParamList(prx, "opWithBinaryContext", idempotent: false, compress: false, format: default,
private void Run(IMyObjectPrx prx, Interceptor interceptor) { System.IO.TextWriter output = GetWriter(); output.Write("testing simple interceptor... "); output.Flush(); Assert(interceptor.GetLastOperation() == null); Assert(!interceptor.AsyncCompletion); prx.IcePing(); Assert(interceptor.GetLastOperation() !.Equals("ice_ping")); Assert(!interceptor.AsyncCompletion); string typeId = prx.IceId(); Assert(interceptor.GetLastOperation() !.Equals("ice_id")); Assert(!interceptor.AsyncCompletion); Assert(prx.IceIsA(typeId)); Assert(interceptor.GetLastOperation() !.Equals("ice_isA")); Assert(!interceptor.AsyncCompletion); Assert(prx.Add(33, 12) == 45); Assert(interceptor.GetLastOperation() !.Equals("add")); Assert(!interceptor.AsyncCompletion); output.WriteLine("ok"); output.Write("testing retry... "); output.Flush(); Assert(prx.AddWithRetry(33, 12) == 45); Assert(interceptor.GetLastOperation() !.Equals("addWithRetry")); Assert(!interceptor.AsyncCompletion); output.WriteLine("ok"); output.Write("testing remote exception... "); output.Flush(); try { prx.BadAdd(33, 12); Assert(false); } catch (InvalidInputException) { // expected } Assert(interceptor.GetLastOperation() !.Equals("badAdd")); Assert(!interceptor.AsyncCompletion); output.WriteLine("ok"); output.Write("testing ONE... "); output.Flush(); interceptor.Clear(); try { prx.NotExistAdd(33, 12); Assert(false); } catch (ObjectNotExistException) { // expected } Assert(interceptor.GetLastOperation() !.Equals("notExistAdd")); Assert(!interceptor.AsyncCompletion); output.WriteLine("ok"); output.Write("testing exceptions raised by the interceptor... "); output.Flush(); TestInterceptorExceptions(prx); output.WriteLine("ok"); output.Write("testing binary context... "); output.Flush(); bool ice2 = Communicator() !.DefaultProtocol != Protocol.Ice1; if (ice2) { for (int size = 128; size < 4096; size *= 2) { var token = new Token(1, "mytoken", Enumerable.Range(0, size).Select(i => (byte)2).ToArray()); var request = OutgoingRequestFrame.WithParamList(prx, "opWithBinaryContext", idempotent: false, compress: false, format: default,