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: 129 inserted, 5 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,85 +1,88 | |||
|
1 | 1 | class GraderMessage < ActiveRecord::Base |
|
2 | 2 | |
|
3 | + belongs_to :taken_grader_process, :class_name => :grader_process | |
|
4 | + | |
|
3 | 5 | GRADE_SUBMISSION = 1 |
|
4 | 6 | GRADE_TEST_REQUEST = 2 |
|
5 | 7 | STOP = 3 |
|
6 | 8 | |
|
9 | + RECIPIENT_ANY = -1 | |
|
10 | + | |
|
7 | 11 | def self.create_message(recipient, command, options=nil, target_id=nil) |
|
8 | 12 | recipient_id = recipient |
|
9 |
- if recipient == :a |
|
|
10 | - recipient_id = -1 | |
|
11 | - elsif recipient == :any | |
|
12 | - recipient_id = 0 | |
|
13 | + if recipient == :any | |
|
14 | + recipient_id = GraderMessage::RECIPIENT_ANY | |
|
13 | 15 | end |
|
14 | 16 | |
|
15 | 17 | GraderMessage.create(:grader_process_id => recipient_id, |
|
16 | 18 | :command => command, |
|
17 | 19 | :options => options, |
|
18 | 20 | :target_id => target_id, |
|
19 | 21 | :taken => false) |
|
20 | 22 | end |
|
21 | 23 | |
|
22 | 24 | def self.create_grade_submission(mode,submission) |
|
23 | 25 | GraderMessage.create_message(:any, |
|
24 | 26 | GraderMessage::GRADE_SUBMISSION, |
|
25 | 27 | mode, |
|
26 | 28 | submission.id) |
|
27 | 29 | end |
|
28 | 30 | |
|
29 | 31 | def self.create_grade_test_request(mode,test_request) |
|
30 | 32 | GraderMessage.create_message(:any, |
|
31 | 33 | GraderMessage::GRADE_TEST_REQUEST, |
|
32 | 34 | mode, |
|
33 | 35 | test_request.id) |
|
34 | 36 | end |
|
35 | 37 | |
|
36 | 38 | def self.create_stop(grader_process_id) |
|
37 | 39 | GraderMessage.create_message(grader_process_id, |
|
38 | 40 | GraderMessage::STOP) |
|
39 | 41 | end |
|
40 | 42 | |
|
41 | 43 | def self.get_message_for(recipient_id, accepting_commands=:all) |
|
42 | 44 | command_conditions = |
|
43 | 45 | GraderMessage.build_command_conditions(accepting_commands) |
|
44 | 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 | 49 | message = nil # need this to bind message in do-block for transaction |
|
48 | 50 | begin |
|
49 | 51 | GraderMessage.transaction do |
|
50 | 52 | message = GraderMessage.find(:first, |
|
51 | 53 | :order => "created_at", |
|
52 | 54 | :conditions => |
|
53 | 55 | "(`taken`=0)" + |
|
54 | 56 | "AND (#{recp_conditions})" + |
|
55 | 57 | " AND (#{command_conditions})", |
|
56 | 58 | :lock => true) |
|
57 | 59 | if message!=nil |
|
58 | 60 | message.taken = true |
|
61 | + message.taken_grader_process_id = recipient_id | |
|
59 | 62 | message.save! |
|
60 | 63 | end |
|
61 | 64 | end |
|
62 | 65 | |
|
63 | 66 | rescue |
|
64 | 67 | message = nil |
|
65 | 68 | |
|
66 | 69 | end |
|
67 | 70 | |
|
68 | 71 | message |
|
69 | 72 | end |
|
70 | 73 | |
|
71 | 74 | protected |
|
72 | 75 | |
|
73 | 76 | def self.build_command_conditions(accepting_commands) |
|
74 | 77 | if accepting_commands==:all |
|
75 | 78 | return '(1=1)' |
|
76 | 79 | else |
|
77 | 80 | conds = [] |
|
78 | 81 | accepting_commands.each do |command| |
|
79 | 82 | conds << "(`command` = #{command.to_i})" |
|
80 | 83 | end |
|
81 | 84 | return "(" + conds.join(" OR ") + ")" |
|
82 | 85 | end |
|
83 | 86 | end |
|
84 | 87 | |
|
85 | 88 | end |
@@ -1,16 +1,17 | |||
|
1 | 1 | class CreateGraderMessages < ActiveRecord::Migration |
|
2 | 2 | def self.up |
|
3 | 3 | create_table :grader_messages do |t| |
|
4 | 4 | t.integer :grader_process_id |
|
5 | 5 | t.integer :command |
|
6 | 6 | t.string :options |
|
7 | 7 | t.integer :target_id |
|
8 | 8 | t.boolean :taken |
|
9 | + t.integer :taken_grader_process_id | |
|
9 | 10 | t.timestamps |
|
10 | 11 | end |
|
11 | 12 | end |
|
12 | 13 | |
|
13 | 14 | def self.down |
|
14 | 15 | drop_table :grader_messages |
|
15 | 16 | end |
|
16 | 17 | end |
@@ -1,205 +1,206 | |||
|
1 | 1 | # This file is auto-generated from the current state of the database. Instead of editing this file, |
|
2 | 2 | # please use the migrations feature of Active Record to incrementally modify your database, and |
|
3 | 3 | # then regenerate this schema definition. |
|
4 | 4 | # |
|
5 | 5 | # Note that this schema.rb definition is the authoritative source for your database schema. If you need |
|
6 | 6 | # to create the application database on another system, you should be using db:schema:load, not running |
|
7 | 7 | # all the migrations from scratch. The latter is a flawed and unsustainable approach (the more migrations |
|
8 | 8 | # you'll amass, the slower it'll run and the greater likelihood for issues). |
|
9 | 9 | # |
|
10 | 10 | # It's strongly recommended to check this file into your version control system. |
|
11 | 11 | |
|
12 | 12 | ActiveRecord::Schema.define(:version => 20090429014554) do |
|
13 | 13 | |
|
14 | 14 | create_table "announcements", :force => true do |t| |
|
15 | 15 | t.string "author" |
|
16 | 16 | t.text "body" |
|
17 | 17 | t.boolean "published" |
|
18 | 18 | t.datetime "created_at" |
|
19 | 19 | t.datetime "updated_at" |
|
20 | 20 | t.boolean "frontpage", :default => false |
|
21 | 21 | t.boolean "contest_only", :default => false |
|
22 | 22 | end |
|
23 | 23 | |
|
24 | 24 | create_table "configurations", :force => true do |t| |
|
25 | 25 | t.string "key" |
|
26 | 26 | t.string "value_type" |
|
27 | 27 | t.string "value" |
|
28 | 28 | t.datetime "created_at" |
|
29 | 29 | t.datetime "updated_at" |
|
30 | 30 | end |
|
31 | 31 | |
|
32 | 32 | create_table "countries", :force => true do |t| |
|
33 | 33 | t.string "name" |
|
34 | 34 | t.datetime "created_at" |
|
35 | 35 | t.datetime "updated_at" |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | create_table "descriptions", :force => true do |t| |
|
39 | 39 | t.text "body" |
|
40 | 40 | t.boolean "markdowned" |
|
41 | 41 | t.datetime "created_at" |
|
42 | 42 | t.datetime "updated_at" |
|
43 | 43 | end |
|
44 | 44 | |
|
45 | 45 | create_table "grader_messages", :force => true do |t| |
|
46 | 46 | t.integer "grader_process_id" |
|
47 | 47 | t.integer "command" |
|
48 | 48 | t.string "options" |
|
49 | 49 | t.integer "target_id" |
|
50 | 50 | t.boolean "taken" |
|
51 | + t.integer "taken_grader_process_id" | |
|
51 | 52 | t.datetime "created_at" |
|
52 | 53 | t.datetime "updated_at" |
|
53 | 54 | end |
|
54 | 55 | |
|
55 | 56 | create_table "grader_processes", :force => true do |t| |
|
56 | 57 | t.string "host", :limit => 20 |
|
57 | 58 | t.integer "pid" |
|
58 | 59 | t.string "mode" |
|
59 | 60 | t.boolean "active" |
|
60 | 61 | t.datetime "created_at" |
|
61 | 62 | t.datetime "updated_at" |
|
62 | 63 | t.integer "task_id" |
|
63 | 64 | t.string "task_type" |
|
64 | 65 | t.boolean "terminated" |
|
65 | 66 | end |
|
66 | 67 | |
|
67 | 68 | add_index "grader_processes", ["host", "pid"], :name => "index_grader_processes_on_ip_and_pid" |
|
68 | 69 | |
|
69 | 70 | create_table "languages", :force => true do |t| |
|
70 | 71 | t.string "name", :limit => 10 |
|
71 | 72 | t.string "pretty_name" |
|
72 | 73 | t.string "ext", :limit => 10 |
|
73 | 74 | t.string "common_ext" |
|
74 | 75 | end |
|
75 | 76 | |
|
76 | 77 | create_table "messages", :force => true do |t| |
|
77 | 78 | t.integer "sender_id" |
|
78 | 79 | t.integer "receiver_id" |
|
79 | 80 | t.integer "replying_message_id" |
|
80 | 81 | t.text "body" |
|
81 | 82 | t.boolean "replied" |
|
82 | 83 | t.datetime "created_at" |
|
83 | 84 | t.datetime "updated_at" |
|
84 | 85 | end |
|
85 | 86 | |
|
86 | 87 | create_table "problems", :force => true do |t| |
|
87 | 88 | t.string "name", :limit => 30 |
|
88 | 89 | t.string "full_name" |
|
89 | 90 | t.integer "full_score" |
|
90 | 91 | t.date "date_added" |
|
91 | 92 | t.boolean "available" |
|
92 | 93 | t.string "url" |
|
93 | 94 | t.integer "description_id" |
|
94 | 95 | t.boolean "test_allowed" |
|
95 | 96 | t.boolean "output_only" |
|
96 | 97 | end |
|
97 | 98 | |
|
98 | 99 | create_table "rights", :force => true do |t| |
|
99 | 100 | t.string "name" |
|
100 | 101 | t.string "controller" |
|
101 | 102 | t.string "action" |
|
102 | 103 | end |
|
103 | 104 | |
|
104 | 105 | create_table "rights_roles", :id => false, :force => true do |t| |
|
105 | 106 | t.integer "right_id" |
|
106 | 107 | t.integer "role_id" |
|
107 | 108 | end |
|
108 | 109 | |
|
109 | 110 | add_index "rights_roles", ["role_id"], :name => "index_rights_roles_on_role_id" |
|
110 | 111 | |
|
111 | 112 | create_table "roles", :force => true do |t| |
|
112 | 113 | t.string "name" |
|
113 | 114 | end |
|
114 | 115 | |
|
115 | 116 | create_table "roles_users", :id => false, :force => true do |t| |
|
116 | 117 | t.integer "role_id" |
|
117 | 118 | t.integer "user_id" |
|
118 | 119 | end |
|
119 | 120 | |
|
120 | 121 | add_index "roles_users", ["user_id"], :name => "index_roles_users_on_user_id" |
|
121 | 122 | |
|
122 | 123 | create_table "sessions", :force => true do |t| |
|
123 | 124 | t.string "session_id" |
|
124 | 125 | t.text "data" |
|
125 | 126 | t.datetime "updated_at" |
|
126 | 127 | end |
|
127 | 128 | |
|
128 | 129 | add_index "sessions", ["session_id"], :name => "index_sessions_on_session_id" |
|
129 | 130 | add_index "sessions", ["updated_at"], :name => "index_sessions_on_updated_at" |
|
130 | 131 | |
|
131 | 132 | create_table "sites", :force => true do |t| |
|
132 | 133 | t.string "name" |
|
133 | 134 | t.boolean "started" |
|
134 | 135 | t.datetime "start_time" |
|
135 | 136 | t.datetime "created_at" |
|
136 | 137 | t.datetime "updated_at" |
|
137 | 138 | t.integer "country_id" |
|
138 | 139 | t.string "password" |
|
139 | 140 | end |
|
140 | 141 | |
|
141 | 142 | create_table "submissions", :force => true do |t| |
|
142 | 143 | t.integer "user_id" |
|
143 | 144 | t.integer "problem_id" |
|
144 | 145 | t.integer "language_id" |
|
145 | 146 | t.text "source" |
|
146 | 147 | t.binary "binary" |
|
147 | 148 | t.datetime "submitted_at" |
|
148 | 149 | t.datetime "compiled_at" |
|
149 | 150 | t.text "compiler_message" |
|
150 | 151 | t.datetime "graded_at" |
|
151 | 152 | t.integer "points" |
|
152 | 153 | t.text "grader_comment" |
|
153 | 154 | t.integer "number" |
|
154 | 155 | t.string "source_filename" |
|
155 | 156 | end |
|
156 | 157 | |
|
157 | 158 | add_index "submissions", ["user_id", "problem_id", "number"], :name => "index_submissions_on_user_id_and_problem_id_and_number", :unique => true |
|
158 | 159 | add_index "submissions", ["user_id", "problem_id"], :name => "index_submissions_on_user_id_and_problem_id" |
|
159 | 160 | |
|
160 | 161 | create_table "tasks", :force => true do |t| |
|
161 | 162 | t.integer "submission_id" |
|
162 | 163 | t.datetime "created_at" |
|
163 | 164 | t.integer "status" |
|
164 | 165 | t.datetime "updated_at" |
|
165 | 166 | end |
|
166 | 167 | |
|
167 | 168 | create_table "test_requests", :force => true do |t| |
|
168 | 169 | t.integer "user_id" |
|
169 | 170 | t.integer "problem_id" |
|
170 | 171 | t.integer "submission_id" |
|
171 | 172 | t.string "input_file_name" |
|
172 | 173 | t.string "output_file_name" |
|
173 | 174 | t.string "running_stat" |
|
174 | 175 | t.integer "status" |
|
175 | 176 | t.datetime "updated_at" |
|
176 | 177 | t.datetime "submitted_at" |
|
177 | 178 | t.datetime "compiled_at" |
|
178 | 179 | t.text "compiler_message" |
|
179 | 180 | t.datetime "graded_at" |
|
180 | 181 | t.string "grader_comment" |
|
181 | 182 | t.datetime "created_at" |
|
182 | 183 | t.float "running_time" |
|
183 | 184 | t.string "exit_status" |
|
184 | 185 | t.integer "memory_usage" |
|
185 | 186 | end |
|
186 | 187 | |
|
187 | 188 | add_index "test_requests", ["user_id", "problem_id"], :name => "index_test_requests_on_user_id_and_problem_id" |
|
188 | 189 | |
|
189 | 190 | create_table "users", :force => true do |t| |
|
190 | 191 | t.string "login", :limit => 50 |
|
191 | 192 | t.string "full_name" |
|
192 | 193 | t.string "hashed_password" |
|
193 | 194 | t.string "salt", :limit => 5 |
|
194 | 195 | t.string "alias" |
|
195 | 196 | t.string "email" |
|
196 | 197 | t.integer "site_id" |
|
197 | 198 | t.integer "country_id" |
|
198 | 199 | t.boolean "activated", :default => false |
|
199 | 200 | t.datetime "created_at" |
|
200 | 201 | t.datetime "updated_at" |
|
201 | 202 | end |
|
202 | 203 | |
|
203 | 204 | add_index "users", ["login"], :name => "index_users_on_login", :unique => true |
|
204 | 205 | |
|
205 | 206 | end |
You need to be logged in to leave comments.
Login now