From 9a6db1d7c914bb04518c4c1db146aeb055f5951b Mon Sep 17 00:00:00 2001 From: Bernard Normier Date: Thu, 17 Oct 2024 10:02:40 -0400 Subject: [PATCH] More bi-dir testing (#2911) --- cpp/test/Ice/ami/AllTests.cpp | 8 ++-- cpp/test/Ice/ami/Test.ice | 2 +- cpp/test/Ice/ami/TestI.cpp | 42 +++++++++++++++++-- cpp/test/Ice/ami/TestI.h | 6 ++- csharp/test/Ice/ami/AllTests.cs | 29 +++++++------ csharp/test/Ice/ami/Test.ice | 2 +- csharp/test/Ice/ami/TestI.cs | 23 +++++----- .../src/main/java/test/Ice/ami/AllTests.java | 28 +++++++------ java/test/src/main/java/test/Ice/ami/Test.ice | 2 +- .../src/main/java/test/Ice/ami/TestI.java | 23 +++++----- python/test/Ice/ami/AllTests.py | 6 ++- python/test/Ice/ami/Test.ice | 2 +- python/test/Ice/ami/TestI.py | 13 ++++-- swift/test/Ice/ami/AllTests.swift | 28 +++++++++++++ swift/test/Ice/ami/TestI.swift | 12 +++++- 15 files changed, 161 insertions(+), 65 deletions(-) diff --git a/cpp/test/Ice/ami/AllTests.cpp b/cpp/test/Ice/ami/AllTests.cpp index 7867c8989d9..33f1108317e 100644 --- a/cpp/test/Ice/ami/AllTests.cpp +++ b/cpp/test/Ice/ami/AllTests.cpp @@ -1279,11 +1279,13 @@ allTests(TestHelper* helper, bool collocated) } cout << "ok" << endl; - cout << "testing bidir... " << flush; + cout << "testing bi-dir... " << flush; auto adapter = communicator->createObjectAdapter(""); auto replyI = make_shared(); - auto reply = uncheckedCast(adapter->addWithUUID(replyI)); - adapter->activate(); + auto reply = adapter->addWithUUID(replyI); + + map context{{"ONE", ""}}; + p->pingBiDir(reply, context); p->ice_getConnection()->setAdapter(adapter); p->pingBiDir(reply); diff --git a/cpp/test/Ice/ami/Test.ice b/cpp/test/Ice/ami/Test.ice index 499448524ed..652ee536132 100644 --- a/cpp/test/Ice/ami/Test.ice +++ b/cpp/test/Ice/ami/Test.ice @@ -44,7 +44,7 @@ interface TestIntf bool supportsAMD(); bool supportsFunctionalTests(); - void pingBiDir(PingReply* reply); + ["amd"] void pingBiDir(PingReply* reply); } interface TestIntfController diff --git a/cpp/test/Ice/ami/TestI.cpp b/cpp/test/Ice/ami/TestI.cpp index 0f9aede14a8..b19827ffe1e 100644 --- a/cpp/test/Ice/ami/TestI.cpp +++ b/cpp/test/Ice/ami/TestI.cpp @@ -173,9 +173,45 @@ TestIntfI::supportsFunctionalTests(const Ice::Current&) } void -TestIntfI::pingBiDir(optional reply, const Ice::Current& current) -{ - reply->ice_fixed(current.con)->replyAsync().get(); +TestIntfI::pingBiDirAsync( + optional reply, + std::function response, + std::function exception, + const Ice::Current& current) +{ + reply = reply->ice_fixed(current.con); + const bool expectSuccess = current.ctx.find("ONE") == current.ctx.end(); + + reply->ice_fixed(current.con) + ->replyAsync( + [expectSuccess, response, exception]() + { + try + { + test(expectSuccess); + response(); + } + catch (...) + { + exception(std::current_exception()); + } + }, + [expectSuccess, response, exception](exception_ptr ex) + { + try + { + test(!expectSuccess); + rethrow_exception(ex); + } + catch (const Ice::ObjectNotExistException&) + { + response(); + } + catch (...) + { + exception(std::current_exception()); + } + }); } void diff --git a/cpp/test/Ice/ami/TestI.h b/cpp/test/Ice/ami/TestI.h index 853f55a3895..315087e956e 100644 --- a/cpp/test/Ice/ami/TestI.h +++ b/cpp/test/Ice/ami/TestI.h @@ -47,7 +47,11 @@ class TestIntfI final : public Test::TestIntf bool supportsAMD(const Ice::Current&) final; bool supportsFunctionalTests(const Ice::Current&) final; - void pingBiDir(std::optional, const Ice::Current&) final; + void pingBiDirAsync( + std::optional, + std::function, + std::function, + const Ice::Current&) final; private: int _batchCount; diff --git a/csharp/test/Ice/ami/AllTests.cs b/csharp/test/Ice/ami/AllTests.cs index a7e2eb602b1..f845076637b 100644 --- a/csharp/test/Ice/ami/AllTests.cs +++ b/csharp/test/Ice/ami/AllTests.cs @@ -865,21 +865,26 @@ public static async Task allTestsAsync(global::Test.TestHelper helper, bool coll s2.SetResult(1); Task.WaitAll(t1, t2, t3, t4); } - - if (!collocated) - { - ObjectAdapter adapter = communicator.createObjectAdapter(""); - var replyI = new PingReplyI(); - var reply = Test.PingReplyPrxHelper.uncheckedCast(adapter.addWithUUID(replyI)); - - p.ice_getConnection().setAdapter(adapter); - p.pingBiDir(reply); - test(replyI.checkReceived()); - adapter.destroy(); - } } output.WriteLine("ok"); + if (!collocated) + { + output.Write("testing bi-dir... "); + ObjectAdapter adapter = communicator.createObjectAdapter(""); + var replyI = new PingReplyI(); + var reply = Test.PingReplyPrxHelper.uncheckedCast(adapter.addWithUUID(replyI)); + + var context = new Dictionary { ["ONE"] = "" }; + await p.pingBiDirAsync(reply, context); + + p.ice_getConnection().setAdapter(adapter); + await p.pingBiDirAsync(reply); + test(replyI.checkReceived()); + adapter.destroy(); + output.WriteLine("ok"); + } + output.Write("testing result struct... "); output.Flush(); { diff --git a/csharp/test/Ice/ami/Test.ice b/csharp/test/Ice/ami/Test.ice index 2bf9d844e55..095f815b45c 100644 --- a/csharp/test/Ice/ami/Test.ice +++ b/csharp/test/Ice/ami/Test.ice @@ -46,7 +46,7 @@ interface TestIntf ["amd"] void opWithUEAsyncDispatch() throws TestIntfException; - void pingBiDir(PingReply* reply); + ["amd"] void pingBiDir(PingReply* reply); } interface TestIntfController diff --git a/csharp/test/Ice/ami/TestI.cs b/csharp/test/Ice/ami/TestI.cs index 475e3f4db5c..2bd7825e29b 100644 --- a/csharp/test/Ice/ami/TestI.cs +++ b/csharp/test/Ice/ami/TestI.cs @@ -145,19 +145,20 @@ public override async Task await self(current).opWithUEAsync(); } - public override void - pingBiDir(Test.PingReplyPrx reply, Ice.Current current) + public override async Task pingBiDirAsync(Test.PingReplyPrx reply, Ice.Current current) { reply = Test.PingReplyPrxHelper.uncheckedCast(reply.ice_fixed(current.con)); - Thread dispatchThread = Thread.CurrentThread; - reply.replyAsync().ContinueWith( - (t) => - { - Thread callbackThread = Thread.CurrentThread; - test(dispatchThread != callbackThread); - test(callbackThread.Name.Contains("Ice.ThreadPool.Server")); - }, - reply.ice_scheduler()).Wait(); + bool expectSuccess = !current.ctx.ContainsKey("ONE"); + + try + { + await reply.replyAsync(); + test(expectSuccess); + } + catch (ObjectNotExistException) + { + test(!expectSuccess); + } } private Test.TestIntfPrx diff --git a/java/test/src/main/java/test/Ice/ami/AllTests.java b/java/test/src/main/java/test/Ice/ami/AllTests.java index eb3fc139d12..c7ad48b1d85 100644 --- a/java/test/src/main/java/test/Ice/ami/AllTests.java +++ b/java/test/src/main/java/test/Ice/ami/AllTests.java @@ -1043,21 +1043,25 @@ public static void allTests(test.TestHelper helper, boolean collocated) { }, p.ice_executor()) .join(); - - if (!collocated) { - com.zeroc.Ice.ObjectAdapter adapter = communicator.createObjectAdapter(""); - PingReplyI replyI = new PingReplyI(); - PingReplyPrx reply = PingReplyPrx.uncheckedCast(adapter.addWithUUID(replyI)); - adapter.activate(); - - p.ice_getConnection().setAdapter(adapter); - p.pingBiDir(reply); - test(replyI.checkReceived()); - adapter.destroy(); - } } out.println("ok"); + if (!collocated) { + out.print("testing bi-dir... "); + com.zeroc.Ice.ObjectAdapter adapter = communicator.createObjectAdapter(""); + PingReplyI replyI = new PingReplyI(); + PingReplyPrx reply = PingReplyPrx.uncheckedCast(adapter.addWithUUID(replyI)); + + var context = java.util.Map.of("ONE", ""); + p.pingBiDir(reply, context); + + p.ice_getConnection().setAdapter(adapter); + p.pingBiDir(reply); + test(replyI.checkReceived()); + adapter.destroy(); + out.println("ok"); + } + out.print("testing result struct... "); out.flush(); { diff --git a/java/test/src/main/java/test/Ice/ami/Test.ice b/java/test/src/main/java/test/Ice/ami/Test.ice index e9b7ef1d431..aac507999fb 100644 --- a/java/test/src/main/java/test/Ice/ami/Test.ice +++ b/java/test/src/main/java/test/Ice/ami/Test.ice @@ -47,7 +47,7 @@ interface TestIntf float opFloat(float f); double opDouble(double d); - void pingBiDir(PingReply* reply); + ["amd"] void pingBiDir(PingReply* reply); } interface TestIntfController diff --git a/java/test/src/main/java/test/Ice/ami/TestI.java b/java/test/src/main/java/test/Ice/ami/TestI.java index e9d81ea6ae7..7865f2be559 100644 --- a/java/test/src/main/java/test/Ice/ami/TestI.java +++ b/java/test/src/main/java/test/Ice/ami/TestI.java @@ -96,18 +96,19 @@ public double opDouble(double d, com.zeroc.Ice.Current current) { } @Override - public void pingBiDir(PingReplyPrx reply, com.zeroc.Ice.Current current) { + public CompletionStage pingBiDirAsync(PingReplyPrx reply, com.zeroc.Ice.Current current) { reply = reply.ice_fixed(current.con); - final Thread dispatchThread = Thread.currentThread(); - reply.replyAsync() - .whenCompleteAsync( - (result, ex) -> { - Thread callbackThread = Thread.currentThread(); - test(callbackThread != dispatchThread); - test(callbackThread.getName().indexOf("Ice.ThreadPool.Server") != -1); - }, - reply.ice_executor()) - .join(); + boolean expectSuccess = !current.ctx.containsKey("ONE"); + return reply.replyAsync() + .handle( + (r, ex) -> { + if (expectSuccess) { + test(ex == null); + } else { + test(ex instanceof com.zeroc.Ice.ObjectNotExistException); + } + return null; // means success + }); } @Override diff --git a/python/test/Ice/ami/AllTests.py b/python/test/Ice/ami/AllTests.py index c523d444f46..2ad8fc802b0 100644 --- a/python/test/Ice/ami/AllTests.py +++ b/python/test/Ice/ami/AllTests.py @@ -629,12 +629,14 @@ def allTestsFuture(helper, communicator, collocated): print("ok") if not collocated: - sys.stdout.write("testing bidir invocation... ") + sys.stdout.write("testing bi-dir... ") sys.stdout.flush() adapter = communicator.createObjectAdapter("") replyI = PingReplyI() reply = Test.PingReplyPrx.uncheckedCast(adapter.addWithUUID(replyI)) - adapter.activate() + + context = { "ONE": "" } + p.pingBiDir(reply, context) p.ice_getConnection().setAdapter(adapter) p.pingBiDir(reply) diff --git a/python/test/Ice/ami/Test.ice b/python/test/Ice/ami/Test.ice index a11e1223268..2adfe72d791 100644 --- a/python/test/Ice/ami/Test.ice +++ b/python/test/Ice/ami/Test.ice @@ -39,7 +39,7 @@ interface TestIntf bool supportsAMD(); bool supportsFunctionalTests(); - void pingBiDir(PingReply* reply); + ["amd"] void pingBiDir(PingReply* reply); } interface TestIntfController diff --git a/python/test/Ice/ami/TestI.py b/python/test/Ice/ami/TestI.py index 2ac31847d72..c226e11b9c4 100644 --- a/python/test/Ice/ami/TestI.py +++ b/python/test/Ice/ami/TestI.py @@ -90,10 +90,15 @@ def supportsAMD(self, current): def supportsFunctionalTests(self, current): return False - def pingBiDir(self, reply, current): - # TODO: verify correct thread with add_done_callback_async - reply.ice_fixed(current.con).replyAsync().result() - + async def pingBiDir(self, reply, current): + expectSuccess = "ONE" not in current.ctx + try: + await reply.ice_fixed(current.con).replyAsync() + if not expectSuccess: + raise Test.TestIntfException() + except Ice.ObjectNotExistException: + if expectSuccess: + raise Test.TestIntfException() class TestIntfII(Test.Outer.Inner.TestIntf): def op(self, i, current): diff --git a/swift/test/Ice/ami/AllTests.swift b/swift/test/Ice/ami/AllTests.swift index c2af5be20f8..03479b39d9a 100644 --- a/swift/test/Ice/ami/AllTests.swift +++ b/swift/test/Ice/ami/AllTests.swift @@ -317,6 +317,22 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) async throws { output.writeLine("ok") + if (!collocated) { + output.write("testing bi-dir... "); + let adapter = try communicator.createObjectAdapter("") + let replyI = PingReplyI() + let reply = try uncheckedCast(prx: adapter.addWithUUID(PingReplyDisp(replyI)), type: PingReplyPrx.self) + + let context: [String: String] = ["ONE": ""] + try await p.pingBiDir(reply, context: context); + + try await p.ice_getConnection()!.setAdapter(adapter) + try await p.pingBiDir(reply); + try test(replyI.checkReceived()) + adapter.destroy() + output.writeLine("ok") + } + output.write("testing connection abort... ") do { // @@ -351,3 +367,15 @@ func allTests(_ helper: TestHelper, collocated: Bool = false) async throws { } try await p.shutdown() } + +class PingReplyI: PingReply { + private var _received = false + + func checkReceived() -> Bool { + return _received + } + + func reply(current: Ice.Current) throws { + _received = true + } +} diff --git a/swift/test/Ice/ami/TestI.swift b/swift/test/Ice/ami/TestI.swift index f13110df0b4..7d34cabb9c3 100644 --- a/swift/test/Ice/ami/TestI.swift +++ b/swift/test/Ice/ami/TestI.swift @@ -102,8 +102,16 @@ class TestI: TestIntf { } func pingBiDir(reply: PingReplyPrx?, current: Current) async throws { - if let reply = reply { - try await reply.ice_fixed(current.con!).reply() + let expectSuccess = !current.ctx.keys.contains("ONE") + do { + try await reply!.ice_fixed(current.con!).reply() + if !expectSuccess { + throw TestIntfException() + } + } catch is ObjectNotExistException { + if expectSuccess { + throw TestIntfException() + } } }