예제 #1
0
class TestPubSubServer:

    @pytest.fixture(autouse=True)
    def setup(self):
        self.ps_factory = PubSubFactory()

    def test_dataReceived_subscribe(self):
        sub_proto = self.ps_factory.buildProtocol(('127.0.0.1', 0))
        tr = proto_helpers.StringTransport()
        sub_proto.makeConnection(tr)

        sub_proto.dataReceived(make_subscribe_request('test'))

        assert sub_proto in self.ps_factory.topics['test']

    def test_dataReceived_publish(self):
        # Using a mock here since otherwise StringTransport will complain
        # about being passed unicode when data is being sent to subscribers.

        mock_sub = MagicMock()
        self.ps_factory.topics['test'].add(mock_sub)

        pub_proto = self.ps_factory.buildProtocol(('127.0.0.1', 0))
        pub_tr = proto_helpers.StringTransport()
        pub_proto.makeConnection(pub_tr)

        pub_proto.dataReceived(make_publish_request('test', testdata=5))
        mock_sub.transport.write.assert_called_with(
            json.dumps({'testdata': 5}))

    def test_connectionLost(self):
        sub_proto = self.ps_factory.buildProtocol(('127.0.0.1', 0))
        tr = proto_helpers.StringTransport()
        sub_proto.makeConnection(tr)

        sub_proto.dataReceived(make_subscribe_request('test'))

        assert sub_proto in self.ps_factory.topics['test']

        sub_proto.connectionLost(None)

        assert sub_proto not in self.ps_factory.topics['test']
예제 #2
0
파일: test_net.py 프로젝트: mttr/pubsub
class TestPubSubServer:
    @pytest.fixture(autouse=True)
    def setup(self):
        self.ps_factory = PubSubFactory()

    def test_dataReceived_subscribe(self):
        sub_proto = self.ps_factory.buildProtocol(("127.0.0.1", 0))
        tr = proto_helpers.StringTransport()
        sub_proto.makeConnection(tr)

        sub_proto.dataReceived(make_subscribe_request("test"))

        assert sub_proto in self.ps_factory.topics["test"]

    def test_dataReceived_publish(self):
        # Using a mock here since otherwise StringTransport will complain
        # about being passed unicode when data is being sent to subscribers.

        mock_sub = MagicMock()
        self.ps_factory.topics["test"].add(mock_sub)

        pub_proto = self.ps_factory.buildProtocol(("127.0.0.1", 0))
        pub_tr = proto_helpers.StringTransport()
        pub_proto.makeConnection(pub_tr)

        pub_proto.dataReceived(make_publish_request("test", testdata=5))
        mock_sub.transport.write.assert_called_with(json.dumps({"testdata": 5}))

    def test_connectionLost(self):
        sub_proto = self.ps_factory.buildProtocol(("127.0.0.1", 0))
        tr = proto_helpers.StringTransport()
        sub_proto.makeConnection(tr)

        sub_proto.dataReceived(make_subscribe_request("test"))

        assert sub_proto in self.ps_factory.topics["test"]

        sub_proto.connectionLost(None)

        assert sub_proto not in self.ps_factory.topics["test"]
예제 #3
0
 def setup(self):
     self.ps_factory = PubSubFactory()
예제 #4
0
파일: test_net.py 프로젝트: mttr/pubsub
 def setup(self):
     self.ps_factory = PubSubFactory()