コード例 #1
0
ファイル: QueueServerRollbackTest.cs プロジェクト: aooshi/adf
        public void Test()
        {
            var host = "127.0.0.1";
            //var host = "192.168.199.13";
            var port  = 231;
            var topic = "/test";

            //
            ushort commitTimeout = 2;
            var    client        = new Adf.QueueServerClient(host, port, topic, commitTimeout);

            client.Timeout = 30;
            Console.WriteLine("connect {0}:{1}", host, port);

            client.Connect(5);

            //
            Push(client);

            Console.WriteLine("commit timeout: " + commitTimeout + "s");
            Console.WriteLine("loop");
            for (int i = 0; i < int.MaxValue; i++)
            {
                Console.WriteLine();

                var msg = client.Pull();
                Console.WriteLine("msgid: {0}, dup: {1}", msg.MessageId, msg.Duplications);

                if (i % 5 == 0)
                {
                    Console.WriteLine("Sleep(1.5s) and commit");
                    System.Threading.Thread.Sleep(1500);
                    client.Commit(msg.MessageId);

                    if (client.Count() == 0)
                    {
                        Push(client);
                    }
                }
                else
                {
                    Console.WriteLine("Sleep(3s)");
                    System.Threading.Thread.Sleep(3000);
                }
            }
        }
