Exemple #1
0
/**
 * Test all 8 bit char values for values. All should be valid.
 */
void ConnectorTest4::testPubSub_Value8bit()
{
	TU_INIT_TESTCASE("testPubSub_Value8bit");
	ConT4_ResetCounters();
	ConT4_ResetPayloads();
	int i = 0;
	int sleep = 0;

	tstring serverUrl = "http://localhost:8000/kn";
	wstring topic = L"/what/Tests/functional/ConnectorT4/testPubSub_Value8bit";

	wchar_t tmpw[258];
	ZeroMemory(tmpw, 258);
	for (i = 0; i < 256; i++){
		tmpw[i] = (unsigned) i;
	}
	wstring valw(tmpw, 256);

	char tmp[258];
	ZeroMemory(tmp, 258);
	for ( i = 0; i < 256; i++){
		tmp[i] = (unsigned char)i;
	}
	string  val(tmp, 256);

	wsPubPayloads.push_back(valw);
	sPubPayloads.push_back(val);



	// Start the subscriber.
	Connector subConn;
	ConT4_SubListener subListener;
	ConT4_SubRequestStatusHandler subReqSH;
	ITransport::Parameters subITParams;

    // This is only necessary to get subConnSH_OnConnStatus
	ConT4_SubConnStatusHandler subConnSH;
	subConn.AddConnectionStatusHandler(&subConnSH);

	subITParams.m_ServerUrl = serverUrl;
   	if (!subConn.Open(subITParams)){
		CPPUNIT_FAIL("Open(subITParams) failed.");
		return;
	}
	
	wstring rid = subConn.Subscribe(topic, &subListener, Message(), &subReqSH);
	// ConT4_DumpStr(L"<sub>rid = " + rid);
	if (rid.length() == 0)
	{
		CPPUNIT_FAIL("Failed to subscribe.");
		return;
	}
	Sleep(100);	// Avoid race conditions.



	// Start the publisher.
	Connector pubConn;
	ConT4_PubRequestStatusHandler pubReqSH;
	ITransport::Parameters pubITParams;
	Message pubMsg;

	pubMsg.Set("do_method", "notify");
	pubMsg.Set(L"kn_to", topic);
	pubMsg.Set("kn_response_format", "simple");

    // This is only necessary to get pubConnSH_OnConnStatus
	ConT4_PubConnStatusHandler pubConnSH;
	pubConn.AddConnectionStatusHandler(&pubConnSH);

	pubITParams.m_ServerUrl = serverUrl;
   	if (!pubConn.Open(pubITParams)){
		CPPUNIT_FAIL("Open(pubITParams) failed.");
		return;
	}
	
	// Publish wstrings.
	for( i = 0; i < wsPubPayloads.size(); i++)
	{
		pubMsg.Set(L"kn_payload", wsPubPayloads[i]);
		pubConn.Publish(pubMsg, &pubReqSH);
	}
	
	// Publish strings.
	for( i = 0; i < sPubPayloads.size(); i++)
	{
		pubMsg.Set("kn_payload", sPubPayloads[i]);
		pubConn.Publish(pubMsg, &pubReqSH);
	}



	// Avoid race conditions.
	Sleep(1000);//<<<<<<<<Subscriber still needs time to get pubs (especially if dumping output).




	// Done, disconnect.
	// ConT4_DumpStr("<sub>Unsubscribing.");
	if (!subConn.Unsubscribe(rid, &subReqSH))
	{
		CPPUNIT_FAIL("failed to unsubscribe.");
	}

	pubConn.Close();
	subConn.Close();

	subConn.RemoveConnectionStatusHandler(&subConnSH);



	// Check that pub == sub - Hardcode to make sure.
	CPPUNIT_ASSERT_MESSAGE("If this happens you could have a race condition."
		,wsSubPayloads.size() == 2);

	// Check that all pubs and subs occurred.
	// ConT4_DumpCounters();
	CPPUNIT_ASSERT(conT4_count_pubConnSH_OnConnStatus==1);// 1st Publish()
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnSuccess    ==2);// Publish() updates
	CPPUNIT_ASSERT(conT4_count_subListener_OnUpdate  ==2);// updates
	CPPUNIT_ASSERT(conT4_count_subConnSH_OnConnStatus==1);// 1st Subscribe()
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnSuccess    ==2);// Subscribe(),Unsubscribe()

	// Convert the string payloads and push them onto the wstring to compare.
	// ConT4_DumpPayloads();
	for( i = 0; i < sPubPayloads.size(); i++)
	{
		wstring tmp = ConvertToWide(sPubPayloads[i]);
		wsPubPayloads.push_back(tmp);
	}

	// Check that all pubs == subs
	// ConT4_DumpPayloads();
	for( i = 0; i < wsPubPayloads.size(); i++)
	{
		CPPUNIT_ASSERT(wsPubPayloads[i] == wsSubPayloads[i]);
	}
}
Exemple #2
0
/**
 * Test all 16 bit char values for values except Unicode surrogate values U+D800 - U+DFFF
 * which can only be used with the appropriate syntax sequences.
 * All other characters should be valid in any order.
 */
