test_dimRpcInfo.py

Go to the documentation of this file.
00001 #!/usr/bin/python
00002 #############################################################################
00003 # Python script to test the PyDimRpc and PyDimRpc classes
00004 #############################################################################
00005 
00006 import time, unittest, random
00007 from threading import *
00008 from debug import *
00009 
00010 from dimcpp import * 
00011 # importing for the dim_start_serving()
00012 import dimc
00013 
00014 ##
00015 # @addtogroup examples
00016 # @{
00017 #
00018 class PyRpcInt (DimRpc):
00019     def __init__(self):
00020          DimRpc.__init__(self, 'testRpc1', 'I', 'I')
00021          self.counter = 0
00022 
00023     def rpcHandler(self):
00024          DEBUG('Getting value')
00025          i = self.getInt()
00026          DEBUG("getInt - received: ", i)
00027          i = self.getData()
00028          DEBUG("getData - received: ", i)
00029          self.counter += i
00030          DEBUG('Setting response data ', self.counter+1)
00031          self.setData(self.counter+1) 
00032 
00033 
00034 class PyRpcIntInfo(DimRpcInfo):
00035     def __init__(self):
00036         DimRpcInfo.__init__(self, 'testRpc1', 'I', 'I', -1)
00037         self.value = -1
00038         self.count = 10
00039 
00040     def rpcInfoHandler(self):
00041         DEBUG("Getting value")
00042         i = self.getInt()
00043         DEBUG("Received value ", i)
00044         self.value = i
00045         
00046 # ---------------------------------------------------------------------------
00047 class PyRpcString (DimRpc):
00048     def __init__(self, name='testRpc2'):
00049          DimRpc.__init__(self, name, 'C', 'C')
00050          self.counter = 0
00051 
00052     def rpcHandler(self):
00053          s = self.getData()
00054          DEBUG("getData - received: ", s)
00055          l = [x for x in s]
00056          l.sort()
00057          s = ""
00058          for x in l: s += str(x)
00059          DEBUG('Setting response data ', s)
00060          self.setData(s)
00061 
00062 class PyRpcStringInfo(DimRpcInfo):
00063     def __init__(self, name='testRpc2'):
00064         DimRpcInfo.__init__(self, name, 'C', 'C', 'Error')
00065         self.value = 'Error'
00066 
00067     def rpcInfoHandler(self):
00068         s = self.getString()
00069         DEBUG("Received value '"+s+"'")
00070         self.value = s
00071 
00072 # ---------------------------------------------------------------------------
00073 class PyRpcComposite (DimRpc):
00074     def __init__(self):
00075         self.format_in = 'I:1;F:2;D:2;X:1;C'
00076         self.format_out = 'C:10;I'
00077         DimRpc.__init__(self, 'testRpc3', self.format_in, self.format_out)
00078         self.counter = 0
00079 
00080     def rpcHandler(self):
00081          s = self.getData()
00082          DEBUG("getData - received: ", s)         
00083          tup = ("It's OK123", 1, 2, 3, 4, 5, 6, 7, 8, 9)
00084          DEBUG('Setting response data: ', tup)
00085          self.setData("It's OK123", 1, 2, 3, 4, 5, 6, 7, 8, 9)
00086          #self.setData(tup)
00087 
00088 class PyRpcCompositeInfo(DimRpcInfo):
00089     def __init__(self):
00090         self.format_in = 'I:1;F:2;D:2;X:1;C'
00091         self.format_out = 'C:10;I'
00092         DimRpcInfo.__init__(self, 'testRpc3', self.format_in, 
00093                             self.format_out, None)
00094         self.value = None 
00095 
00096     def rpcInfoHandler(self):
00097         v = self.getData()
00098         DEBUG("Received value ", v)
00099         self.value = v
00100 
00101 #############################################################################
00102 # Implements test classes functionality
00103 #############################################################################
00104 myRpcInt = PyRpcInt()
00105 myRpcIntCaller = PyRpcIntInfo()
00106 myRpcString = PyRpcString()
00107 myRpcStringCaller = PyRpcStringInfo()
00108 myRpcComposite = PyRpcComposite()
00109 myRpcCompositeCaller = PyRpcCompositeInfo()
00110 #myRpcString = PyRpcString()
00111 myRpcFailCaller = PyRpcStringInfo('testRcp4')
00112 DEBUG("Starting DIM")
00113 dimc.dic_disable_padding()
00114 dimc.dis_start_serving()
00115 dimc.dic_disable_padding()
00116 time.sleep(1)
00117 
00118 class TestDimRpc(unittest.TestCase):
00119 
00120     def testRpcInt(self):
00121         print('\n'+80*'-')
00122         print('Running RPC int test')
00123         print(80*'-')
00124         DEBUG('Running RPC int test')
00125         time.sleep(1)
00126         count = 1
00127         res = 0
00128         for i in range(1,10):
00129             count += i
00130             myRpcIntCaller.setData(i)
00131             res = myRpcIntCaller.getData()
00132             self.assertEqual(count, res) 
00133      
00134     def testRpcString(self):
00135         print('\n'+80*'-')
00136         print('Running RPC string test')
00137         print(80*'-'+'\n')
00138         time.sleep(1) # dim threads are not initialized imediately unfortunately 
00139         r=range(1,10)
00140         s1 = ""
00141         s2 = ""
00142         for x in r: s1 += str(x)
00143         random.shuffle(r)
00144         for x in r: s2 += str(x)
00145         myRpcStringCaller.setData(s2)
00146         res1 = myRpcStringCaller.getData() 
00147         res2 = myRpcStringCaller.value  
00148         self.assertEqual(s1, res1)
00149         self.assertEqual(s1, res2)
00150         
00151     def testRpcComposite(self):
00152         print('\n'+80*'-')
00153         print('Running RPC test using composite parameters')
00154         print(80*'-')
00155         tup = (1, 2.1, 2.2, 3.1, 3.2, 1234567890123, "Test string")
00156         DEBUG("Setting data", tup)
00157         myRpcCompositeCaller.setData(1, 2.1, 2.2, 3.1, 3.2, 1234567890123, "Test string")
00158         res1 = myRpcCompositeCaller.getData() 
00159         res2 = myRpcCompositeCaller.value
00160         DEBUG(res2)
00161         tup=("It's OK123", 1, 2, 3, 4, 5, 6, 7, 8, 9)
00162         self.assertEqual(tup, res1)
00163         self.assertEqual(tup, res2)
00164 
00165     def testRpcFailure(self):
00166         print('\n'+80*'-')
00167         print('Running RPC test using composite parameters')
00168         print(80*'-')
00169         myRpcFailCaller.setData('ALA BALA PORTOCALA')
00170         res1 = myRpcFailCaller.value
00171         res2 = myRpcFailCaller.getData()
00172         DEBUG(myRpcFailCaller.value)
00173         self.assertEqual('Error', res1)
00174         self.assertEqual(res1, res2)
00175 
00176 ##
00177 # @}
00178 #
00179 
00180 #def suite():
00181 #    tests = ['testRpcInt', 'testRpcString', 'testRpcComposite']
00182 #    return unittest.TestSuite(map(WidgetTestCase, tests))
00183 
00184 
00185 #############################################################################
00186 # Execute tests
00187 #############################################################################
00188 
00189 if __name__ == '__main__':
00190     unittest.main()
00191 
00192 
00193 

Generated on 5 Feb 2014 for PyDIM by  doxygen 1.4.7