本文整理汇总了Python中sync.Sync.parallel_sync方法的典型用法代码示例。如果您正苦于以下问题:Python Sync.parallel_sync方法的具体用法?Python Sync.parallel_sync怎么用?Python Sync.parallel_sync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sync.Sync
的用法示例。
在下文中一共展示了Sync.parallel_sync方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestSyncParallel
# 需要导入模块: from sync import Sync [as 别名]
# 或者: from sync.Sync import parallel_sync [as 别名]
class TestSyncParallel(unittest.TestCase):
"""Test the parallel functionality"""
@patch('sync.GlanceSync', auto_spec=True)
def setUp(self, glancesync):
"""create constructor, mock with glancesync, Set a master region"""
regions = ['region1', 'region2']
self.sync = Sync(regions)
self.glancesync = glancesync
self.log = logging.getLogger('glancesync')
config = {
'return_value.master_region': 'MasterRegion',
'return_value.log': self.log,
'return_value.sync_region.side_effect': lambda region:
time.sleep(1.5) or
self.log.info('Sync ' + region + ' ' + str(time.time()))
}
self.glancesync.configure_mock(**config)
path = os.path.abspath(os.curdir)
self.dir_name = os.path.join(path, 'sync_20200206_2357')
self.tearDown()
def tearDown(self):
"""clean directory and files created during the test"""
if os.path.exists(self.dir_name):
for name in os.listdir(self.dir_name):
os.unlink(os.path.join(self.dir_name, name))
os.rmdir(self.dir_name)
def _check_sync_invoked(self, datetime_mock):
"""Check that the files indicating than the regions are synchronised
are invoked and return the difference of the timestamp where each file
is printed. This way is possible to determine if both process are
invoked at the some time or not.
:param datetime_mock: the absolute difference time, in seconds (float)
:return:
"""
match_obj1 = None
match_obj2 = None
dt = datetime.datetime(2020, 2, 6, 23, 57)
config = {'datetime.now.return_value': dt}
datetime_mock.configure_mock(**config)
self.sync.parallel_sync()
file1 = os.path.join(self.dir_name, 'region1.txt')
file2 = os.path.join(self.dir_name, 'region2.txt')
assert(os.path.exists(file1))
assert(os.path.exists(file2))
data1 = open(file1).read()
data2 = open(file2).read()
# The expected values for data1 and data2 are:
# 'Sync region<region id> <timestamp>' or 'INFO:Sync region<region id> <timestamp>'
regular_expression = r'(INFO:)?Sync region.* (.*)'
match_obj1 = re.match(regular_expression, data1, re.M | re.I)
assert(match_obj1 is not None), 'The file {} does not contain the expected value'.format(file1)
match_obj2 = re.match(regular_expression, data2, re.M | re.I)
assert(match_obj2 is not None), 'The file {} does not contain the expected value'.format(file2)
time1 = float(match_obj1.group(2))
time2 = float(match_obj2.group(2))
return abs(time1 - time2)
@patch('sync.datetime')
def test_parallel_sync(self, datetime_mock):
"""test with support for two clients, so both processes run at the
some time"""
config = {
'return_value.max_children': 2,
}
self.glancesync.configure_mock(**config)
diff = self._check_sync_invoked(datetime_mock)
assert(diff <= 1)
@patch('sync.datetime')
def test_noparallel_sync(self, datetime_mock):
"""test with support for only one client, so one process run first
and then the other one"""
config = {
'return_value.max_children': 1,
}
self.glancesync.configure_mock(**config)
diff = self._check_sync_invoked(datetime_mock)
assert(diff > 1)