コード例 #1
0
def generic_test(self, should_authenticate=False):

    endpoint, appkey = get_test_endpoint_and_appkey('../credentials.json')

    if should_authenticate:
        auth_args = ['--role_name', role, '--role_secret', secret]
        channel = restricted_channel
    else:
        auth_args = []
        channel = make_channel_name('test_cli')

    publisher = subprocess.Popen([
        'python', 'satori_rtm_cli.py', '--appkey', appkey, '--endpoint',
        endpoint, 'publish', channel
    ] + auth_args,
                                 stdout=subprocess.PIPE,
                                 stderr=subprocess.PIPE,
                                 stdin=subprocess.PIPE)

    subscriber = subprocess.Popen([
        'python', 'satori_rtm_cli.py', '--appkey', appkey, '--endpoint',
        endpoint, 'subscribe', channel
    ] + auth_args,
                                  stdout=subprocess.PIPE,
                                  stderr=subprocess.PIPE,
                                  stdin=subprocess.PIPE)

    if subscriber.returncode is not None:
        (sub_out, sub_err) = subscriber.communicate()
        print(sub_out)
        print(sub_err)

    self.assertEqual(subscriber.returncode, None)

    time.sleep(5)

    try:
        publisher.stdin.write(string_message + b'\n')
        publisher.stdin.flush()

        time.sleep(1)

        publisher.stdin.write(json_message + b'\n')
        publisher.stdin.flush()

        time.sleep(1)

        publisher.send_signal(signal.SIGINT)
        (pub_out, pub_err) = publisher.communicate()

        self.assertEqual(
            0, publisher.returncode,
            "publisher failed with code {0}".format(publisher.returncode))
        self.assertEqual(
            u'\n'.join([
                u'Sending input to {0}, press C-d or C-c to stop\n'.format(
                    channel.decode('utf8'))
            ]), pub_out.decode('utf8'))

        time.sleep(1)

        subscriber.send_signal(signal.SIGINT)
        (sub_out, sub_err) = subscriber.communicate()
        self.assertEqual(
            subscriber.returncode, 0,
            "subscriber failed with code {0}".format(subscriber.returncode))
        self.assertEqual(
            u'\n'.join([
                u'{0}: "{1}"',
                u'{0}: {2}\n',
            ]).format(channel.decode('utf8'), string_message.decode('utf8'),
                      json_message.decode('utf8')), sub_out.decode('utf8'))
    except Exception as e:
        try:
            print('Publisher out, err: {0}'.format(publisher.communicate()))
            print('Subscriber out, err: {0}'.format(subscriber.communicate()))
        except:
            pass
        raise e
コード例 #2
0
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from __future__ import print_function

import sys
import time

from satori.rtm.client import make_client, SubscriptionMode
from test.utils import get_test_endpoint_and_appkey

channel = 'my_channel'
endpoint, appkey = get_test_endpoint_and_appkey()


def main():

    global channel
    if len(sys.argv) > 1:
        channel = sys.argv[1]

    print('Creating satori client')
    with make_client(endpoint=endpoint, appkey=appkey) as client:

        class SubscriptionObserver(object):
            def __init__(self, sub_id):
                self._sub_id = sub_id

            def on_enter_subscribed(self):
                print('Established subscription {0}'.format(self._sub_id))
コード例 #3
0
#!/usr/bin/env python

import json
import re
import signal
import subprocess
import time
import unittest

from test.utils import get_test_endpoint_and_appkey
from test.utils import get_test_role_name_secret_and_channel
from test.utils import make_channel_name

endpoint, appkey = get_test_endpoint_and_appkey('../credentials.json')
role, secret, restricted_channel =\
    get_test_role_name_secret_and_channel('../credentials.json')
string_message = b'string message'
json_message = b'{"text": ["json", "message"]}'


class TestCLI(unittest.TestCase):
    def test_without_auth(self):
        generic_test(self, should_authenticate=False)

    def test_with_auth(self):
        generic_test(self, should_authenticate=True)

    def test_reconnect(self):
        def start_tcpkali():
            return subprocess.Popen(['tcpkali', '-T5s', '--ws', '-l', '8999'])