예제 #1
0
        public void Consistently_updated_by_multiple_threads()
        {
            const int itemCount = 10;
            var       items     = new int[itemCount];

            for (var i = 0; i < itemCount; i++)
            {
                items[i] = i;
            }

            var itemsRef = Ref.Of(items.ToArray());

            const int threadCount = 10;
            var       latch       = new CountdownLatch(threadCount);

            for (var i = threadCount - 1; i >= 0; i--)
            {
                var delay  = i * 10;
                var thread = new Thread(() =>
                {
                    Thread.Sleep(delay);
                    itemsRef.Swap(xs => xs.Reverse().ToArray());
                    latch.Signal();
                })
                {
                    IsBackground = true
                };
                thread.Start();
            }

            latch.Wait();

            CollectionAssert.AreEqual(items, itemsRef.Value);
        }
        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());
        }
        //[Test]
        public void PerformanceTest()
        {
            const int  threadCount   = 8;
            const long runTimeMillis = 10 * 1000L;


            long rpcCount = 0;

            int[] runFlag = { 1 };

            var startLatch = new CountdownLatch(threadCount);

            for (int ii = 0; ii < threadCount; ii++)
            {
                new Thread(() =>
                {
                    {
                        try
                        {
                            startLatch.Signal();
                            startLatch.Wait(2000);

                            while (Interlocked.Add(ref runFlag[0], 0) == 1)
                            {
                                Interlocked.Increment(ref rpcCount);
                                Assert.AreEqual("Hello, World!", simpleClient.hello("World!"));
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine(e);
                        }
                    }
                }).Start();
            }

            startLatch.Wait(2000);
            Thread.Sleep(2000);
            Interlocked.Exchange(ref runFlag[0], 1);

            string results = "Completed " + rpcCount + " RPCs in " + runTimeMillis +
                             "ms => " + ((rpcCount / (double)runTimeMillis) * 1000) + " RPCs/sec, " +
                             (runTimeMillis / (double)rpcCount) + " ms/RPC.";

            Debug.WriteLine(results);
        }
        public void Ack()
        {
            simpleClient.ack();

            ackLatch.Wait(2000);
            Assert.IsTrue(ackFlag, "Expected ack flag to be set");

            ackLatch = new CountdownLatch(1);
            simpleClient.ack();
            ackLatch.Wait(2000);
            Assert.IsFalse(ackFlag, "Expected ack flag to be cleared");
        }
        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());
        }
예제 #6
0
        private void StartNetworkService()
        {
            CountdownLatch latch = new CountdownLatch(1);

            this.Network = new ChatServer(this, protocolProvider, latch);
            _            = this.Network.Bind(new IPEndPoint(ServerSettings.SERVER_HOST, ServerSettings.SERVER_PORT));

            RestAPI = new RestServer();
            RestAPI.Start(ServerSettings.WEBSERVER_HOST, ServerSettings.WEBSERVER_PORT, latch);

            latch.Wait();

            new ConsoleManager();
        }
        // AVRO-625 [Test]
        // Currently, SocketTransceiver does not permit out-of-order requests on a stateful connection.
        public void Test()
        {
            var waitLatch       = new CountdownLatch(1);
            var simpleResponder = new SimpleResponder(waitLatch);

            server = new SocketServer("localhost", 0, simpleResponder);

            server.Start();

            int port = server.Port;

            transceiver = new SocketTransceiver("localhost", port);
            proxy       = new GenericRequestor(transceiver, SimpleResponder.Protocol);

            // Step 1:
            proxy.GetRemote(); // force handshake

            new Thread(x =>
            {
                // Step 2a:
                waitLatch.Wait();

                var ack = new GenericRecord(SimpleResponder.Protocol.Messages["ack"].Request);
                // Step 2b:
                proxy.Request("ack", ack);
            }).Start();


            /*
             * 3. Execute the Client.hello("wait") RPC, which will block until the
             *    Client.ack() call has completed in the background thread.
             */

            var request = new GenericRecord(SimpleResponder.Protocol.Messages["hello"].Request);

            request.Add("greeting", "wait");

            var response = (string)proxy.Request("hello", request);

            // 4. If control reaches here, both RPCs have executed concurrently
            Assert.AreEqual("wait", response);
        }
            public override object Respond(Message message, object request)
            {
                if (message.Name == "hello")
                {
                    string greeting = ((GenericRecord)request)["greeting"].ToString();
                    if (greeting == "wait")
                    {
                        // Step 3a:
                        waitLatch.Signal();

                        // Step 3b:
                        ackLatch.Wait();
                    }
                    return(greeting);
                }
                if (message.Name == "ack")
                {
                    ackLatch.Signal();
                }

                throw new NotSupportedException();
            }
예제 #9
0
파일: AcceptanceTests.cs 프로젝트: yaq/yaq
        public void TaskManagerTest()
        {
            using (var host = new ServiceHost(typeof(MessageQueue)))
            {
                var binding = new NetTcpBinding();
                binding.TransactionFlow = true;
                binding.TransactionProtocol = TransactionProtocol.WSAtomicTransactionOctober2004;
                binding.TransferMode = TransferMode.Streamed;

                host.AddServiceEndpoint(
                    typeof(IMessageQueue),
                    binding,
                    c_queueUrl
                );

                host.Open();

                var cf = new ChannelFactory<IAsyncMessageQueue>(binding, c_queueUrl);

                var queuesrv = cf.CreateChannel();
                string qn = Guid.NewGuid().ToString("N");

                const int mcnt = 3;

                for (int i = 0; i < mcnt; i++)
                {
                    queuesrv.PutMessage(qn, new Message(), TimeSpan.MaxValue);
                }

                var latch = new CountdownLatch(mcnt);

                TaskManager tm = new TaskManager(() => cf.CreateChannel());
                tm.Tasks.Add(
                    new TaskInfo
                    {
                        Queue = qn,
                        Task = new TestTask(() => latch.Signal()),
                        VisibilitySpan = TimeSpan.FromMinutes(1),
                        MaxInstances = mcnt,
                        PollSpan =
                        TimeSpan.FromMilliseconds(500)
                    });

                tm.Start();

                Assert.IsTrue(latch.Wait(6000));
            }
        }
 public void WaitForConnectionId()
 {
     connectWatcher.Wait();
 }
예제 #11
0
 public void AwaitMessages()
 {
     allMessages.Wait(2000);
 }