void ConnectorTest4::testPubSub_Value16bit()
{
	TU_INIT_TESTCASE("testPubSub_Value16bit");
	ConT4_ResetCounters();
	ConT4_ResetPayloads();
	int i = 0;
	int sleep = 0;

	tstring serverUrl = "http://localhost:8000/kn";
	wstring topic = L"/what/Tests/functional/ConnectorT4/testPubSub_Value16bit";


	const int size01 = 0xd800;			// 0 to D800
	wchar_t tmpw01[size01+2];			// 0 to 1101 0111 1111 1111
	ZeroMemory(tmpw01, size01+2);
	for ( i = 0; i < size01; i++){
		tmpw01[i] = (unsigned) i;
	}
	wstring valw01(tmpw01, size01);	
	wsPubPayloads.push_back(valw01);	

										// skip D800 to DFFF

	const int size02 = 0xffff-0xe000+1;	// E000 to FFFF
	wchar_t tmpw02[size02+2];			// 1110 0000 0000 0000 to 1111 1111 1111 1111
	ZeroMemory(tmpw02, size02+2);
	for ( i = 0; i < size02; i++){
		tmpw02[i] = (unsigned) i + 0xe000;
	}
	wstring valw02(tmpw02, size02);	
	wsPubPayloads.push_back(valw02);	

	// 01 02 03 04 05 06 07 08 09 10
	// Hardcode the number of messages sent to make very sure of results.
	int totalMsgs = 2;

	// Start the subscriber.
	Connector subConn;
	ConT4_SubListener subListener;
	ConT4_SubRequestStatusHandler subReqSH;
	ITransport::Parameters subITParams;

    // This is only necessary to get subConnSH_OnConnStatus
	ConT4_SubConnStatusHandler subConnSH;
	subConn.AddConnectionStatusHandler(&subConnSH);

	subITParams.m_ServerUrl = serverUrl;
   	if (!subConn.Open(subITParams)){
		CPPUNIT_FAIL("Open(subITParams) failed.");
		return;
	}
	
	wstring rid = subConn.Subscribe(topic, &subListener, Message(), &subReqSH);
	// ConT4_DumpStr(L"<sub>rid = " + rid);
	if (rid.length() == 0)
	{
		CPPUNIT_FAIL("Failed to subscribe.");
		return;
	}
	Sleep(100);	// Avoid race conditions.



	// Start the publisher.
	Connector pubConn;
	ConT4_PubRequestStatusHandler pubReqSH;
	ITransport::Parameters pubITParams;
	Message pubMsg;

	pubMsg.Set("do_method", "notify");
	pubMsg.Set(L"kn_to", topic);
	pubMsg.Set("kn_response_format", "simple");
	pubMsg.Set("kn_expires", "+1200");//20 minutes
	pubMsg.Set("testname", "functional/ConnectorT4::testPubSub_Value16bit()");
	// A large value to make sure problem is payload size
	// not the total msg size. If it is the msg size this will force a
	// different msg to fail.
	//pubMsg.Set(L"testbuf", valw01); // Listener does not trigger.
	//pubMsg.Set(L"testbuf", L"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");

    // This is only necessary to get pubConnSH_OnConnStatus
	ConT4_PubConnStatusHandler pubConnSH;
	pubConn.AddConnectionStatusHandler(&pubConnSH);

	pubITParams.m_ServerUrl = serverUrl;
   	if (!pubConn.Open(pubITParams)){
		CPPUNIT_FAIL("Open(pubITParams) failed.");
		return;
	}
	
	// Publish wstrings.
	for( i = 0; i < wsPubPayloads.size(); i++)
	{
		pubMsg.Set(L"kn_payload", wsPubPayloads[i]);
		pubConn.Publish(pubMsg, &pubReqSH);
	}
	


	// Avoid race conditions.
	Sleep(1000);//<<<<<<<<Subscriber still needs time to get pubs (especially if dumping output).
	//Sleep(2000);//<<<<<<<<Subscriber still needs time to get pubs (especially if dumping output).




	// Done, disconnect.
	// ConT4_DumpStr("<sub>Unsubscribing.");
	if (!subConn.Unsubscribe(rid, &subReqSH))
	{
		CPPUNIT_FAIL("failed to unsubscribe.");
	}

	pubConn.Close();
	subConn.Close();

	subConn.RemoveConnectionStatusHandler(&subConnSH);



	// Check that pub == sub - Hardcode to make sure.
	char pubBuf[12];
	itoa(totalMsgs,pubBuf,10);
	char gotBuf[12];
	itoa(wsSubPayloads.size(),gotBuf,10);
	string msg = (string)"If this happens you could have a race condition or other problem.\n" 
			   + (string)"The results will be unpredictable.\n"
			   + (string)"Attempted to publish " + pubBuf + " messages.\n"
			   + (string)"Sub got " + gotBuf + " messages.";
	CPPUNIT_ASSERT_MESSAGE(msg, wsSubPayloads.size() == totalMsgs);

	// Check that all pubs and subs occurred.
	// ConT4_DumpCounters();
	CPPUNIT_ASSERT(conT4_count_pubConnSH_OnConnStatus==1);// 1st Publish()
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnSuccess    ==totalMsgs);// Publish() updates
	CPPUNIT_ASSERT(conT4_count_subListener_OnUpdate  ==totalMsgs);// updates
	CPPUNIT_ASSERT(conT4_count_subConnSH_OnConnStatus==1);// 1st Subscribe()
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnSuccess    ==2);// Subscribe(),Unsubscribe()

	// Check that all pubs == subs
	// ConT4_DumpPayloads();
	for( i = 0; i < wsPubPayloads.size(); i++)
	{	
		// Construct msg in case of error.
		int sent = wsPubPayloads[i].length();
		int got  = wsSubPayloads[i].length();
		char iBuf[12];
		itoa(i,iBuf,10);
		char sentBuf[12];
		itoa(sent,sentBuf,16);
		char gotBuf[12];
		itoa(got,gotBuf,16);
		string msg = (string)"Payload[" + iBuf
				   + (string)"] Sent wstring length=0x" + sentBuf
				   + (string)" ,got length=0x" + gotBuf;

		//ConT4_DumpStr(">>>Pub Payload size =",wsPubPayloads[i].length());
		//ConT4_DumpStr("   Sub Payload size =",wsSubPayloads[i].length());
		CPPUNIT_ASSERT_MESSAGE(msg,wsPubPayloads[i] == wsSubPayloads[i]);
	}
}
Exemple #3
0
/**
 * Any keyboard character should be valid for values.
 * This test is redundant (testPubSub_Value8bit) but it can be useful for demonstration.
 * You must escape '\' and '"' in C++ code.
 */
