简体   繁体   English

如何在google测试中测试多线程?

[英]How to test multi-threaded in google test?

The following testcode creates a server- and a clientsocket. 以下测试代码创建服务器和clientocket。 Then client sends a message to server and server replies. 然后客户端向服务器和服务器回复发送消息。 Thats all. 就这样。 But I can't compile. 但我无法编译。 All ASSERT_EQ in the threadfunctions raise the error "error: void value not ignored as it ought to be" . 线程函数中的所有ASSERT_EQ都会引发错误"error: void value not ignored as it ought to be" I have no clue what this should tell me. 我不知道这应该告诉我什么。 What is the problem in here? 这里有什么问题? Type is irrelevant as ASSERT_EQ(1, 1); 类型与ASSERT_EQ(1, 1);无关ASSERT_EQ(1, 1); raises the errors too. 也引发了错误。

EDIT Found this in FAQ from google: 编辑在谷歌的常见问题解答中找到了这个:

Q:My compiler complains "void value not ignored as it ought to be." 问:我的编译器抱怨“无法忽略空值,因为它应该是。” What does this mean? 这是什么意思?

A: You're probably using an ASSERT_XY() in a function that doesn't return void. 答:你可能在一个不返回void的函数中使用ASSERT_XY()。 ASSERT_XY() can only be used in void functions. ASSERT_XY()只能在void函数中使用。

How shall I understand this? 我怎么理解这个?

void * serverfunc(void * ptr);  
void * clientfunc(void * ptr);    

TEST(netTest, insert)
{
  pthread_t mThreadID1, mThreadID2;
  ::pthread_create(&mThreadID1, nullptr, serverfunc, nullptr);
  ::sleep(1);
  ::pthread_create(&mThreadID1, nullptr, clientfunc, nullptr);
  ::pthread_join(mThreadID1, nullptr);
  ::pthread_join(mThreadID2, nullptr);        
}

void * serverfunc(void * ptr)
{
  net::ServerSocket serv(IPV4, TCP, 55555,5);
  net::ServerSocket * conn = serv.accept();
  net::Message msg;

  conn->recvmsg(&msg);

  ASSERT_EQ(msg.size(),5);
  ASSERT_EQ(msg[0],1);
  ASSERT_EQ(msg[1],2);
  ASSERT_EQ(msg[2],3);
  ASSERT_EQ(msg[3],4);
  ASSERT_EQ(msg[4],5);

  msg = {9,8,6};
  ASSERT_EQ(msg.size(),3);
  ASSERT_EQ(msg[0],9);
  ASSERT_EQ(msg[1],8);
  ASSERT_EQ(msg[2],6);

  conn->sendmsg(msg);

  ::sleep(1);

  delete conn;
  return 0;
}

void * clientfunc(void * ptr)
{
  net::ClientSocket clie(IPV4, TCP, "localhost",55555);
  net::Message msg;

  msg = {1,2,3,4,5};
  ASSERT_EQ(msg.size(),5);
  ASSERT_EQ(msg[0],1);
  ASSERT_EQ(msg[1],2);
  ASSERT_EQ(msg[2],3);
  ASSERT_EQ(msg[3],4);
  ASSERT_EQ(msg[4],5);

  clie.sendmsg(msg);

  clie.recvmsg(&msg);

  ASSERT_EQ(msg.size(),3);
  ASSERT_EQ(msg[0],9);
  ASSERT_EQ(msg[1],8);
  ASSERT_EQ(msg[2],6);

  return 0;
}

Q:My compiler complains "void value not ignored as it ought to be." 问:我的编译器抱怨“无法忽略空值,因为它应该是。” What does this mean? 这是什么意思?

A: You're probably using an ASSERT_XY() in a function that doesn't return void. 答:你可能在一个不返回void的函数中使用ASSERT_XY()。 ASSERT_XY() can only be used in void functions. ASSERT_XY()只能在void函数中使用。

How shall I understand this? 我怎么理解这个?

Your functions don't return void , they return void* - ie they return something ( void* is a pointer-to-anything) while they should return nothing. 你的函数不返回void ,它们返回void* - 即它们返回一些东西( void*是一个指向任何东西的指针),而它们应该什么都不返回。 The FAQ says it is required for the functions which use ASSERT_EQ() to have the void return type. 常见问题解答说,使用ASSERT_EQ()的函数需要具有void返回类型。

I have the same problem, too, and I found an "ugly" way to solve it: 我也有同样的问题,我发现了一种“丑陋”的方法来解决它:

void* your_func(void* ptr)
{
    _your_func(ptr);
    reutrn NULL;
}

void _your_func(void* ptr)
{
    ...
    ASSERT_EQ(1, 1);
    ...
}

It looks like the ASSERT_EQ can only be called in a function with the right return type (which appears to be void while yours return void *) 看起来ASSERT_EQ只能在具有正确返回类型的函数中调用(当您返回void *时,它看起来是无效的)

Personally I dislike macro over-use but that's the way it is. 我个人不喜欢宏过度使用,但这就是它的方式。 The issue with the macro is it obfuscates the code, so you can't see what it's doing wrong. 宏的问题是它混淆了代码,所以你无法看到它做错了什么。

So just write such a function and get serverfunc and clientfunc to call it. 所以只需编写这样的函数并获取serverfunc和clientfunc来调用它。

至于建议,应更换void*类型,请法void ,并return 0return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM