2019-03-25 17:38:05 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# Copyright 2019 New Vector Ltd
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-03-25 15:54:01 +01:00
|
|
|
|
2019-11-27 22:54:07 +01:00
|
|
|
from mock import Mock
|
|
|
|
|
2020-04-06 10:58:42 +02:00
|
|
|
from synapse.replication.tcp.handler import ReplicationCommandHandler
|
2019-03-25 17:38:05 +01:00
|
|
|
from synapse.replication.tcp.protocol import ClientReplicationStreamProtocol
|
|
|
|
from synapse.replication.tcp.resource import ReplicationStreamProtocolFactory
|
|
|
|
|
|
|
|
from tests import unittest
|
|
|
|
from tests.server import FakeTransport
|
|
|
|
|
|
|
|
|
|
|
|
class BaseStreamTestCase(unittest.HomeserverTestCase):
|
|
|
|
"""Base class for tests of the replication streams"""
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2020-04-06 10:58:42 +02:00
|
|
|
def make_homeserver(self, reactor, clock):
|
|
|
|
self.test_handler = Mock(wraps=TestReplicationDataHandler())
|
|
|
|
return self.setup_test_homeserver(replication_data_handler=self.test_handler)
|
|
|
|
|
2019-03-25 17:38:05 +01:00
|
|
|
def prepare(self, reactor, clock, hs):
|
|
|
|
# build a replication server
|
2020-04-06 10:58:42 +02:00
|
|
|
server_factory = ReplicationStreamProtocolFactory(hs)
|
|
|
|
self.streamer = hs.get_replication_streamer()
|
2020-03-25 15:54:01 +01:00
|
|
|
self.server = server_factory.buildProtocol(None)
|
2019-03-25 17:38:05 +01:00
|
|
|
|
2020-04-06 10:58:42 +02:00
|
|
|
repl_handler = ReplicationCommandHandler(hs)
|
|
|
|
repl_handler.handler = self.test_handler
|
2019-03-25 17:38:05 +01:00
|
|
|
self.client = ClientReplicationStreamProtocol(
|
2020-04-06 10:58:42 +02:00
|
|
|
hs, "client", "test", clock, repl_handler,
|
2019-03-25 17:38:05 +01:00
|
|
|
)
|
|
|
|
|
2020-03-25 15:54:01 +01:00
|
|
|
self._client_transport = None
|
|
|
|
self._server_transport = None
|
|
|
|
|
|
|
|
def reconnect(self):
|
|
|
|
if self._client_transport:
|
|
|
|
self.client.close()
|
|
|
|
|
|
|
|
if self._server_transport:
|
|
|
|
self.server.close()
|
|
|
|
|
|
|
|
self._client_transport = FakeTransport(self.server, self.reactor)
|
|
|
|
self.client.makeConnection(self._client_transport)
|
|
|
|
|
|
|
|
self._server_transport = FakeTransport(self.client, self.reactor)
|
|
|
|
self.server.makeConnection(self._server_transport)
|
|
|
|
|
|
|
|
def disconnect(self):
|
|
|
|
if self._client_transport:
|
|
|
|
self._client_transport = None
|
|
|
|
self.client.close()
|
|
|
|
|
|
|
|
if self._server_transport:
|
|
|
|
self._server_transport = None
|
|
|
|
self.server.close()
|
2019-03-25 17:38:05 +01:00
|
|
|
|
|
|
|
def replicate(self):
|
|
|
|
"""Tell the master side of replication that something has happened, and then
|
|
|
|
wait for the replication to occur.
|
|
|
|
"""
|
|
|
|
self.streamer.on_notifier_poke()
|
|
|
|
self.pump(0.1)
|
|
|
|
|
|
|
|
|
2020-04-06 10:58:42 +02:00
|
|
|
class TestReplicationDataHandler:
|
|
|
|
"""Drop-in for ReplicationDataHandler which just collects RDATA rows"""
|
2019-05-10 07:12:11 +02:00
|
|
|
|
2019-03-25 17:38:05 +01:00
|
|
|
def __init__(self):
|
2020-03-25 15:54:01 +01:00
|
|
|
self.streams = set()
|
|
|
|
self._received_rdata_rows = []
|
2019-03-25 17:38:05 +01:00
|
|
|
|
|
|
|
def get_streams_to_replicate(self):
|
2020-03-25 15:54:01 +01:00
|
|
|
positions = {s: 0 for s in self.streams}
|
|
|
|
for stream, token, _ in self._received_rdata_rows:
|
|
|
|
if stream in self.streams:
|
|
|
|
positions[stream] = max(token, positions.get(stream, 0))
|
|
|
|
return positions
|
2019-03-25 17:38:05 +01:00
|
|
|
|
2020-01-16 10:16:12 +01:00
|
|
|
async def on_rdata(self, stream_name, token, rows):
|
2019-03-25 17:38:05 +01:00
|
|
|
for r in rows:
|
2020-03-25 15:54:01 +01:00
|
|
|
self._received_rdata_rows.append((stream_name, token, r))
|
2020-04-06 10:58:42 +02:00
|
|
|
|
|
|
|
async def on_position(self, stream_name, token):
|
|
|
|
pass
|