drbqs 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,39 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/connection.rb'
4
+
5
+ describe DRbQS::ConnectionClient do
6
+ before(:all) do
7
+ @message = Rinda::TupleSpace.new
8
+ @connection = DRbQS::ConnectionClient.new(@message)
9
+ @node_id = 23
10
+ id_string = @connection.instance_variable_get(:@id_string)
11
+ @message.write([id_string, @node_id])
12
+ end
13
+
14
+ it "should get node ID." do
15
+ @connection.get_id.should == @node_id
16
+ @message.take([:connect, nil]).should be_true
17
+ end
18
+
19
+ it "should get no initialization method." do
20
+ @connection.get_initialization.should be_nil
21
+ end
22
+
23
+ it "should get initialization" do
24
+ ary = [:initialize, [1, 2], :size, []]
25
+ @message.write(ary)
26
+ @connection.get_initialization.should == ary[1..-1]
27
+ end
28
+
29
+ it "should respond :alive_p signal" do
30
+ @message.write([@node_id, :alive_p])
31
+ @connection.respond_alive_signal
32
+ @message.take([:alive, nil]).should be_true
33
+ end
34
+
35
+ it "should respond :exit signal" do
36
+ @message.write([@node_id, :exit])
37
+ @connection.respond_alive_signal.should == :exit
38
+ end
39
+ end
@@ -0,0 +1,50 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require_relative 'test/test1.rb'
4
+
5
+ describe DRbQS do
6
+ before(:all) do
7
+ @tasks = []
8
+ 5.times do |i|
9
+ @tasks << DRbQS::Task.new(Test1.new, :echo, [i])
10
+ end
11
+ @process_id = fork do
12
+ server = DRbQS::Server.new(:port => 13501)
13
+
14
+ @tasks.each do |task|
15
+ server.queue.add(task)
16
+ end
17
+
18
+ server.set_finish_hook do |serv|
19
+ serv.exit
20
+ end
21
+
22
+ server.set_signal_trap
23
+ server.start
24
+ server.wait
25
+ end
26
+ sleep(1)
27
+
28
+ @uri = 'druby://:13501'
29
+
30
+ @manage = DRbQS::Manage.new(@uri)
31
+ end
32
+
33
+ it "should send exit signal" do
34
+ lambda do
35
+ @manage.send_exit_signal
36
+ end.should_not raise_error
37
+ lambda do
38
+ i = 0
39
+ while !Process.waitpid(@process_id, Process::WNOHANG)
40
+ i += 1
41
+ if i > 10
42
+ Process.kill(:KILL, @process_id)
43
+ raise "Server process does not finish."
44
+ end
45
+ sleep(1)
46
+ end
47
+ end.should_not raise_error
48
+ end
49
+
50
+ end
@@ -0,0 +1,71 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/message.rb'
4
+
5
+ describe DRbQS::MessageServer do
6
+ before(:all) do
7
+ @message = Rinda::TupleSpace.new
8
+ @message_server = DRbQS::MessageServer.new(@message)
9
+ @node_id_list = []
10
+ end
11
+
12
+ it "should return nil" do
13
+ @message_server.get_message.should be_nil
14
+ end
15
+
16
+ it "should have no node" do
17
+ @message_server.node_not_exist?.should be_true
18
+ end
19
+
20
+ it "should set initialization task" do
21
+ lambda do
22
+ @message.take([:initialize, nil, Symbol, nil], 0)
23
+ end.should raise_error Rinda::RequestExpiredError
24
+ @message_server.set_initialization(DRbQS::Task.new(Object.new, :object_id))
25
+ @message.take([:initialize, nil, Symbol, nil], 0).should be_true
26
+ end
27
+
28
+ it "should get :connect message" do
29
+ 5.times do |i|
30
+ id_str = "connect_test_#{i}"
31
+ @message.write([:connect, id_str])
32
+ @message_server.get_message
33
+ (ary = @message.take([id_str, Fixnum])).should be_true
34
+ @node_id_list << ary[1]
35
+ @message_server.node_not_exist?.should be_false
36
+ end
37
+ end
38
+
39
+ it "should get :alive message" do
40
+ node_id = 73
41
+ @message.write([:alive, node_id])
42
+ node_list = @message_server.instance_variable_get(:@node_list)
43
+ node_list.should_receive(:set_alive).with(node_id)
44
+ @message_server.get_message
45
+ end
46
+
47
+ it "should get :exit_server message" do
48
+ @message.write([:exit_server, 'message_test'])
49
+ @message_server.get_message.should == :exit_server
50
+ end
51
+
52
+ it "should send exit message" do
53
+ @message_server.send_exit
54
+ @node_id_list.each do |id|
55
+ @message.take([id, :exit]).should be_true
56
+ end
57
+ end
58
+
59
+ it "should delete a node" do
60
+ @message_server.check_connection.should == []
61
+ @node_id_list.each do |id|
62
+ @message.take([id, :alive_p]).should be_true
63
+ end
64
+ @message_server.check_connection.should == @node_id_list
65
+ @node_id_list.each do |id|
66
+ lambda do
67
+ @message.take([id, :alive_p], 0)
68
+ end.should raise_error Rinda::RequestExpiredError
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,54 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/node_list'
4
+
5
+ describe DRbQS::NodeList do
6
+ before(:all) do
7
+ @node_list = DRbQS::NodeList.new
8
+ @id_strings = 10.times.map { |i| sprintf("%05d", i) }
9
+ @id_list = []
10
+ end
11
+
12
+ it "should be empty." do
13
+ @node_list.empty?.should be_true
14
+ end
15
+
16
+ it "should get ids that are not duplicated." do
17
+ @id_strings.each do |str|
18
+ @id_list << @node_list.get_new_id(str)
19
+ end
20
+ @id_list.uniq!
21
+ @id_list.all? { |i| Integer === i }.should be_true
22
+ @id_list.size.should == @id_strings.size
23
+ @node_list.each do |id_num, id_str|
24
+ @id_strings.include?(id_str).should be_true
25
+ @id_list.include?(id_num).should be_true
26
+ end
27
+ end
28
+
29
+ it "should delete all ids" do
30
+ @node_list.empty?.should_not be_true
31
+ @node_list.set_check_connection
32
+ ids = @node_list.delete_not_alive
33
+ ids.sort.should == @id_list.sort
34
+ @node_list.empty?.should be_true
35
+ @id_list.clear
36
+ end
37
+
38
+ it "should set alive flag" do
39
+ alive_id_num = [3, 4, 5]
40
+ @id_strings.each do |str|
41
+ @id_list << @node_list.get_new_id(str)
42
+ end
43
+ @node_list.set_check_connection
44
+ alive_id_num.each do |i|
45
+ @node_list.set_alive(@id_list[i])
46
+ end
47
+ @node_list.delete_not_alive
48
+ alive_ids = alive_id_num.map { |i| @id_list[i] }
49
+ @node_list.each do |id_num, id_str|
50
+ alive_ids.include?(id_num).should be_true
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,59 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/queue'
4
+
5
+ describe DRbQS::QueueServer do
6
+ before(:all) do
7
+ @ts = {
8
+ :queue => Rinda::TupleSpace.new,
9
+ :result => Rinda::TupleSpace.new
10
+ }
11
+ @queue_server = DRbQS::QueueServer.new(@ts[:queue], @ts[:result])
12
+ @task = { :obj => DRbQS::Task.new([1, 2, 3], :size, []), :id => nil }
13
+ @node_id = 100
14
+ end
15
+
16
+ it "should be empty" do
17
+ @queue_server.calculating_task_number.should == 0
18
+ @queue_server.empty?.should be_true
19
+ @queue_server.finished?.should be_true
20
+ end
21
+
22
+ it "should add a task" do
23
+ @task[:id] = @queue_server.add(@task[:obj])
24
+ @queue_server.calculating_task_number.should == 0
25
+ @queue_server.empty?.should be_false
26
+ @queue_server.finished?.should be_false
27
+ @ts[:queue].take([nil, nil, nil, nil]).should be_true
28
+ end
29
+
30
+ it "should get accept signal" do
31
+ @ts[:result].write([:accept, @task[:id], @node_id])
32
+ @queue_server.get_accept_signal.should == 1
33
+ @queue_server.calculating_task_number.should == 1
34
+ @queue_server.empty?.should be_true
35
+ @queue_server.finished?.should be_false
36
+ @ts[:result].read_all([nil, nil, nil]).size.should == 0
37
+ end
38
+
39
+ it "should get result" do
40
+ @ts[:result].write([:result, @task[:id], :result_object])
41
+ @queue_server.get_result
42
+ @queue_server.calculating_task_number.should == 0
43
+ @queue_server.empty?.should be_true
44
+ @queue_server.finished?.should be_true
45
+ end
46
+
47
+ it "should delete node" do
48
+ @task[:id] = @queue_server.add(@task[:obj])
49
+ @ts[:queue].take([nil, nil, nil, nil]).should be_true
50
+ @ts[:result].write([:accept, @task[:id], 100])
51
+ @queue_server.get_accept_signal.should == 1
52
+ @queue_server.requeue_for_deleted_node_id([@node_id])
53
+ @queue_server.calculating_task_number.should == 0
54
+ @queue_server.empty?.should be_false
55
+ @queue_server.finished?.should be_false
56
+ (ary = @ts[:queue].take([nil, nil, nil, nil])).should be_true
57
+ ary[0].should == @task[:id]
58
+ end
59
+ end
@@ -0,0 +1,34 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe DRbQS::ServerDefinition do
4
+ context "when we call class methods" do
5
+ before(:all) do
6
+ @server_definition = DRbQS.class_variable_get(:@@server_def)
7
+ end
8
+
9
+ it "should define server" do
10
+ @server_definition.should_receive(:define_server)
11
+ DRbQS.define_server do |server, argv, opts|
12
+ server.set_finish_hook do |serv|
13
+ serv.exit
14
+ end
15
+ end
16
+ end
17
+
18
+ it "should set parser of options" do
19
+ @server_definition.should_receive(:option_parser)
20
+ DRbQS.option_parser do |opt, hash|
21
+ opt.on('--test') do |v|
22
+ hash[:test] = true
23
+ end
24
+ end
25
+ end
26
+
27
+ it "should parse options" do
28
+ @server_definition.should_receive(:parse_option)
29
+ DRbQS.parse_option(['--test'])
30
+ end
31
+
32
+ end
33
+
34
+ end
@@ -0,0 +1,53 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/task_client'
4
+
5
+ describe DRbQS::TaskClient do
6
+ before(:all) do
7
+ @node_id = 4
8
+ @ts_queue = Rinda::TupleSpace.new
9
+ @ts_result = Rinda::TupleSpace.new
10
+ @task_client = DRbQS::TaskClient.new(@node_id, @ts_queue, @ts_result)
11
+ @task_example = [[1, 2, 3], :size, []]
12
+ @task_id = 10
13
+ end
14
+
15
+ it "should be empty" do
16
+ @task_client.task_empty?.should be_true
17
+ @task_client.result_empty?.should be_true
18
+ @task_client.calculating_task.should be_nil
19
+ end
20
+
21
+ it "should queue a task" do
22
+ @ts_queue.write([@task_id] + @task_example)
23
+ @task_client.add_new_task
24
+ @task_client.task_empty?.should_not be_true
25
+ @task_client.result_empty?.should be_true
26
+ @task_client.calculating_task.should == @task_id
27
+ @ts_queue.read_all([nil, nil, nil, nil]).size.should == 0
28
+ @ts_result.take([:accept, nil, nil], 0).should be_true
29
+ end
30
+
31
+ it "should dequeue a task" do
32
+ ary = @task_client.dequeue_task
33
+ ary.should == @task_example
34
+ @task_client.task_empty?.should be_true
35
+ @task_client.result_empty?.should be_true
36
+ @task_client.calculating_task.should == @task_id
37
+ end
38
+
39
+ it "should queue result" do
40
+ @task_client.queue_result(@task_example[0].__send__(@task_example[1], *@task_example[2]))
41
+ @task_client.task_empty?.should be_true
42
+ @task_client.result_empty?.should_not be_true
43
+ @task_client.calculating_task.should == @task_id
44
+ end
45
+
46
+ it "should send result" do
47
+ @task_client.send_result
48
+ @task_client.task_empty?.should be_true
49
+ @task_client.result_empty?.should be_true
50
+ @task_client.calculating_task.should be_nil
51
+ @ts_result.take([:result, nil, nil], 0).should be_true
52
+ end
53
+ end
@@ -0,0 +1,32 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require 'drbqs/task_generator'
4
+
5
+ describe DRbQS::TaskGenerator do
6
+ def check_task_ary(tasks, num)
7
+ tasks.should have(num).items
8
+ tasks.all? { |t| DRbQS::Task === t }.should be_true
9
+ end
10
+
11
+ subject { DRbQS::TaskGenerator.new(:abc => 'ABC', :def => 123, :data => [1, 2, 3]) }
12
+
13
+ it "should initialize instance varibles" do
14
+ subject.instance_variable_get('@abc').should == 'ABC'
15
+ subject.instance_variable_get('@def').should == 123
16
+ end
17
+
18
+ it "should create new tasks" do
19
+ subject.set(2) do
20
+ @data.each do |i|
21
+ create_add_task(i, :to_s)
22
+ end
23
+ end
24
+ check_task_ary(subject.new_tasks, 2)
25
+ check_task_ary(subject.new_tasks, 1)
26
+ subject.new_tasks.should be_nil
27
+ end
28
+
29
+ it "should debug generator" do
30
+ subject.debug_all_tasks.should be_true
31
+ end
32
+ end
data/spec/task_spec.rb ADDED
@@ -0,0 +1,21 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe DRbQS::Task do
4
+ it "should not set hook" do
5
+ task = DRbQS::Task.new([1, 2, 3], :size)
6
+ task.hook.should be_nil
7
+ end
8
+
9
+ it "should set hook" do
10
+ task = DRbQS::Task.new([1, 2, 3], :size) do |server, ret|
11
+ p ret
12
+ end
13
+ task.hook.should be_an_instance_of Proc
14
+ end
15
+
16
+ it "should have same targets" do
17
+ task1 = DRbQS::Task.new([1, 2, 3], :concat, [3, 4, 5])
18
+ task2 = DRbQS::Task.new([1, 2, 3], :concat, [3, 4, 5])
19
+ task1.same_target?(task2).should be_true
20
+ end
21
+ end
@@ -0,0 +1,13 @@
1
+ class Test1
2
+ @@execute_echo_number = 0
3
+
4
+ def echo(*args)
5
+ puts "execute Test1#echo(*#{args.inspect.strip})"
6
+ @@execute_echo_number += 1
7
+ args
8
+ end
9
+
10
+ def self.get_execute_echo_number
11
+ @@execute_echo_number
12
+ end
13
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require_relative '../lib/drbqs.rb'
4
+ require_relative 'test/test1.rb'
5
+
6
+ describe DRbQS do
7
+ before(:all) do
8
+ @tasks = []
9
+ 5.times do |i|
10
+ @tasks << DRbQS::Task.new(Test1.new, :echo, [i])
11
+ end
12
+ @process_id = fork do
13
+ server = DRbQS::Server.new(:port => 13501)
14
+
15
+ @tasks.each do |task|
16
+ server.queue.add(task)
17
+ end
18
+
19
+ server.set_finish_hook do |serv|
20
+ serv.exit
21
+ end
22
+
23
+ server.set_signal_trap
24
+ server.start
25
+ server.wait
26
+ end
27
+ sleep(1)
28
+
29
+ @uri = 'druby://:13501'
30
+ @client = DRbQS::Client.new(@uri, :log_file => $stdout, :continue => true)
31
+ end
32
+
33
+ it "should have nil instance variables" do
34
+ @client.instance_variable_get(:@task_client).should be_nil
35
+ @client.instance_variable_get(:@connection).should be_nil
36
+ @client.connect
37
+ end
38
+
39
+ it "should initialize @task_client" do
40
+ task_client = @client.instance_variable_get(:@task_client)
41
+ task_client.should be_an_instance_of DRbQS::TaskClient
42
+ task_client.node_id.should be_an_instance_of Fixnum
43
+ task_client.task_empty?.should be_true
44
+ task_client.result_empty?.should be_true
45
+ end
46
+
47
+ it "should initialize @connection" do
48
+ connection = @client.instance_eval { @connection }
49
+ connection.should be_an_instance_of DRbQS::ConnectionClient
50
+ connection.instance_variable_get(:@id_number).should be_an_instance_of Fixnum
51
+ connection.instance_variable_get(:@id_string).should be_an_instance_of String
52
+ end
53
+
54
+ it "should calculate" do
55
+ task_client = @client.instance_eval { @task_client }
56
+ # *** Too late ***
57
+ # task_client.should_receive(:add_new_task).at_least(:once)
58
+ # task_client.should_receive(:transit).exactly(5).times
59
+ # task_client.should_receive(:send_result).exactly(5).times
60
+ lambda do
61
+ @client.calculate
62
+ end.should_not raise_error
63
+ Test1.get_execute_echo_number.should == @tasks.size
64
+ end
65
+
66
+ after(:all) do
67
+ lambda do
68
+ i = 0
69
+ while !Process.waitpid(@process_id, Process::WNOHANG)
70
+ i += 1
71
+ if i > 10
72
+ Process.kill(:KILL, @process_id)
73
+ raise "Server process does not finish."
74
+ end
75
+ sleep(1)
76
+ end
77
+ end.should_not raise_error
78
+ end
79
+
80
+ end
@@ -0,0 +1,75 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ require_relative '../lib/drbqs.rb'
4
+ require_relative 'test/test1.rb'
5
+
6
+ describe DRbQS do
7
+ before(:all) do
8
+ @tasks = []
9
+ @task_generator = DRbQS::TaskGenerator.new(:iterate => 3)
10
+ @task_generator.set do
11
+ @iterate.times do |i|
12
+ create_add_task(Test1.new, :echo, [i])
13
+ end
14
+ end
15
+ @process_id = fork do
16
+ server = DRbQS::Server.new(:port => 13501)
17
+
18
+ server.set_task_generator(@task_generator)
19
+
20
+ server.set_finish_hook do |serv|
21
+ serv.exit
22
+ end
23
+
24
+ server.set_signal_trap
25
+ server.start
26
+ server.wait
27
+ end
28
+ sleep(1)
29
+
30
+ @uri = 'druby://:13501'
31
+ @client = DRbQS::Client.new(@uri, :log_file => $stdout, :continue => true)
32
+ end
33
+
34
+ it "should have nil instance variables" do
35
+ @client.instance_variable_get(:@task_client).should be_nil
36
+ @client.instance_variable_get(:@connection).should be_nil
37
+ @client.connect
38
+ end
39
+
40
+ it "should initialize @task_client" do
41
+ task_client = @client.instance_variable_get(:@task_client)
42
+ task_client.should be_an_instance_of DRbQS::TaskClient
43
+ task_client.node_id.should be_an_instance_of Fixnum
44
+ task_client.task_empty?.should be_true
45
+ task_client.result_empty?.should be_true
46
+ end
47
+
48
+ it "should initialize @connection" do
49
+ connection = @client.instance_eval { @connection }
50
+ connection.should be_an_instance_of DRbQS::ConnectionClient
51
+ connection.instance_variable_get(:@id_number).should be_an_instance_of Fixnum
52
+ connection.instance_variable_get(:@id_string).should be_an_instance_of String
53
+ end
54
+
55
+ it "should calculate" do
56
+ lambda do
57
+ @client.calculate
58
+ end.should_not raise_error
59
+ end
60
+
61
+ after(:all) do
62
+ lambda do
63
+ i = 0
64
+ while !Process.waitpid(@process_id, Process::WNOHANG)
65
+ i += 1
66
+ if i > 10
67
+ Process.kill(:KILL, @process_id)
68
+ raise "Server process does not finish."
69
+ end
70
+ sleep(1)
71
+ end
72
+ end.should_not raise_error
73
+ end
74
+
75
+ end