class ChannelsDAG(EvolvingDAG): def __init__(self, *args, **kw): super().__init__(*args, **kw) self.gsetLocal = GSet(id='localchannels') self.available.connectTo(self.onAvailable) subscriber.add_async_listener(keyChatChannels, self.onChatChannels) async def initDag(self, ipfsop): return {'channels': {'all': ['#galacteek', '#ipfs'], 'favourites': []}} @property def channels(self): return self.root['channels']['all'] @property def privateChannels(self): return self.root['channels']['private'] @property def channelsSorted(self): return sorted(self.root['channels']['all']) async def onAvailable(self, obj): pass def registerPublic(self, channel): if self.channels and channel not in self.channels: log.debug(f'Registering public channel: {channel}') self.channels.append(channel) self.changed.emit() def registerPrivate(self, channelPath): privChans = self.root['channels'].setdefault('private', []) privChans.append(channelPath) self.changed.emit() async def onChatChannels(self, key, msg): await self.merge(msg.channels) async def merge(self, channels): if self.channelsSorted == channels: return set = GSet(id='remote') [set.add(chan) for chan in channels] for chan in self.channels: if not self.gsetLocal.query(chan): self.gsetLocal.add(chan) async with self.wLock: self.gsetLocal.merge(set) self.root['channels']['all'] = self.gsetLocal.payload self.changed.emit()
def setUp(self): # Create a GSet self.gset1 = GSet(uuid.uuid4()) # Create another GSet self.gset2 = GSet(uuid.uuid4()) # Add elements to gset1 self.gset1.add('a') self.gset1.add('b') # Add elements to gset1 self.gset2.add('b') self.gset2.add('c') self.gset2.add('d')
async def merge(self, channels): if self.channelsSorted == channels: return set = GSet(id='remote') [set.add(chan) for chan in channels] for chan in self.channels: if not self.gsetLocal.query(chan): self.gsetLocal.add(chan) async with self.wLock: self.gsetLocal.merge(set) self.root['channels']['all'] = self.gsetLocal.payload self.changed.emit()
def __init__(self, *args, **kw): super().__init__(*args, **kw) self.gsetLocal = GSet(id='localchannels') self.available.connectTo(self.onAvailable) subscriber.add_async_listener(keyChatChannels, self.onChatChannels)
class TestLWW(unittest.TestCase): def setUp(self): # Create a GSet self.gset1 = GSet(uuid.uuid4()) # Create another GSet self.gset2 = GSet(uuid.uuid4()) # Add elements to gset1 self.gset1.add('a') self.gset1.add('b') # Add elements to gset1 self.gset2.add('b') self.gset2.add('c') self.gset2.add('d') def test_elements_add_correctly_gset(self): self.assertEqual(self.gset1.payload, ['a', 'b']) self.assertEqual(self.gset2.payload, ['b', 'c', 'd']) def test_querying_gset_without_merging(self): # Check gset1 querying self.assertTrue(self.gset1.query('a')) self.assertTrue(self.gset1.query('b')) self.assertFalse(self.gset1.query('c')) self.assertFalse(self.gset1.query('d')) # Check gset2 querying self.assertFalse(self.gset2.query('a')) self.assertTrue(self.gset2.query('b')) self.assertTrue(self.gset2.query('c')) self.assertTrue(self.gset2.query('d')) def test_merging_gset(self): # Check gset1 merging self.gset1.merge(self.gset2) self.assertEqual(self.gset1.payload, ['a', 'b', 'c', 'd']) # Check gset2 merging self.gset2.merge(self.gset1) self.assertEqual(self.gset2.payload, ['a', 'b', 'c', 'd']) # Check if they are both equal self.assertEqual(self.gset1.payload, self.gset2.payload) def test_querying_gset_with_merging(self): # Check gset2 merging self.gset2.merge(self.gset1) self.assertTrue(self.gset2.query('a')) self.assertTrue(self.gset2.query('b')) self.assertTrue(self.gset2.query('c')) self.assertTrue(self.gset2.query('d')) # Check gset1 merging self.gset1.merge(self.gset2) self.assertTrue(self.gset1.query('a')) self.assertTrue(self.gset1.query('b')) self.assertTrue(self.gset1.query('c')) self.assertTrue(self.gset1.query('d'))