Thrift第七课 C#语言测试C++服务器
  TEZNKK3IfmPf 2023年11月14日 62 0

场景

目前通过Thrift框架生成了C#代码,由于没有怎么接触过这门语言,因此直接启动程序模拟多个用户进行测试。在无法修改代码进行负载性测试的情况下,可以使用system函数启动程序,模拟多个用户的实际操作。

#include <windows.h>

int main()

{

while (1)

{

::system("E:/thriftserver/thrift-csharp/ThriftTest/bin/Debug/ThriftTest.exe");

::Sleep(1000);

}

return 0;

}

C#的实际代码如下

       static void ProcessThread(TProtocol protocol)

        {

            TProcessor processor = new PushMessageService.Processor(new CRecvMessageSErviceImpl());

            while (true)

            {

                try

                {

                    while (processor.Process(protocol, protocol)) { };

                    return;

                }

                catch (System.NullReferenceException tt)

                {

                    return;

                }

                catch (Thrift.Transport.TTransportException tt)

                {

                    //服务器没有启动,会抛出此异常

                    return;

                }

            }

        }

 

        static void MonitorThread(TTransport trans, Action<string> callback)

        {

            while (true)

            {

                try

                {

                    if (!trans.Peek())

                    {

                        callback("connect close\n");

                    }

                    Thread.Sleep(200);

                }

                catch (Thrift.TApplicationException ex)

                {

                    callback(ex.Message);

                    return;

                }

            }

        }

 

        static void Main(string[] args)

        {

            TTransport transport = new TBufferedTransport(new TSocket("192.168.1.110", 7001));

            TProtocol protocol = new TBinaryProtocol(transport);

            UploadMessageService.Client client = new UploadMessageService.Client(protocol);

            Action<TProtocol> processAction = new Action<TProtocol>(ProcessThread);

            Action<TTransport, Action<string>> monitorAction = new Action<TTransport, Action<string>>(MonitorThread);

 

            try

            {

                transport.Open();

                processAction.BeginInvoke(protocol, (result) =>

                {

                    processAction.EndInvoke(result);

                }, null);

 

                monitorAction.BeginInvoke(transport, (msg) =>

                {

                    Console.WriteLine("connect stop", msg);

                }, (result) =>

                {

 

                }, null);

 

 

                info.StrUserId = "admin";

                info.StrPassword = "admin";

                client.Login(info);

                }

            }

            catch (System.NullReferenceException  ttx)

            {

                //服务器停止运行,会抛出此异常

                return;

            }

            catch (System.IO.IOException ttx)

            {

                return;

            }

            catch (Thrift.Transport.TTransportException tt)

            {

                //服务器没有启动,会抛出此异常

                return;

            }

            //transport.Close();

//在这里模拟异常情况,没有正常关闭

        }

    }

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   17天前   22   0   0 C++
  TEZNKK3IfmPf   17天前   21   0   0 指针C++
  TEZNKK3IfmPf   2024年05月31日   21   0   0 算法C++
TEZNKK3IfmPf