Files
multi-user/test_replication.py

105 lines
2.7 KiB
Python
Raw Normal View History

2019-07-04 15:56:03 +02:00
import unittest
2019-07-05 18:07:16 +02:00
from replication import ReplicatedDatablock, ReplicatedDataFactory
import umsgpack
import logging
2019-07-05 18:47:40 +02:00
from replication_client import Client, Server
import time
2019-07-05 18:07:16 +02:00
log = logging.getLogger(__name__)
class SampleData():
def __init__(self):
self.map = {
"sample":"data",
"sample":"data",
"sample":"data",
"sample":"data"
}
class RepSampleData(ReplicatedDatablock):
def serialize(self,data):
import pickle
return pickle.dumps(data)
def deserialize(self,data):
import pickle
2019-07-18 18:15:01 +02:00
return pickle.loads(data)
2019-07-05 18:47:40 +02:00
2019-07-18 18:15:01 +02:00
class TestDataFactory(unittest.TestCase):
2019-07-18 16:38:13 +02:00
def test_data_factory(self):
2019-07-07 12:22:45 +02:00
factory = ReplicatedDataFactory()
factory.register_type(SampleData, RepSampleData)
2019-07-05 18:47:40 +02:00
data_sample = SampleData()
2019-07-18 18:15:01 +02:00
rep_sample = factory.construct_from_dcc(data_sample)(owner="toto", data=data_sample)
2019-07-08 20:05:12 +02:00
2019-07-05 18:47:40 +02:00
self.assertEqual(isinstance(rep_sample,RepSampleData), True)
2019-07-07 12:22:45 +02:00
2019-07-18 18:15:01 +02:00
class TestDataReplication(unittest.TestCase):
def __init__(self,methodName='runTest'):
unittest.TestCase.__init__(self, methodName)
def test_empty_snapshot(self):
# Setup
factory = ReplicatedDataFactory()
factory.register_type(SampleData, RepSampleData)
2019-07-07 12:22:45 +02:00
server = Server(factory=factory)
client = Client(factory=factory, id="client_test_callback")
2019-07-18 16:38:13 +02:00
server.serve(port=5570)
client.connect(port=5570)
test_state = client.state
server.stop()
client.disconnect()
2019-07-18 16:38:13 +02:00
self.assertNotEqual(test_state, 2)
2019-07-18 16:38:13 +02:00
2019-07-18 18:15:01 +02:00
def test_register_client_data(self):
# Setup
factory = ReplicatedDataFactory()
factory.register_type(SampleData, RepSampleData)
2019-07-18 16:38:13 +02:00
server = Server(factory=factory)
server.serve(port=5560)
2019-07-18 16:38:13 +02:00
client = Client(factory=factory, id="client_1")
client.connect(port=5560)
2019-07-18 16:38:13 +02:00
client2 = Client(factory=factory, id="client_2")
client2.connect(port=5560)
time.sleep(1)
data_sample_key = client.register(SampleData())
2019-07-06 12:52:15 +02:00
2019-07-18 18:15:01 +02:00
#Waiting for server to receive the datas
time.sleep(2)
test_key = client2._rep_store[data_sample_key]
client.disconnect()
client2.disconnect()
server.stop()
2019-07-05 18:47:40 +02:00
2019-07-18 18:15:01 +02:00
#Check if the server receive them
self.assertNotEqual(test_key, None)
def suite():
suite = unittest.TestSuite()
suite.addTest(TestDataFactory('test_data_factory'))
suite.addTest(TestDataReplication('test_empty_snapshot'))
2019-07-18 18:15:01 +02:00
suite.addTest(TestDataReplication('test_register_client_data'))
2019-07-05 18:07:16 +02:00
2019-07-18 18:15:01 +02:00
return suite
2019-07-04 15:56:03 +02:00
if __name__ == '__main__':
2019-07-18 18:15:01 +02:00
# unittest.main()
runner = unittest.TextTestRunner(failfast=True)
runner.run(suite())