public void Echo()
        {
            var record = new TestRecord
            {
                hash =
                    new MD5
                {
                    Value =
                        new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8 }
                },
                kind = Kind.FOO,
                name = "My Record"
            };

            // Test synchronous RPC:
            TestRecord testRecord = simpleClient.echo(record);

            Assert.AreEqual(record, testRecord);

            // Test asynchronous RPC (future):
            var future1 = new CallFuture <TestRecord>();

            simpleClient.echo(record, future1);
            Assert.AreEqual(record, future1.WaitForResult(2000));
            Assert.IsNull(future1.Error);

            // Test asynchronous RPC (callback):
            var future2 = new CallFuture <TestRecord>();

            simpleClient.echo(record, new NestedCallFuture <TestRecord>(future2));

            Assert.AreEqual(record, future2.WaitForResult(2000));
            Assert.IsNull(future2.Error);
        }
        public void Error(bool systemError)
        {
            Type expected;

            if (systemError)
            {
                expected = typeof(Exception);
                SimpleImpl.throwSystemError = true;
            }
            else
            {
                expected = typeof(TestError);
                SimpleImpl.throwSystemError = false;
            }

            // Test synchronous RPC:
            try
            {
                simpleClient.error();
                Assert.Fail("Expected " + expected.Name + " to be thrown");
            }
            catch (Exception e)
            {
                Assert.AreEqual(expected, e.GetType());
            }

            // Test asynchronous RPC (future):
            var future = new CallFuture <object>();

            simpleClient.error(future);
            try
            {
                future.WaitForResult(2000);
                Assert.Fail("Expected " + expected.Name + " to be thrown");
            }
            catch (Exception e)
            {
                Assert.AreEqual(expected, e.GetType());
            }

            Assert.IsNotNull(future.Error);
            Assert.AreEqual(expected, future.Error.GetType());
            Assert.IsNull(future.Result);

            // Test asynchronous RPC (callback):
            Exception errorRef = null;
            var       latch    = new CountdownLatch(1);

            simpleClient.error(new CallbackCallFuture <object>(
                                   result => Assert.Fail("Expected " + expected.Name),
                                   exception =>
            {
                errorRef = exception;
                latch.Signal();
            }));

            Assert.IsTrue(latch.Wait(2000), "Timed out waiting for error");
            Assert.IsNotNull(errorRef);
            Assert.AreEqual(expected, errorRef.GetType());
        }
        private string Hello(string howAreYou)
        {
            var response = new CallFuture <string>();

            simpleClient.hello(howAreYou, response);

            return(response.WaitForResult(2000));
        }
        // AVRO-625 [Test]
        public void CancelPendingRequestsOnTransceiverClose()
        {
            // Start up a second server so that closing the server doesn't
            // interfere with the other unit tests:
            var blockingSimpleImpl = new BlockingSimpleImpl();

            var responder = new SpecificResponder <Simple>(blockingSimpleImpl);
            var server2   = new SocketServer("localhost", 0, responder);

            server2.Start();

            try
            {
                int serverPort = server2.Port;

                var transceiver2 = new SocketTransceiver("localhost", serverPort);

                var addFuture = new CallFuture <int>();
                try
                {
                    var simpleClient2 = SpecificRequestor.CreateClient <SimpleCallback>(transceiver2);

                    // The first call has to block for the handshake:
                    Assert.AreEqual(3, simpleClient2.add(1, 2));

                    // Now acquire the semaphore so that the server will block:
                    blockingSimpleImpl.acquireRunPermit();
                    simpleClient2.add(1, 2, addFuture);
                }
                finally
                {
                    // When the transceiver is closed, the CallFuture should get
                    // an IOException
                    transceiver2.Close();
                }
                bool ioeThrown = false;
                try
                {
                    addFuture.WaitForResult(2000);
                }
                catch (Exception)
                {
                }
                //catch (ExecutionException e) {
                //  ioeThrown = e.getCause() instanceof IOException;
                //  Assert.assertTrue(e.getCause() instanceof IOException);
                //} catch (Exception e) {
                //  e.printStackTrace();
                //  Assert.fail("Unexpected Exception: " + e.toString());
                //}
                Assert.IsTrue(ioeThrown, "Expected IOException to be thrown");
            }
            finally
            {
                blockingSimpleImpl.releaseRunPermit();
                server2.Stop();
            }
        }
        public void Error()
        {
            // Test synchronous RPC:
            try
            {
                simpleClient.error();
                Assert.Fail("Expected " + typeof(TestError).Name + " to be thrown");
            }
            catch (TestError)
            {
                // Expected
            }
            catch (Exception e)
            {
                Assert.Fail("Unexpected error: " + e);
            }

            // Test asynchronous RPC (future):
            var future = new CallFuture <object>();

            simpleClient.error(future);
            try
            {
                future.WaitForResult(2000);
                Assert.Fail("Expected " + typeof(TestError).Name + " to be thrown");
            }
            catch (TestError)
            {
            }

            Assert.IsNotNull(future.Error);
            Assert.AreEqual(typeof(TestError), future.Error.GetType());
            Assert.IsNull(future.Result);

            // Test asynchronous RPC (callback):
            Exception errorRef = null;
            var       latch    = new CountdownLatch(1);

            simpleClient.error(new CallbackCallFuture <object>(
                                   result => Assert.Fail("Expected " + typeof(TestError).Name),
                                   exception =>
            {
                errorRef = exception;
                latch.Signal();
            }));

            Assert.IsTrue(latch.Wait(2000), "Timed out waiting for error");
            Assert.IsNotNull(errorRef);
            Assert.AreEqual(typeof(TestError), errorRef.GetType());
        }
        public void Add()
        {
            // Test synchronous RPC:
            Assert.AreEqual(8, simpleClient.add(2, 6));

            // Test asynchronous RPC (future):
            var future1 = new CallFuture <int>();

            simpleClient.add(8, 8, future1);
            Assert.AreEqual(16, future1.WaitForResult(2000));
            Assert.IsNull(future1.Error);

            // Test asynchronous RPC (callback):
            var future2 = new CallFuture <int>();

            simpleClient.add(512, 256, new NestedCallFuture <int>(future2));

            Assert.AreEqual(768, future2.WaitForResult(2000));
            Assert.IsNull(future2.Error);
        }
        public void EchoBytes()
        {
            var byteBuffer = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };

            // Test synchronous RPC:
            Assert.AreEqual(byteBuffer, simpleClient.echoBytes(byteBuffer));

            // Test asynchronous RPC (future):
            var future1 = new CallFuture <byte[]>();

            simpleClient.echoBytes(byteBuffer, future1);
            Assert.AreEqual(byteBuffer, future1.WaitForResult(2000));
            Assert.IsNull(future1.Error);

            // Test asynchronous RPC (callback):
            var future2 = new CallFuture <byte[]>();

            simpleClient.echoBytes(byteBuffer, new NestedCallFuture <byte[]>(future2));

            Assert.AreEqual(byteBuffer, future2.WaitForResult(2000));
            Assert.IsNull(future2.Error);
        }
        public void Greeting()
        {
            // Test synchronous RPC:
            string response = Hello("how are you?");

            Assert.AreEqual("Hello, how are you?", response);

            // Test asynchronous RPC (future):
            var future1 = new CallFuture <String>();

            Hello("World!", future1);

            string result = future1.WaitForResult();

            Assert.AreEqual("Hello, World!", result);
            Assert.IsNull(future1.Error);

            // Test asynchronous RPC (callback):
            var future2 = new CallFuture <String>();

            Hello("what's up?", new NestedCallFuture <string>(future2));
            Assert.AreEqual("Hello, what's up?", future2.WaitForResult());
            Assert.IsNull(future2.Error);
        }
        public void TestSendAfterChannelClose()
        {
            // Start up a second server so that closing the server doesn't
            // interfere with the other unit tests:

            var responder = new SpecificResponder <Simple>(new SimpleImpl());
            var server2   = new SocketServer("localhost", 0, responder);

            server2.Start();

            try
            {
                var transceiver2 = new SocketTransceiver("localhost", server2.Port);

                try
                {
                    var simpleClient2 = SpecificRequestor.CreateClient <SimpleCallback>(transceiver2);

                    // Verify that connection works:
                    Assert.AreEqual(3, simpleClient2.add(1, 2));

                    // Try again with callbacks:
                    var addFuture = new CallFuture <int>();
                    simpleClient2.add(1, 2, addFuture);
                    Assert.AreEqual(3, addFuture.WaitForResult(2000));

                    // Shut down server:
                    server2.Stop();

                    // Send a new RPC, and verify that it throws an Exception that
                    // can be detected by the client:
                    bool ioeCaught = false;
                    try
                    {
                        simpleClient2.add(1, 2);
                        Assert.Fail("Send after server close should have thrown Exception");
                    }
                    catch (SocketException)
                    {
                        ioeCaught = true;
                    }

                    Assert.IsTrue(ioeCaught, "Expected IOException");

                    // Send a new RPC with callback, and verify that the correct Exception
                    // is thrown:
                    ioeCaught = false;
                    try
                    {
                        addFuture = new CallFuture <int>();
                        simpleClient2.add(1, 2, addFuture);
                        addFuture.WaitForResult(2000);

                        Assert.Fail("Send after server close should have thrown Exception");
                    }
                    catch (SocketException)
                    {
                        ioeCaught = true;
                    }

                    Assert.IsTrue(ioeCaught, "Expected IOException");
                }
                finally
                {
                    transceiver2.Disconnect();
                }
            }
            finally
            {
                server2.Stop();
                Thread.Sleep(1000);
            }
        }
 private void Hello(string howAreYou, CallFuture <string> future1)
 {
     simpleClient.hello(howAreYou, future1);
 }
 public NestedCallFuture(CallFuture <T> cf)
 {
     this.cf = cf;
 }