How to test callback Using GTest

1,449 views
Skip to first unread message

vikramranabhatt

unread,
Dec 27, 2011, 12:18:40 PM12/27/11
to Google C++ Testing Framework, vikramr...@gmail.com
I have C++ Library which expose Asynchronous Api.Here I am enumerating
the Device list and the callback is called up in every 400 ms.user can
set Interval here.But the callback never get called.I am not using any
thread in my static library.bool m_bIsDiscovered is supposed to be
updated by the Callback.But callback never get called.
IDeviceEnumerationCallback
{
virtual void onWiFiDeviceDiscovered( WiFiDeviceInfo*
pDeviceInfo,unsigned short nNoOfDevice) = 0;

};
class IDeviceDiscovery
{
virtual int
InitialiseDeviceDiscovery(IWifiCameraEnumerationCallback*) = 0;
virtual int EnumerateDevice() = 0;
virtual void UnInitialiseDiscovery() = 0;
};

Unit Test

I have Developed a class which implements the Callback Interface
and

class CEnumDevTst:IDeviceEnumerationCallback
{
// implemented the Callback
bool m_bIsDeviceDiscovered;
}

class CUEnumDevTst: public testing::Test
{

}
void CUEnumDevTst::SetUp()
{
m_CEnumDevTst = new CEnumDevTst();
}
void CUEnumDevTst::TearDown()
{
if (NULL != m_CEnumDevTst)
{
delete CEnumDevTst;
m_CEnumDevTst = NULL;
}
}

TEST_F(CUEnumWifiDevTst,WiFiDisc_Callback)

{
ASSERT_TRUE( NULL != m_CEnumDevTst );

int nret = m_CEnumDevTst ->EnumWiFiDevice();

Sleep(400);
EXPECT_TRUE(true == m_CEnumDevTst ->IsDeviceDiscovered());
}

};

Here I awaiting for callback for 400 ms but the callback never get
called.

Greg Miller

unread,
Jan 3, 2012, 11:16:39 AM1/3/12
to vikramranabhatt, Google C++ Testing Framework
What calls your callback? If you're not using threads, are you relying on some other event loop mechanism? If your callback is to be called asynchronously, you need to either have a thread call your callback or somehow return control to some event loop you may be using.

Greg
Reply all
Reply to author
Forward
0 new messages