minor refactoring
[public/netxms.git] / tests / test-libnetxms / nxcp.cpp
1 #include <nms_common.h>
2 #include <nms_util.h>
3 #include <nxcpapi.h>
4 #include <testtools.h>
5
6 /**
7 * Poster thread
8 */
9 static THREAD_RESULT THREAD_CALL PosterThread(void *arg)
10 {
11 NXCPMessage *msg = new NXCPMessage();
12 msg->setCode(CMD_REQUEST_COMPLETED);
13 msg->setId(42);
14 ThreadSleepMs((UINT32)GetCurrentTimeMs() % 2000);
15 ((MsgWaitQueue *)arg)->put(msg);
16 return THREAD_OK;
17 }
18
19 /**
20 * Test message wait queue
21 */
22 void TestMsgWaitQueue()
23 {
24 StartTest(_T("Message wait queue"));
25
26 MsgWaitQueue *queue = new MsgWaitQueue;
27 ThreadCreate(PosterThread, 0, queue);
28 NXCPMessage *msg = queue->waitForMessage(CMD_REQUEST_COMPLETED, 42, 5000);
29 AssertNotNull(msg);
30 delete msg;
31
32 msg = queue->waitForMessage(CMD_REQUEST_COMPLETED, 42, 1000);
33 AssertNull(msg);
34
35 delete queue;
36
37 EndTest();
38 }
39
40 /**
41 * Test message class
42 */
43 void TestMessageClass()
44 {
45 StartTest(_T("NXCPMessage class"));
46
47 NXCPMessage msg;
48
49 uuid guid = uuid::generate();
50 msg.setField(1, guid);
51 uuid guid2 = msg.getFieldAsGUID(1);
52 AssertTrue(guid.equals(guid2));
53
54 msg.setField(1, (UINT16)1234);
55 AssertTrue(msg.getFieldAsUInt16(1) == 1234);
56
57 msg.setField(1, (UINT32)1234);
58 AssertTrue(msg.getFieldAsUInt32(1) == 1234);
59
60 msg.setField(1, (UINT64)1234);
61 AssertTrue(msg.getFieldAsUInt64(1) == 1234);
62
63 msg.setField(1, _T("test text"));
64 TCHAR buffer[64];
65 AssertTrue(!_tcscmp(msg.getFieldAsString(1, buffer, 64), _T("test text")));
66
67 EndTest();
68 }