Created
          August 26, 2009 02:04 
        
      - 
      
- 
        Save typester/175245 to your computer and use it in GitHub Desktop. 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | From e3d97037aafb701374c02bf5cd1a3d7e2454188c Mon Sep 17 00:00:00 2001 | |
| From: Daisuke Murase <typester@cpan.org> | |
| Date: Wed, 26 Aug 2009 11:03:06 +0900 | |
| Subject: [PATCH] fixed a cleanup bug | |
| --- | |
| lib/AnyEvent/Twitter/Stream.pm | 4 +- | |
| t/cleanup.t | 62 ++++++++++++++++++++++++++++++++++++++++ | |
| 2 files changed, 64 insertions(+), 2 deletions(-) | |
| create mode 100644 t/cleanup.t | |
| diff --git a/lib/AnyEvent/Twitter/Stream.pm b/lib/AnyEvent/Twitter/Stream.pm | |
| index ade0139..0fd3ed1 100644 | |
| --- a/lib/AnyEvent/Twitter/Stream.pm | |
| +++ b/lib/AnyEvent/Twitter/Stream.pm | |
| @@ -35,7 +35,7 @@ sub new { | |
| my $self = bless {}, $class; | |
| - http_get $uri, | |
| + $self->{connection_guard} = http_get $uri, | |
| headers => { Authorization => "Basic $auth" }, | |
| on_header => sub { | |
| my($headers) = @_; | |
| @@ -47,7 +47,6 @@ sub new { | |
| want_body_handle => 1, # for some reason on_body => sub {} doesn't work :/ | |
| sub { | |
| my ($handle, $headers) = @_; | |
| - Scalar::Util::weaken($self); | |
| $self->{_handle} = $handle; | |
| if ($handle) { | |
| @@ -68,6 +67,7 @@ sub new { | |
| $self->{guard} = AnyEvent::Util::guard { undef $reader }; | |
| } | |
| }; | |
| + Scalar::Util::weaken($self); | |
| return $self; | |
| } | |
| diff --git a/t/cleanup.t b/t/cleanup.t | |
| new file mode 100644 | |
| index 0000000..c7b78f2 | |
| --- /dev/null | |
| +++ b/t/cleanup.t | |
| @@ -0,0 +1,62 @@ | |
| +use Test::Base; | |
| +use Test::TCP; | |
| + | |
| +plan 'no_plan'; | |
| + | |
| +use AnyEvent::Socket; | |
| +use AnyEvent::Handle; | |
| + | |
| +use AnyEvent::Twitter::Stream; | |
| + | |
| +my $port = empty_port; | |
| + | |
| +# mock server | |
| +my $connect_state = 'initial'; | |
| +my $server = tcp_server undef, $port, sub { | |
| + my ($fh) = @_ or die $!; | |
| + | |
| + $connect_state = 'connected'; | |
| + | |
| + my $handle; $handle = AnyEvent::Handle->new( | |
| + fh => $fh, | |
| + on_eof => sub { | |
| + $connect_state = 'disconnected'; | |
| + }, | |
| + on_error => sub { | |
| + die $_[2]; | |
| + undef $handle; | |
| + }, | |
| + on_read => sub { | |
| + $_[0]->push_read( line => sub {} ); # ignore input | |
| + }, | |
| + ); | |
| +}; | |
| + | |
| +# mock URI::query_form; | |
| +{ | |
| + require URI::_query;; | |
| + no warnings 'redefine', 'once'; | |
| + *URI::_query::query_form = sub { | |
| + $_[0] = URI->new("http://127.0.0.1:$port/") | |
| + }; | |
| +} | |
| + | |
| +my $cv = AnyEvent->condvar; | |
| + | |
| +{ | |
| + my $stream = AnyEvent::Twitter::Stream->new( method => 'firehose' ); | |
| +} | |
| +# $stream destroyed here | |
| + | |
| +{ | |
| + my $t; $t = AnyEvent->timer( | |
| + after => 0.5, | |
| + cb => sub { | |
| + undef $t; | |
| + is $connect_state, 'disconnected', 'disconnected ok'; | |
| + $cv->send; | |
| + }, | |
| + ); | |
| +} | |
| + | |
| +$cv->recv; | |
| -- | |
| 1.6.0.2 | |
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment