1+ import unittest
2+
3+ __author__ = 'ADlink Technology'
4+
5+ from cdds import *
6+
7+ import os , sys
8+
9+ from idl_parser import parser
10+
11+ parser_ = parser .IDLParser ()
12+
13+ import cdds .py_dds_utils as utils
14+
15+ import time
16+
17+ class WaitsetTest (unittest .TestCase ):
18+ def setUp (self ):
19+
20+ self .helloworld_lib = CDLL (helloworld_lib_path )
21+
22+ self .rt = Runtime .get_runtime ()
23+ self .dp = Participant (0 )
24+ self .pub = Publisher (self .dp )
25+ self .sub = Subscriber (self .dp )
26+
27+ topic_name = "HelloWorldData_Msg"
28+ #type_support = self.get_hello_world_simple_value_type_support()
29+ type_support = self .rt .get_key_value_type_support ()
30+
31+ self .topic = self .dp .create_topic (topic_name , type_support )
32+
33+ self .writer = Writer (self .pub , self .topic , [Reliable (), KeepLastHistory (10 )])
34+ self .reader = Reader (self .sub , self .topic , [Reliable (), KeepLastHistory (10 )])
35+
36+ def tearDown (self ):
37+ self .rt .close ()
38+
39+ def test_init_Waitset (self ):
40+ ws0 = WaitSet (self .dp )
41+ self .assertIsNotNone (ws0 , "Waitset Constructor created an invalid object" )
42+ self .assertIsInstance (ws0 , WaitSet , "Waitset constructor created an object of not expected type" )
43+
44+ mask = DDS_ANY_SAMPLE_STATE | DDS_ANY_INSTANCE_STATE | DDS_ANY_VIEW_STATE
45+ cond = ReadCondition ( self .reader , mask )
46+ self .assertIsNotNone (cond , "ReadCondition creation faild" )
47+
48+ ws = WaitSet (self .dp , cond )
49+ self .assertIsNotNone (ws , "Waitset Constructor created an invalid object" )
50+ self .assertIsInstance (ws , WaitSet , "Waitset constructor created an object of not expected type" )
51+
52+ def test_attach (self ):
53+ mask = DDS_ANY_SAMPLE_STATE | DDS_ANY_INSTANCE_STATE | DDS_ANY_VIEW_STATE
54+ cond = ReadCondition ( self .reader , mask )
55+ self .assertIsNotNone (cond , "ReadCondition creation failed" )
56+
57+ ws = WaitSet (self .dp )
58+ self .assertIsNotNone (ws , "Waitset Constructor created an invalid object" )
59+ self .assertIsInstance (ws , WaitSet , "Waitset constructor created an object of not expected type" )
60+
61+ rc = ws .attach (cond )
62+ self .assertTrue (rc , "Attach did not return the expected result" )
63+
64+ cond_list = ws .conditions
65+ self .assertIsNotNone (cond_list , "Attached conditions of the waitset after attach operation is an invalid value" )
66+ self .assertIn (cond , cond_list , "Condition is not attached to the condition list as expected" )
67+
68+
69+ def test_detach (self ):
70+ mask = DDS_ANY_SAMPLE_STATE | DDS_ANY_INSTANCE_STATE | DDS_ANY_VIEW_STATE
71+ cond = ReadCondition ( self .reader , mask )
72+ self .assertIsNotNone (cond , "ReadCondition creation failed" )
73+
74+ ws = WaitSet (self .dp )
75+ self .assertIsNotNone (ws , "Waitset Constructor created an invalid object" )
76+ self .assertIsInstance (ws , WaitSet , "Waitset constructor created an object of not expected type" )
77+
78+ rc = ws .attach (cond )
79+ self .assertTrue (rc , "Attach did not return the expected result" )
80+
81+ cond_list = ws .conditions
82+ self .assertIsNotNone (cond_list , "Attached conditions of the waitset after attach operation is an invalid value" )
83+ self .assertIn (cond , cond_list , "Condition is not attached to the condition list as expected" )
84+
85+ rc = ws .detach (cond )
86+ self .assertTrue (rc , "Attach did not return the expected result" )
87+
88+ cond_list = ws .conditions
89+ self .assertIsNotNone (cond_list , "Attached conditions of the waitset after attach operation is an invalid value" )
90+ self .assertNotIn (cond , cond_list , "Condition is not attached to the condition list as expected" )
91+
92+ def test_wait (self ):
93+ mask = DDS_ANY_SAMPLE_STATE | DDS_ANY_INSTANCE_STATE | DDS_ANY_VIEW_STATE
94+ cond = ReadCondition ( self .reader , mask )
95+ self .assertIsNotNone (cond , "ReadCondition creation failed" )
96+
97+ ws = WaitSet (self .dp )
98+ self .assertIsNotNone (ws , "Waitset Constructor created an invalid object" )
99+ self .assertIsInstance (ws , WaitSet , "Waitset constructor created an object of not expected type" )
100+
101+ rc = ws .attach (cond )
102+ self .assertTrue (rc , "Attach did not return the expected result" )
103+ cond_list = ws .conditions
104+ self .assertIsNotNone (cond_list , "Attached conditions of the waitset after attach operation is an invalid value" )
105+ self .assertIn (cond , cond_list , "Condition is not attached to the condition list as expected" )
106+
107+ rc = ws .wait (dds_secs (1 ))
108+
109+ self .assertEqual (rc , 0 , "The number of triggered entity is not correct" )
110+
111+ idl_path = '/home/firas/cyclone/cdds-python/lexer/example.idl'
112+ className = "HelloWorldData_Msg"
113+ HelloWorldData_Msg = utils .create_class (className , idl_path )
114+
115+ i = 1
116+ newMsg = HelloWorldData_Msg (userID = i , message = "Other message {0}" .format (i ))
117+ print ("Writer >> Begin writeing data" )
118+ rc = self .writer .write (newMsg )
119+
120+ rc = ws .wait (dds_secs (5 ))
121+
122+ self .assertEqual (rc , 1 , "The number of triggered entity is not correct" )
123+
124+ mask1 = DDS_NOT_READ_SAMPLE_STATE | DDS_ANY_INSTANCE_STATE | DDS_ANY_VIEW_STATE
125+ cond1 = ReadCondition ( self .reader , mask1 )
126+ self .assertIsNotNone (cond1 , "ReadCondition creation failed" )
127+
128+ rc = ws .attach (cond1 )
129+
130+ i = 2
131+ newMsg = HelloWorldData_Msg (userID = i , message = "Other message {0}" .format (i ))
132+ print ("Writer >> Begin writeing data" )
133+ rc = self .writer .write (newMsg )
134+
135+ rc = ws .wait (dds_secs (5 ))
136+ self .assertEqual (rc , 2 , "The number of triggered entity is not correct" )
0 commit comments