void ConnectorTest4::testPubSub_ValueKeyboard()
{
	TU_INIT_TESTCASE("testPubSub_ValueKeyboard");
	ConT4_ResetCounters();
	ConT4_ResetPayloads();
	int i = 0;
	int sleep = 0;

	tstring serverUrl = "http://*****:*****@#$%^&*()_+[]\\{}|;':\",./<>?");
	wsPubPayloads.push_back(L" ");
	wsPubPayloads.push_back(L"");
	sPubPayloads.push_back("`1234567890-=~!@#$%^&*()_+[]\\{}|;':\",./<>?");
	sPubPayloads.push_back(" ");
	sPubPayloads.push_back("");



	// Start the subscriber.
	Connector subConn;
	ConT4_SubListener subListener;
	ConT4_SubRequestStatusHandler subReqSH;
	ITransport::Parameters subITParams;

    // This is only necessary to get subConnSH_OnConnStatus
	ConT4_SubConnStatusHandler subConnSH;
	subConn.AddConnectionStatusHandler(&subConnSH);

	subITParams.m_ServerUrl = serverUrl;
   	if (!subConn.Open(subITParams)){
		CPPUNIT_FAIL("Open(subITParams) failed.");
		return;
	}
	
	wstring rid = subConn.Subscribe(topic, &subListener, Message(), &subReqSH);
	// ConT4_DumpStr(L"<sub>rid = " + rid);
	if (rid.length() == 0)
	{
		CPPUNIT_FAIL("Failed to subscribe.");
		return;
	}
	Sleep(100); // Avoid race conditions.



	// Start the publisher.
	Connector pubConn;
	ConT4_PubRequestStatusHandler pubReqSH;
	ITransport::Parameters pubITParams;
	Message pubMsg;

	pubMsg.Set("do_method", "notify");
	pubMsg.Set(L"kn_to", topic);
	pubMsg.Set("kn_response_format", "simple");

    // This is only necessary to get pubConnSH_OnConnStatus
	ConT4_PubConnStatusHandler pubConnSH;
	pubConn.AddConnectionStatusHandler(&pubConnSH);

	pubITParams.m_ServerUrl = serverUrl;
   	if (!pubConn.Open(pubITParams)){
		CPPUNIT_FAIL("Open(pubITParams) failed.");
		return;
	}
	
	// Publish wstrings.
	for( i = 0; i < wsPubPayloads.size(); i++)
	{
		pubMsg.Set(L"kn_payload", wsPubPayloads[i]);
		pubConn.Publish(pubMsg, &pubReqSH);
	}
	
	// Publish strings.
	for( i = 0; i < sPubPayloads.size(); i++)
	{
		pubMsg.Set("kn_payload", sPubPayloads[i]);
		pubConn.Publish(pubMsg, &pubReqSH);
	}



	// Avoid race conditions.
	/*
	sleep = 20; 
	while (pubConn.HasItems())
	{
		Sleep(100);	
		printf("p");
		if( sleep-- == 0 ) break;
	}
	if (pubConn.HasItems())
	{
		CPPUNIT_FAIL("There's something in the publish queue (it should be empty).");
		return;
	}
	sleep = 20; 
	while (subConn.HasItems())
	{
		Sleep(100);	
		printf("s");
		if( sleep-- == 0 ) break;
	}
	if (subConn.HasItems())
	{
		CPPUNIT_FAIL("There's something in the subscribe queue (it should be empty).");
		return;
	}
	//*/
	Sleep(1000);//<<<<<<<<Subscriber still needs time to get pubs (especially if dumping output).



	// Done, disconnect.
	// ConT4_DumpStr("<sub>Unsubscribing.");
	if (!subConn.Unsubscribe(rid, &subReqSH))
	{
		CPPUNIT_FAIL("failed to unsubscribe.");
	}

	pubConn.Close();
	subConn.Close();

	subConn.RemoveConnectionStatusHandler(&subConnSH);



	// Check that pub == sub - Hardcode to make sure.
	CPPUNIT_ASSERT_MESSAGE("If this happens you could have a race condition."
		,wsSubPayloads.size() == 6);

	// Check that all pubs and subs occurred.
	// ConT4_DumpCounters();
	CPPUNIT_ASSERT(conT4_count_pubConnSH_OnConnStatus==1);// 1st Publish()
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_pubReqSH_OnSuccess    ==6);// Publish() updates
	CPPUNIT_ASSERT(conT4_count_subListener_OnUpdate  ==6);// updates
	CPPUNIT_ASSERT(conT4_count_subConnSH_OnConnStatus==1);// 1st Subscribe()
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnStatus     ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnError      ==0);
	CPPUNIT_ASSERT(conT4_count_subReqSH_OnSuccess    ==2);// Subscribe(),Unsubscribe()

	// Convert the string payloads and push them onto the wstring to compare.
	// ConT4_DumpPayloads();
	for( i = 0; i < sPubPayloads.size(); i++)
	{
		wstring tmp = ConvertToWide(sPubPayloads[i]);
		wsPubPayloads.push_back(tmp);
	}

	// Check that all pubs == subs
	// ConT4_DumpPayloads();
	for( i = 0; i < wsPubPayloads.size(); i++)
	{
		CPPUNIT_ASSERT(wsPubPayloads[i] == wsSubPayloads[i]);
	}
}