script/lib/tweetstream/tests/test_tweetstream.py
author Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
Fri, 01 Jul 2011 10:15:32 +0200
changeset 13 79b6e132e3d7
parent 12 4daf47fcf792
child 14 10e7a0c7c64f
child 207 621fa6caec0c
permissions -rw-r--r--
upgrade tweetstream to 1.0.0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     1
import contextlib
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     2
import threading
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     3
import time
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     4
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
     5
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
     6
from tweetstream import TweetStream, FollowStream, TrackStream, LocationStream
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
     7
from tweetstream import ConnectionError, AuthenticationError, SampleStream
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
     8
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
     9
import pytest
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    10
from pytest import raises
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    11
slow = pytest.mark.slow
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    13
from servercontext import test_server
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    14
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    15
single_tweet = r"""{"in_reply_to_status_id":null,"in_reply_to_user_id":null,"favorited":false,"created_at":"Tue Jun 16 10:40:14 +0000 2009","in_reply_to_screen_name":null,"text":"record industry just keeps on amazing me: http:\/\/is.gd\/13lFo - $150k per song you've SHARED, not that somebody has actually DOWNLOADED.","user":{"notifications":null,"profile_background_tile":false,"followers_count":206,"time_zone":"Copenhagen","utc_offset":3600,"friends_count":191,"profile_background_color":"ffffff","profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/250715794\/profile_normal.png","description":"Digital product developer, currently at Opera Software. My tweets are my opinions, not those of my employer.","verified_profile":false,"protected":false,"favourites_count":0,"profile_text_color":"3C3940","screen_name":"eiriksnilsen","name":"Eirik Stridsklev N.","following":null,"created_at":"Tue May 06 12:24:12 +0000 2008","profile_background_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_background_images\/10531192\/160x600opera15.gif","profile_link_color":"0099B9","profile_sidebar_fill_color":"95E8EC","url":"http:\/\/www.stridsklev-nilsen.no\/eirik","id":14672543,"statuses_count":506,"profile_sidebar_border_color":"5ED4DC","location":"Oslo, Norway"},"id":2190767504,"truncated":false,"source":"<a href=\"http:\/\/widgets.opera.com\/widget\/7206\">Twitter Opera widget<\/a>"}"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    16
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    17
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    18
def parameterized(funcarglist):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    19
    def wrapper(function):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    20
        function.funcarglist = funcarglist
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    21
        return function
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    22
    return wrapper
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    23
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    24
def pytest_generate_tests(metafunc):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    25
    for funcargs in getattr(metafunc.function, 'funcarglist', ()):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    26
        metafunc.addcall(funcargs=funcargs)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    27
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    28
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    29
streamtypes = [
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    30
    dict(cls=TweetStream, args=[], kwargs=dict()),
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    31
    dict(cls=SampleStream, args=[], kwargs=dict()),
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    32
    dict(cls=FollowStream, args=[[1, 2, 3]], kwargs=dict()),
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    33
    dict(cls=TrackStream, args=["opera"], kwargs=dict()),
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    34
    dict(cls=LocationStream, args=["123,4321"], kwargs=dict())
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    35
]
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    36
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    37
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    38
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    39
def test_bad_auth(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    40
    """Test that the proper exception is raised when the user could not be
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    41
    authenticated"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    42
    def auth_denied(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    43
        request.send_error(401)
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    44
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    45
    with test_server(handler=auth_denied, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    46
        stream = cls("user", "passwd", *args, url=server.baseurl)
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    47
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    48
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    49
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    50
def test_404_url(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    51
    """Test that the proper exception is raised when the stream URL can't be
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    52
    found"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    53
    def not_found(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    54
        request.send_error(404)
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    55
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    56
    with test_server(handler=not_found, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    57
        stream = cls("user", "passwd", *args, url=server.baseurl)
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    58
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    59
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    60
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    61
def test_bad_content(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    62
    """Test error handling if we are given invalid data"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    63
    def bad_content(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    64
        for n in xrange(10):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    65
            # what json we pass doesn't matter. It's not verifying the
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    66
            # strcuture, only checking that it's parsable
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    67
            yield "[1,2,3]"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    68
        yield "[1,2, I need no stinking close brace"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    69
        yield "[1,2,3]"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    70
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    71
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    72
    with raises(ConnectionError):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    73
        with test_server(handler=bad_content, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    74
            stream = cls("user", "passwd", *args, url=server.baseurl)
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    75
            for tweet in stream:
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    76
                pass
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    77
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    78
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    79
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    80
def test_closed_connection(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    81
    """Test error handling if server unexpectedly closes connection"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    82
    cnt = 1000
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    83
    def bad_content(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    84
        for n in xrange(cnt):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    85
            # what json we pass doesn't matter. It's not verifying the
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    86
            # strcuture, only checking that it's parsable
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    87
            yield "[1,2,3]"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    88
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    89
    with raises(ConnectionError):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    90
        with test_server(handler=bad_content, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    91
            stream = cls("foo", "bar", *args, url=server.baseurl)
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    92
            for tweet in stream:
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    93
                pass
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
    94
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    95
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    96
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    97
def test_bad_host(cls, args, kwargs):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    98
    """Test behaviour if we can't connect to the host"""
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
    99
    with raises(ConnectionError):
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   100
        stream = cls("username", "passwd", *args, url="http://wedfwecfghhreewerewads.foo")
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   101
        stream.next()
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   102
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   103
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   104
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   105
def smoke_test_receive_tweets(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   106
    """Receive 100k tweets and disconnect (slow)"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   107
    total = 100000
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   108
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   109
    def tweetsource(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   110
        while True:
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   111
            yield single_tweet + "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   112
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   113
    with test_server(handler=tweetsource, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   114
        stream = cls("foo", "bar", *args, url=server.baseurl)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   115
        for tweet in stream:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   116
            if stream.count == total:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   117
                break
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   118
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   119
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   120
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   121
def test_keepalive(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   122
    """Make sure we behave sanely when there are keepalive newlines in the
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   123
    data recevived from twitter"""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   124
    def tweetsource(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   125
        yield single_tweet+"\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   126
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   127
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   128
        yield single_tweet+"\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   129
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   130
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   131
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   132
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   133
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   134
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   135
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   136
        yield single_tweet+"\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   137
        yield "\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   138
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   139
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   140
    with test_server(handler=tweetsource, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   141
        stream = cls("foo", "bar", *args, url=server.baseurl)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   142
        try:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   143
            for tweet in stream:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   144
                pass
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   145
        except ConnectionError:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   146
            assert stream.count == 3, "Got %s, wanted 3" % stream.count
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   147
        else:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   148
            assert False, "Didn't handle keepalive"
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   149
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   150
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   151
@slow
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   152
@parameterized(streamtypes)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   153
def test_buffering(cls, args, kwargs):
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   154
    """Test if buffering stops data from being returned immediately.
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   155
    If there is some buffering in play that might mean data is only returned
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   156
    from the generator when the buffer is full. If buffer is bigger than a
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   157
    tweet, this will happen. Default buffer size in the part of socket lib
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   158
    that enables readline is 8k. Max tweet length is around 3k."""
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   159
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   160
    def tweetsource(request):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   161
        yield single_tweet+"\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   162
        time.sleep(2)
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   163
        # need to yield a bunch here so we're sure we'll return from the
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   164
        # blocking call in case the buffering bug is present.
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   165
        for n in xrange(100):
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   166
            yield single_tweet+"\n"
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   167
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   168
13
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   169
    with test_server(handler=tweetsource, methods=("post", "get"), port="random") as server:
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   170
        stream = cls("foo", "bar", *args, url=server.baseurl)
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   171
        start = time.time()
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   172
        stream.next()
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   173
        first = time.time()
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   174
        diff = first - start
79b6e132e3d7 upgrade tweetstream to 1.0.0
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents: 12
diff changeset
   175
        assert diff < 1, "Getting first tweet took more than a second!"
12
4daf47fcf792 move lib and prepare tweetstream for modification
Yves-Marie Haussonne <1218002+ymph@users.noreply.github.com>
parents:
diff changeset
   176