Description:
added rspec test for grader_message
git-svn-id: http://theory.cpe.ku.ac.th/grader/web/trunk@387 6386c4cd-e34a-4fa8-8920-d93eb39b512e
Commit status:
[Not Reviewed]
References:
Diff options:
Comments:
0 Commit comments
0 Inline Comments
Unresolved TODOs:
There are no unresolved TODOs
r184:47909a48a10e - - 4 files changed: 131 inserted, 7 deleted
@@ -0,0 +1,119 | |||||
|
|
1 | + | ||
|
|
2 | + require File.dirname(__FILE__) + '/../spec_helper' | ||
|
|
3 | + | ||
|
|
4 | + describe GraderMessage do | ||
|
|
5 | + | ||
|
|
6 | + def add_submission_with_id(id) | ||
|
|
7 | + submission = stub(Submission, :id => id) | ||
|
|
8 | + GraderMessage.create_grade_submission("exam",submission) | ||
|
|
9 | + end | ||
|
|
10 | + | ||
|
|
11 | + before(:each) do | ||
|
|
12 | + GraderMessage.destroy_all | ||
|
|
13 | + end | ||
|
|
14 | + | ||
|
|
15 | + it "should return nil when there is no messages to me" do | ||
|
|
16 | + GraderMessage.create_message(1,0) | ||
|
|
17 | + GraderMessage.get_message_for(2).should == nil | ||
|
|
18 | + end | ||
|
|
19 | + | ||
|
|
20 | + it "should return a messages directed to me" do | ||
|
|
21 | + GraderMessage.create_message(1,0) | ||
|
|
22 | + GraderMessage.get_message_for(2).should == nil | ||
|
|
23 | + end | ||
|
|
24 | + | ||
|
|
25 | + it "should return a messages directed to me, in order of creation" do | ||
|
|
26 | + msg1 = GraderMessage.create_message(1,0) | ||
|
|
27 | + msg2 = GraderMessage.create_message(2,2) | ||
|
|
28 | + msg3 = GraderMessage.create_message(1,2) | ||
|
|
29 | + GraderMessage.get_message_for(1).id.should == msg1.id | ||
|
|
30 | + GraderMessage.get_message_for(1).id.should == msg3.id | ||
|
|
31 | + end | ||
|
|
32 | + | ||
|
|
33 | + it "should not return a messages directed to me if the command is not on my list of accepting commands" do | ||
|
|
34 | + msg1 = GraderMessage.create_message(1,GraderMessage::GRADE_SUBMISSION) | ||
|
|
35 | + msg2 = GraderMessage.create_message(1,GraderMessage::STOP) | ||
|
|
36 | + GraderMessage.get_message_for(1,[GraderMessage::GRADE_TEST_REQUEST]).should == nil | ||
|
|
37 | + end | ||
|
|
38 | + | ||
|
|
39 | + it "should return a messages directed to me if the command is on my list of accepting commands" do | ||
|
|
40 | + msg1 = GraderMessage.create_message(1,0) | ||
|
|
41 | + msg2 = GraderMessage.create_message(1,2) | ||
|
|
42 | + msg3 = GraderMessage.create_message(2,2) | ||
|
|
43 | + GraderMessage.get_message_for(1,[2]).id.should == msg2.id | ||
|
|
44 | + GraderMessage.get_message_for(1,[2]).should == nil | ||
|
|
45 | + end | ||
|
|
46 | + | ||
|
|
47 | + it "should return a message directed to anyone when I'm requesting" do | ||
|
|
48 | + msg1 = GraderMessage.create_message(:any,0) | ||
|
|
49 | + GraderMessage.get_message_for(1).id.should == msg1.id | ||
|
|
50 | + end | ||
|
|
51 | + | ||
|
|
52 | + it "should return a messages directed to anyone only if the command is on my list of accepting commands" do | ||
|
|
53 | + msg1 = GraderMessage.create_message(:any,0) | ||
|
|
54 | + GraderMessage.get_message_for(1,[1]).should == nil | ||
|
|
55 | + msg2 = GraderMessage.create_message(:any,1) | ||
|
|
56 | + GraderMessage.get_message_for(1,[1]).id.should == msg2.id | ||
|
|
57 | + end | ||
|
|
58 | + | ||
|
|
59 | + it "should return messages directed to anyone to many graders in order of requests" do | ||
|
|
60 | + msg1 = GraderMessage.create_message(:any,0) | ||
|
|
61 | + msg2 = GraderMessage.create_message(:any,2) | ||
|
|
62 | + msg3 = GraderMessage.create_message(:any,2) | ||
|
|
63 | + GraderMessage.get_message_for(1).id.should == msg1.id | ||
|
|
64 | + GraderMessage.get_message_for(2).id.should == msg2.id | ||
|
|
65 | + GraderMessage.get_message_for(1).id.should == msg3.id | ||
|
|
66 | + end | ||
|
|
67 | + | ||
|
|
68 | + it "should return messages directed to anyone to graders accepting those commands in order of requests" do | ||
|
|
69 | + msg1 = GraderMessage.create_message(:any,0) | ||
|
|
70 | + msg2 = GraderMessage.create_message(:any,1) | ||
|
|
71 | + msg3 = GraderMessage.create_message(:any,2) | ||
|
|
72 | + msg4 = GraderMessage.create_message(:any,2) | ||
|
|
73 | + msg5 = GraderMessage.create_message(:any,3) | ||
|
|
74 | + GraderMessage.get_message_for(1).id.should == msg1.id | ||
|
|
75 | + GraderMessage.get_message_for(2,[2]).id.should == msg3.id | ||
|
|
76 | + GraderMessage.get_message_for(1,[3]).id.should == msg5.id | ||
|
|
77 | + GraderMessage.get_message_for(2).id.should == msg2.id | ||
|
|
78 | + GraderMessage.get_message_for(1).id.should == msg4.id | ||
|
|
79 | + end | ||
|
|
80 | + | ||
|
|
81 | + it "should get all messages dispatched when there are many concurrent processes" do | ||
|
|
82 | + n = 100 | ||
|
|
83 | + msg = [] | ||
|
|
84 | + n.times do |i| | ||
|
|
85 | + msg << GraderMessage.create_message(:any,i) | ||
|
|
86 | + end | ||
|
|
87 | + | ||
|
|
88 | + #puts "#{n} messages created" | ||
|
|
89 | + | ||
|
|
90 | + t = 10 # use 10 threads | ||
|
|
91 | + ths = [] | ||
|
|
92 | + t.times do |i| | ||
|
|
93 | + fork do | ||
|
|
94 | + #puts "I'm the #{i+1}-th process." | ||
|
|
95 | + begin | ||
|
|
96 | + m = GraderMessage.get_message_for(i) | ||
|
|
97 | + #puts "#{i+1} got #{m.id}" if m | ||
|
|
98 | + sleep 0.1 | ||
|
|
99 | + end while m!=nil | ||
|
|
100 | + #puts "The #{i+1}-th process terminated." | ||
|
|
101 | + exit 0 | ||
|
|
102 | + end | ||
|
|
103 | + end | ||
|
|
104 | + | ||
|
|
105 | + t.times do | ||
|
|
106 | + Process.wait | ||
|
|
107 | + end | ||
|
|
108 | + | ||
|
|
109 | + # for some reason the connection is lost at this point. | ||
|
|
110 | + GraderMessage.connection.reconnect! | ||
|
|
111 | + | ||
|
|
112 | + # check that all messages have been processed | ||
|
|
113 | + GraderMessage.find(:all) do |msg| | ||
|
|
114 | + msg.taken_grader_process.should != nil | ||
|
|
115 | + end | ||
|
|
116 | + | ||
|
|
117 | + end | ||
|
|
118 | + | ||
|
|
119 | + end |
@@ -1,15 +1,17 | |||||
|
1 | class GraderMessage < ActiveRecord::Base |
|
1 | class GraderMessage < ActiveRecord::Base |
|
2 |
|
2 | ||
|
|
3 | + belongs_to :taken_grader_process, :class_name => :grader_process | ||
|
|
4 | + | ||
|
3 | GRADE_SUBMISSION = 1 |
|
5 | GRADE_SUBMISSION = 1 |
|
4 | GRADE_TEST_REQUEST = 2 |
|
6 | GRADE_TEST_REQUEST = 2 |
|
5 | STOP = 3 |
|
7 | STOP = 3 |
|
6 |
|
8 | ||
|
|
9 | + RECIPIENT_ANY = -1 | ||
|
|
10 | + | ||
|
7 | def self.create_message(recipient, command, options=nil, target_id=nil) |
|
11 | def self.create_message(recipient, command, options=nil, target_id=nil) |
|
8 | recipient_id = recipient |
|
12 | recipient_id = recipient |
|
9 |
- if recipient == :a |
|
13 | + if recipient == :any |
|
10 | - recipient_id = -1 |
|
14 | + recipient_id = GraderMessage::RECIPIENT_ANY |
|
11 | - elsif recipient == :any |
|
||
|
12 | - recipient_id = 0 |
|
||
|
13 | end |
|
15 | end |
|
14 |
|
16 | ||
|
15 | GraderMessage.create(:grader_process_id => recipient_id, |
|
17 | GraderMessage.create(:grader_process_id => recipient_id, |
@@ -42,7 +44,7 | |||||
|
42 | command_conditions = |
|
44 | command_conditions = |
|
43 | GraderMessage.build_command_conditions(accepting_commands) |
|
45 | GraderMessage.build_command_conditions(accepting_commands) |
|
44 | recp_conditions= "((`grader_process_id` = #{recipient_id.to_i})" + |
|
46 | recp_conditions= "((`grader_process_id` = #{recipient_id.to_i})" + |
|
45 |
- " OR (`grader_process_id` = |
|
47 | + " OR (`grader_process_id` = #{GraderMessage::RECIPIENT_ANY}))" |
|
46 |
|
48 | ||
|
47 | message = nil # need this to bind message in do-block for transaction |
|
49 | message = nil # need this to bind message in do-block for transaction |
|
48 | begin |
|
50 | begin |
@@ -50,12 +52,13 | |||||
|
50 | message = GraderMessage.find(:first, |
|
52 | message = GraderMessage.find(:first, |
|
51 | :order => "created_at", |
|
53 | :order => "created_at", |
|
52 | :conditions => |
|
54 | :conditions => |
|
53 | - "(`taken`=0)" + |
|
55 | + "(`taken` = 0)" + |
|
54 | - "AND (#{recp_conditions})" + |
|
56 | + " AND (#{recp_conditions})" + |
|
55 | " AND (#{command_conditions})", |
|
57 | " AND (#{command_conditions})", |
|
56 | :lock => true) |
|
58 | :lock => true) |
|
57 | if message!=nil |
|
59 | if message!=nil |
|
58 | message.taken = true |
|
60 | message.taken = true |
|
|
61 | + message.taken_grader_process_id = recipient_id | ||
|
59 | message.save! |
|
62 | message.save! |
|
60 | end |
|
63 | end |
|
61 | end |
|
64 | end |
@@ -6,6 +6,7 | |||||
|
6 | t.string :options |
|
6 | t.string :options |
|
7 | t.integer :target_id |
|
7 | t.integer :target_id |
|
8 | t.boolean :taken |
|
8 | t.boolean :taken |
|
|
9 | + t.integer :taken_grader_process_id | ||
|
9 | t.timestamps |
|
10 | t.timestamps |
|
10 | end |
|
11 | end |
|
11 | end |
|
12 | end |
@@ -48,6 +48,7 | |||||
|
48 | t.string "options" |
|
48 | t.string "options" |
|
49 | t.integer "target_id" |
|
49 | t.integer "target_id" |
|
50 | t.boolean "taken" |
|
50 | t.boolean "taken" |
|
|
51 | + t.integer "taken_grader_process_id" | ||
|
51 | t.datetime "created_at" |
|
52 | t.datetime "created_at" |
|
52 | t.datetime "updated_at" |
|
53 | t.datetime "updated_at" |
|
53 | end |
|
54 | end |
You need to be logged in to leave comments.
Login now