feat: serialize/deserialize logic
feat: basic replication graph for dependency hanfling refacor: replicateddatablock
This commit is contained in:
@ -4,6 +4,8 @@ import umsgpack
|
||||
import logging
|
||||
from replication_client import Client, Server
|
||||
import time
|
||||
import cProfile
|
||||
import re
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
@ -16,13 +18,23 @@ class SampleData():
|
||||
class RepSampleData(ReplicatedDatablock):
|
||||
def serialize(self,data):
|
||||
import pickle
|
||||
|
||||
|
||||
return pickle.dumps(data)
|
||||
|
||||
def deserialize(self,data):
|
||||
import pickle
|
||||
|
||||
return pickle.loads(data)
|
||||
|
||||
def dump(self):
|
||||
import json
|
||||
output = {}
|
||||
output['map'] = json.dumps(self.pointer.map)
|
||||
return output
|
||||
|
||||
|
||||
def load(self,target=None):
|
||||
target = self.buffer
|
||||
|
||||
|
||||
class TestDataFactory(unittest.TestCase):
|
||||
@ -30,7 +42,7 @@ class TestDataFactory(unittest.TestCase):
|
||||
factory = ReplicatedDataFactory()
|
||||
factory.register_type(SampleData, RepSampleData)
|
||||
data_sample = SampleData()
|
||||
rep_sample = factory.construct_from_dcc(data_sample)(owner="toto", data=data_sample)
|
||||
rep_sample = factory.construct_from_dcc(data_sample)(owner="toto", pointer=data_sample)
|
||||
|
||||
self.assertEqual(isinstance(rep_sample,RepSampleData), True)
|
||||
|
||||
@ -63,7 +75,7 @@ class TestClient(unittest.TestCase):
|
||||
factory.register_type(SampleData, RepSampleData)
|
||||
|
||||
server = Server(factory=factory)
|
||||
client = Client(factory=factory, id="client_test_callback")
|
||||
client = Client(factory=factory, id="cli_test_filled_snapshot")
|
||||
client2 = Client(factory=factory, id="client_2")
|
||||
|
||||
server.serve(port=5575)
|
||||
@ -86,26 +98,27 @@ class TestClient(unittest.TestCase):
|
||||
|
||||
def test_register_client_data(self):
|
||||
# Setup environment
|
||||
|
||||
factory = ReplicatedDataFactory()
|
||||
factory.register_type(SampleData, RepSampleData)
|
||||
|
||||
server = Server(factory=factory)
|
||||
server.serve(port=5560)
|
||||
|
||||
client = Client(factory=factory, id="client_1")
|
||||
client = Client(factory=factory, id="cli_test_register_client_data")
|
||||
client.connect(port=5560)
|
||||
|
||||
client2 = Client(factory=factory, id="client_2")
|
||||
client2 = Client(factory=factory, id="cli2_test_register_client_data")
|
||||
client2.connect(port=5560)
|
||||
|
||||
|
||||
# Test the key registering
|
||||
data_sample_key = client.register(SampleData())
|
||||
|
||||
|
||||
time.sleep(4)
|
||||
#Waiting for server to receive the datas
|
||||
time.sleep(.5)
|
||||
|
||||
rep_test_key = client2._rep_store[data_sample_key].uuid
|
||||
|
||||
|
||||
|
||||
client.disconnect()
|
||||
client2.disconnect()
|
||||
@ -122,21 +135,21 @@ class TestClient(unittest.TestCase):
|
||||
server = Server(factory=factory)
|
||||
server.serve(port=5560)
|
||||
|
||||
client = Client(factory=factory, id="client_1")
|
||||
client = Client(factory=factory, id="cli_test_client_data_intergity")
|
||||
client.connect(port=5560)
|
||||
|
||||
client2 = Client(factory=factory, id="client_2")
|
||||
client2 = Client(factory=factory, id="cli2_test_client_data_intergity")
|
||||
client2.connect(port=5560)
|
||||
|
||||
test_map = {"toto":"test"}
|
||||
# Test the key registering
|
||||
client.register(SampleData(map=test_map))
|
||||
data_sample_key = client.register(SampleData(map=test_map))
|
||||
|
||||
test_map_result = {}
|
||||
#Waiting for server to receive the datas
|
||||
time.sleep(.5)
|
||||
time.sleep(1)
|
||||
|
||||
rep_test_key = client2._rep_store[data_sample_key].uuid
|
||||
client2._rep_store[data_sample_key].load(target=test_map_result)
|
||||
|
||||
|
||||
client.disconnect()
|
||||
@ -144,7 +157,7 @@ class TestClient(unittest.TestCase):
|
||||
server.stop()
|
||||
|
||||
|
||||
self.assertEqual(rep_test_key, data_sample_key)
|
||||
self.assertEqual(test_map_result["toto"], test_map["toto"])
|
||||
|
||||
def suite():
|
||||
suite = unittest.TestSuite()
|
||||
@ -152,9 +165,10 @@ def suite():
|
||||
suite.addTest(TestClient('test_empty_snapshot'))
|
||||
suite.addTest(TestClient('test_filled_snapshot'))
|
||||
suite.addTest(TestClient('test_register_client_data'))
|
||||
|
||||
# suite.addTest(TestClient('test_client_data_intergity'))
|
||||
return suite
|
||||
|
||||
if __name__ == '__main__':
|
||||
runner = unittest.TextTestRunner(failfast=True,verbosity=2)
|
||||
runner.run(suite())
|
||||
runner.run(suite())
|
||||
|
Reference in New Issue
Block a user