Files
multi-user/test_replication.py

82 lines
2.0 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
return pickle.load(data)
2019-07-04 15:56:03 +02:00
2019-07-07 12:22:45 +02:00
class TestDataReplication(unittest.TestCase):
2019-07-05 18:47:40 +02:00
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()
rep_sample = factory.construct_from_dcc(data_sample)(owner="toto")
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-18 16:38:13 +02:00
def test_basic_client_start(self):
2019-07-07 12:22:45 +02:00
factory = ReplicatedDataFactory()
factory.register_type(SampleData, RepSampleData)
2019-07-18 16:38:13 +02:00
server = Server(factory=factory)
server.serve()
2019-07-07 12:22:45 +02:00
2019-07-18 16:38:13 +02:00
client = Client(factory=factory)
client.connect()
time.sleep(1)
self.assertEqual(client.state(), 2)
# def test_register_client_data(self):
# # Setup data factory
# factory = ReplicatedDataFactory()
# factory.register_type(SampleData, RepSampleData)
# server = Server(factory=factory)
# server.serve()
# client = Client(factory=factory)
# client.connect()
# client2 = Client(factory=factory)
# client2.connect()
# data_sample_key = client.register(SampleData())
2019-07-06 12:52:15 +02:00
2019-07-18 16:38:13 +02:00
# #Waiting for server to receive the datas
# time.sleep(1)
2019-07-18 16:38:13 +02:00
# #Check if the server receive them
# self.assertNotEqual(client2._rep_store[data_sample_key],None)
2019-07-05 18:47:40 +02:00
2019-07-05 18:07:16 +02:00
2019-07-04 15:56:03 +02:00
if __name__ == '__main__':
unittest.main()