Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test multi-threaded in google test?

The following testcode creates a server- and a clientsocket. 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". I have no clue what this should tell me. What is the problem in here? Type is irrelevant as 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. ASSERT_XY() can only be used in void functions.

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;
}
like image 727
ManuelSchneid3r Avatar asked Nov 22 '12 15:11

ManuelSchneid3r


People also ask

How can multithreaded testing be turned off in googletest?

How can multithreaded testing be turned off in googletest? Show activity on this post. There's no commandline switch for single/multi-threading. libgtest is built either single-threading or multi-threading. To make it single-threading, build gtest with ./configure --with-pthreads=no, then link your unit test app without -pthread

How do you test multi-threaded code?

The software police will not arrest you for firing up a thread in a unit test, though how to actually go about testing multi-threaded code is another matter. Some excellent asynchronous technologies like Akka and Vert.x provide test kits to ease the burden.

What are the threading features of Google Test?

Google Test has three features intended to raise awareness of threading issues. A warning is emitted if multiple threads are running when a death test is encountered. Test cases with a name ending in "DeathTest" are run before all other tests.

What is multithreading in unit testing Java?

Unit Testing Java Multithreaded Code We write multithreaded code to run programs concurrently. Multithreading means multiple parts of the same program running concurrently. We divide the same program into multiple parts/threads and run those threads concurrently.


1 Answers

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. ASSERT_XY() can only be used in void functions.

How shall I understand this?

Your functions don't return void, they return void* - i.e. they return something (void* is a pointer-to-anything) while they should return nothing. The FAQ says it is required for the functions which use ASSERT_EQ() to have the void return type.

like image 84
bikjub Avatar answered Oct 28 '22 14:10

bikjub