コード例 #1
0
    def test_upper(self):
        port = find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/upper.py?handler=handle' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%s' % port)
        wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['text/plain']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=bytes("hello", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("world", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("foo", 'UTF-8'),
                                headers=headers),
                message.Message(payload=bytes("bar", 'UTF-8'),
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        responses = self.stub.Call(generate_messages())
        expected = [b'HELLO', b'WORLD', b'FOO', b'BAR']

        for response in responses:
            self.assertTrue(response.payload in expected)
            expected.remove(response.payload)

        self.assertEqual(0, len(expected))
コード例 #2
0
    def test_accepts_not_supported(self):
        port = find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/concat.py?handler=concat' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%s' % port)
        wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['application/json']),
                'Accept':
                message.Message.HeaderValue(values=['application/xml']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=b'{"foo":"bar","hello":"world"}',
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        try:
            responses = self.stub.Call(generate_messages())
            self.assertEqual(grpc._channel._Rendezvous, type(responses))
            # TODO: Investigate error handling
            # https://github.com/projectriff/python2-function-invoker/issues/5
        except RuntimeError:
            pass
コード例 #3
0
    def test_accepts_text_plain(self):
        port = find_free_port()
        env = {
            'PYTHONPATH':
            self.PYTHONPATH,
            'GRPC_PORT':
            str(port),
            'FUNCTION_URI':
            'file://%s/tests/functions/concat.py?handler=concat' % os.getcwd()
        }

        self.process = subprocess.Popen(
            self.command,
            cwd=self.workingdir,
            shell=True,
            env=env,
            preexec_fn=os.setsid,
        )

        channel = grpc.insecure_channel('localhost:%d' % port)
        wait_until_channel_ready(channel)

        self.stub = function.MessageFunctionStub(channel)

        def generate_messages():
            headers = {
                'Content-Type':
                message.Message.HeaderValue(values=['application/json']),
                'Accept':
                message.Message.HeaderValue(values=['text/plain']),
                'correlationId':
                message.Message.HeaderValue(values=[str(uuid.uuid4())])
            }

            messages = [
                message.Message(payload=b'{"foo":"bar","hello":"world"}',
                                headers=headers),
            ]
            for msg in messages:
                yield msg

        responses = self.stub.Call(generate_messages())

        for response in responses:
            self.assertEqual(b'{"result": "foobarhelloworld"}',
                             response.payload)