コード例 #2
0
ファイル: QueueServerTest.cs プロジェクト: aooshi/adf
        public void Test()
        {
            var host = "";

            host = "127.0.0.1";
            host = "192.168.199.13";
            var    port            = 231;
            var    manual          = false;
            var    connectionCount = 64;
            ushort commitTimeout   = 120;
            var    topic           = "/test/1";
            //var batchtest = false;
            var enableParallel = false;

            var dictionary = new Dictionary <UInt64, int>(SIZE);
            var count      = 0u;
            var client     = new Adf.QueueServerClient(host, port, topic, commitTimeout);

            client.Timeout = 30;
            Console.WriteLine("connect {0}:{1}", host, port);
            client.Connect(5);


            Console.WriteLine("topic " + topic);
            Console.WriteLine("subscribe 2 topic ");
            try
            {
                client.Subscribe(topic + "sub1");
                client.Subscribe(topic + "sub2");
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception.ToString());
            }

            var subTopics = client.SubscribeList();

            Console.WriteLine("subscribe list " + string.Join(",", subTopics));

            Console.WriteLine("please key enter");
            Console.ReadLine();

            //--------------------------------------

            Console.WriteLine("lpush 3");
            client.LPush(new byte[0]);
            client.LPush(new byte[0]);
            client.LPush(new byte[0]);
            Console.WriteLine("rpush 3");
            client.RPush(new byte[0]);
            client.RPush(new byte[0]);
            client.RPush(new byte[0]);


            Console.WriteLine("unsubscribe " + topic + "sub1");
            Console.WriteLine("unsubscribe " + topic + "sub2");
            client.Unsubscribe(topic + "sub1");
            client.Unsubscribe(topic + "sub2");

            subTopics = client.SubscribeList();
            Console.WriteLine("subscribe list " + string.Join(",", subTopics));

            Console.WriteLine("please key enter");
            Console.ReadLine();

            Console.WriteLine();
            client.Select(topic + "sub1");
            client.Delete();
            Console.WriteLine(topic + "sub1 delete");
            //
            client.Select(topic + "sub2");
            client.Delete();
            Console.WriteLine(topic + "sub2 delete");


            Console.WriteLine();
            client.Select(topic);
            Console.WriteLine("topic " + client.Topic);


            count = client.Count();
            Console.WriteLine();
            Console.WriteLine("count " + count);


            count = client.Clear();
            Console.WriteLine("clear " + count);


            //--------------------------------------
            this.Push(SIZE, client, dictionary);


            //--------------------------------------
            this.Pull(SIZE, client, dictionary);

            //--------------------------------------

            count = client.Count();
            Console.WriteLine();
            Console.WriteLine("count " + count);

            //--------------------------------------
            this.PushAsync(SIZE * 10, client, dictionary);
            //Console.WriteLine();
            //Console.WriteLine("async pull - commit " + SIZE);
            //this.PullAsync(SIZE, client);
            count = client.Count();
            Console.WriteLine();
            Console.WriteLine("count " + count);
            if (manual)
            {
                Console.WriteLine("key enter continue");
                Console.ReadLine();
            }


            //--------------------------------------
            if (enableParallel)
            {
                this.ParallelPushAndPullCommit(SIZE, client, dictionary);
                count = client.Count();
                Console.WriteLine();
                Console.WriteLine("count " + count);
                if (manual)
                {
                    Console.WriteLine("key enter continue");
                    Console.ReadLine();
                }
            }

            //count = client.Count();
            //Console.WriteLine();
            //Console.WriteLine("count " + count);
            //if (SPLIT)
            //{
            //    Console.WriteLine("key enter continue");
            //    Console.ReadLine();
            //}
            ////--------------------------------------


            //this.ParallelPushAndPullAsync(SIZE, client, dictionary);


            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("push " + 20);
            for (int i = 0; i < 20; i++)
            {
                var data  = Adf.BaseDataConverter.ToBytes(i);
                var msgid = client.RPush(data);
                dictionary.Add(msgid, i);
            }

            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("commit 10");

            for (int i = 0; i < 10; i++)
            {
                var msg = client.Pull();
                client.Commit(msg.MessageId);
                Console.WriteLine("pull message id:{0}, duplications:{1}, length: {2}", msg.MessageId, msg.Duplications, msg.Data.Length);
                Console.WriteLine("commit message id:" + msg.MessageId);
            }

            count = client.Count();
            Console.WriteLine();
            Console.WriteLine("count " + count);


            Console.WriteLine();
            Console.WriteLine();
            Console.WriteLine("rollback 10");
            for (int i = 0; i < 10; i++)
            {
                var msg = client.Pull();
                client.Rollback(msg.MessageId);
                Console.WriteLine("pull message id:{0}, duplications:{1}, length: {2}", msg.MessageId, msg.Duplications, msg.Data.Length);
                Console.WriteLine("rollback message id:" + msg.MessageId);
            }


            Console.WriteLine();
            count = client.Count();
            Console.WriteLine("count " + count);


            //check auto rollback
            Console.WriteLine();
            CheckAutoRollback(client);


            Console.WriteLine();
            //if (batchtest)
            //    Console.WriteLine("keep " + threadCount + " connection received-commit " + count);
            //else
            Console.WriteLine("keep " + connectionCount + " thread received-commit " + count);


            var stopwatch    = Stopwatch.StartNew();
            var receiveCount = 0;

            System.Threading.ThreadPool.QueueUserWorkItem(q =>
            {
                var last = 0;
                while (true)
                {
                    if (receiveCount > 0)
                    {
                        if (count == receiveCount)
                        {
                            Console.WriteLine("completed");

                            client.Dispose();

                            break;
                        }

                        last = receiveCount;
                        Console.WriteLine(last + " - " + (last / (double)(stopwatch.ElapsedMilliseconds / 1000)).ToString("0.000") + "/s");
                    }

                    System.Threading.Thread.Sleep(2000);
                }
            });

            //if (batchtest)
            //{
            //    batchClient = new QueueServerBatchClient(host, port, topic, commitTimeout);
            //    batchClient.Received += (QueueServerClient client2, QueueServerMessage message) =>
            //    {
            //        var remoteValue = Adf.BaseDataConverter.ToInt32(message.Data);
            //        var localValue = 0;
            //        lock (dictionary)
            //        {
            //            if (dictionary.TryGetValue(message.MessageId, out localValue) == false)
            //            {
            //                localValue = -1;
            //            }
            //        }
            //        //diff value
            //        if (localValue != remoteValue)
            //        {
            //            Console.WriteLine("receive result error, local: {0} -  remote : {1}, dup: {2}, mid: {3}", localValue, remoteValue, message.Duplications, message.MessageId);
            //        }
            //        client2.Commit(message.MessageId);

            //        System.Threading.Interlocked.Increment(ref receiveCount);
            //    };
            //    batchClient.Receive(threadCount);

            //}
            //else

            client.ReceiveError += this.ReceiveErrorCallback;
            client.Receive((QueueServerClient client6, QueueServerMessageAckArgs message) =>
            {
                var remoteValue = Adf.BaseDataConverter.ToInt32(message.Data);
                var localValue  = 0;
                lock (dictionary)
                    dictionary.TryGetValue(message.MessageId, out localValue);
                //diff value
                if (localValue != remoteValue)
                {
                    Console.WriteLine("receive result error, local: {0} -  remote : {1}, dup: {2}", localValue, remoteValue, message.Duplications);
                }

                System.Threading.Interlocked.Increment(ref receiveCount);

                return(QueueServerReceiveOption.Commit);
            }, connectionCount);


            Console.ReadLine();
            //Console.WriteLine();
            //Console.WriteLine("completed");
        }