OSDN Git Service

test/TC_league.rb failed with Ruby 1.8.7. This issue has been fixed.
[shogi-server/shogi-server.git] / test / TC_floodgate_thread.rb
1 $:.unshift File.join(File.dirname(__FILE__), "..")
2 require 'test/unit'
3 require 'ostruct'
4 $topdir = File.expand_path File.dirname(__FILE__)
5 require 'shogi_server'
6 require 'shogi_server/league/floodgate'
7 require 'shogi_server/league/floodgate_thread'
8 require 'test/mock_log_message'
9
10 class MySetupFloodgate < ShogiServer::SetupFloodgate
11   def initialize(game_names)
12     super
13     @is_reload_shogi_server = false
14     @is_start_games = false
15   end
16   attr_reader :is_reload_shogi_server, :is_start_games
17
18   def reload_shogi_server
19     @is_reload_shogi_server = true
20   end
21
22   def start_games(floodgate)
23     @is_start_games = floodgate
24   end
25 end
26
27 class TestSetupFloodgate < Test::Unit::TestCase
28   def setup
29     game_names = %w(floodgate-900-0 floodgate-3600-0)
30     @sf = MySetupFloodgate.new game_names
31   end
32
33   def test_initialize_empty
34     sf = ShogiServer::SetupFloodgate.new []
35     thread = sf.start
36     assert_nil thread
37   end
38
39   def test_mk_leagues
40     leagues = @sf.mk_leagues
41     assert_equal 2, leagues.size
42     assert_equal "floodgate-900-0",  leagues[0].game_name
43     assert_equal "floodgate-3600-0", leagues[1].game_name
44   end
45
46   def test_next_league
47     fa = OpenStruct.new
48     now = Time.now
49     fa.next_time = now
50     fb = OpenStruct.new
51     fb.next_time = now + 1
52     assert_equal fa.next_time, @sf.next_league([fa]).next_time
53     assert_equal fa.next_time, @sf.next_league([fa,fb]).next_time
54     assert_equal fa.next_time, @sf.next_league([fb,fa]).next_time
55   end
56
57   def test_wait_next_floodgate
58     f = OpenStruct.new
59     f.next_time = Time.now + 1;
60     assert @sf.wait_next_floodgate f
61     f.next_time = Time.now - 1;
62     assert(!@sf.wait_next_floodgate(f))
63   end
64
65   def test_regenerate_leagues
66     game_names = %w(floodgate-900-0 floodgate-3600-0)
67     now = Time.now
68     next_array = [["floodgate-900-0", now+100], ["floodgate-3600-0", now+200]]
69     objs = @sf.regenerate_leagues(next_array)
70     assert_equal 2, objs.size
71     assert_instance_of ShogiServer::League::Floodgate, objs[0]
72     assert_instance_of ShogiServer::League::Floodgate, objs[1]
73   end
74
75   def test_start
76     def @sf.mk_leagues
77       ret = []
78       now = Time.now
79       ret << ShogiServer::League::Floodgate.new($league, 
80                                                 {:game_name => "floodgate-900-0",
81                                                  :next_time => (now-100)})
82       ret << ShogiServer::League::Floodgate.new($league, 
83                                                 {:game_name => "floodgate-3600-0",
84                                                  :next_time => (now-200)})
85       ret
86     end
87     thread = @sf.start
88     sleep 1
89     assert_instance_of Thread, thread
90     assert_equal("floodgate-3600-0", @sf.is_start_games.game_name)
91   end
92 end