OSDN Git Service

[shogi-server] Enhance capability of Floodgate configuration file
[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 = []
69     next_array << ShogiServer::League::Floodgate.new($league, 
70                     {:game_name => "floodgate-900-0",
71                      :next_time => (now+100)})
72     next_array << ShogiServer::League::Floodgate.new($league, 
73                     {:game_name => "floodgate-3600-0",
74                      :next_time => (now+200)})
75     objs = @sf.regenerate_leagues(next_array)
76     assert_equal 2, objs.size
77     assert_instance_of ShogiServer::League::Floodgate, objs[0]
78     assert_instance_of ShogiServer::League::Floodgate, objs[1]
79   end
80
81   def test_start
82     def @sf.mk_leagues
83       ret = []
84       now = Time.now
85       ret << ShogiServer::League::Floodgate.new($league, 
86                                                 {:game_name => "floodgate-900-0",
87                                                  :next_time => (now-100)})
88       ret << ShogiServer::League::Floodgate.new($league, 
89                                                 {:game_name => "floodgate-3600-0",
90                                                  :next_time => (now-200)})
91       ret
92     end
93     thread = @sf.start
94     sleep 1
95     assert_instance_of Thread, thread
96     assert_equal("floodgate-3600-0", @sf.is_start_games.game_name)
97   end
98 end