spec/ezmlm/listdaemon_spec.rb
changeset 12 3cc813140c80
parent 11 7fc2d1713795
child 13 a03c08c289e9
equal deleted inserted replaced
11:7fc2d1713795 12:3cc813140c80
     1 #!/usr/bin/env ruby
       
     2 
       
     3 BEGIN {
       
     4 	require 'pathname'
       
     5 	basedir = Pathname.new( __FILE__ ).dirname.parent.parent
       
     6 	
       
     7 	libdir = basedir + "lib"
       
     8 	
       
     9 	$LOAD_PATH.unshift( libdir ) unless $LOAD_PATH.include?( libdir )
       
    10 }
       
    11 
       
    12 
       
    13 begin
       
    14 	require 'ostruct'
       
    15 	require 'spec/runner'
       
    16 	require 'spec/lib/helpers'
       
    17 	require 'ezmlm/listdaemon'
       
    18 rescue LoadError
       
    19 	unless Object.const_defined?( :Gem )
       
    20 		require 'rubygems'
       
    21 		retry
       
    22 	end
       
    23 	raise
       
    24 end
       
    25 
       
    26 
       
    27 describe Ezmlm::ListDaemon do
       
    28 	include Ezmlm::SpecHelpers
       
    29 
       
    30 
       
    31 	DEFAULT_ADDRESS = Ezmlm::ListDaemon::DEFAULT_ADDRESS
       
    32 	DEFAULT_PORT = Ezmlm::ListDaemon::DEFAULT_PORT
       
    33 
       
    34 
       
    35 	it "can return a struct that contains its default options" do
       
    36 		opts = Ezmlm::ListDaemon.default_options
       
    37 		
       
    38 		opts.should be_an_instance_of( OpenStruct )
       
    39 		opts.bind_addr.should == DEFAULT_ADDRESS
       
    40 		opts.bind_port.should == DEFAULT_PORT
       
    41 		opts.debugmode.should == false
       
    42 		opts.helpmode.should == false
       
    43 	end
       
    44 
       
    45 	describe "created with defaults" do
       
    46 
       
    47 		DEFAULT_URL = "druby://%s:%d" % [ DEFAULT_ADDRESS, DEFAULT_PORT ]
       
    48 
       
    49 		before( :each ) do
       
    50 			@test_list_dir = Pathname.new( 'lists' )
       
    51 			@daemon = Ezmlm::ListDaemon.new( @test_list_dir )
       
    52 		end
       
    53 		
       
    54 
       
    55 		it "can be started and will return a thread" do
       
    56 			mock_drb_thread = mock( "drb thread" )
       
    57 			
       
    58 			DRb.should_receive( :start_service ).with( DEFAULT_URL, @daemon.service )
       
    59 			DRb.should_receive( :thread ).and_return( mock_drb_thread )
       
    60 
       
    61 			@daemon.start.should == mock_drb_thread
       
    62 		end
       
    63 	end
       
    64 
       
    65 
       
    66 	describe "created with an options struct" do
       
    67 
       
    68 		TEST_ADDRESS = '0.0.0.0'
       
    69 		TEST_PORT = 17771
       
    70 		TEST_URL = "druby://%s:%d" % [ TEST_ADDRESS, TEST_PORT ]
       
    71 
       
    72 		before( :each ) do
       
    73 			@test_list_dir = Pathname.new( 'lists' )
       
    74 
       
    75 			@opts = Ezmlm::ListDaemon.default_options
       
    76 			@opts.bind_addr = TEST_ADDRESS
       
    77 			@opts.bind_port = TEST_PORT
       
    78 
       
    79 			@daemon = Ezmlm::ListDaemon.new( @test_list_dir, @opts )
       
    80 		end
       
    81 		
       
    82 
       
    83 		it "can be started and will return a thread" do
       
    84 			mock_drb_thread = mock( "drb thread" )
       
    85 			
       
    86 			DRb.should_receive( :start_service ).with( TEST_URL, @daemon.service )
       
    87 			DRb.should_receive( :thread ).and_return( mock_drb_thread )
       
    88 
       
    89 			@daemon.start.should == mock_drb_thread
       
    90 		end
       
    91 	end
       
    92 
       
    93 end
       
    94 
       
    95 
       
    96 describe Ezmlm::ListDaemon::Service do
       
    97 	
       
    98 	before( :each ) do
       
    99 		@dummydir = 'lists'
       
   100 		@service = Ezmlm::ListDaemon::Service.new( @dummydir )
       
   101 	end
       
   102 
       
   103 	
       
   104 	it "can return a list object by name if there is a corresponding listdir" do
       
   105 		@service.get_list( 'announce' ).should be_an_instance_of( Ezmlm::List )
       
   106 	end
       
   107 	
       
   108 	it "raises an exception when asked for a list whose name contains invalid characters" do
       
   109 		lambda {
       
   110 			@service.get_list( 'glarg beegun' )
       
   111 		}.should raise_error( ArgumentError )
       
   112 	end
       
   113 	
       
   114 	it "can iterate over listdirs, yielding each as a Ezmlm::List object" do
       
   115 		Ezmlm.should_receive( :each_list ).with( Pathname.new(@dummydir) ).and_yield( :a_list )
       
   116 		@service.each_list {|l| l.should == :a_list }
       
   117 	end
       
   118 	
       
   119 end
       
   120 
       
   121 # listservice = DRbObject.new( nil, 'druby://lists.laika.com:23431' )
       
   122 # announce = listservice.each_list do |list|
       
   123 # 	last_posts << list.last_post
       
   124 # end
       
   125 # announce = listservice.get_list( 'announce' )
       
   126 # 
       
   127 # announce.last_post
       
   128 # 
       
   129 #