-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththreading_test.rb
53 lines (47 loc) · 2.02 KB
/
threading_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
require_relative "command"
require_relative "execution_result"
require_relative "service_base"
require_relative "DataProxies/DatabaseProxies/customer_database_proxy"
require_relative "DataProxies/RestfulServiceProxies/customer_restful_proxy"
require_relative "Rules/field_length_rule"
require_relative "Rules/field_required_rule"
class CustomerService < ServiceBase
def initialize(data_proxy)
super(data_proxy)
end
def rules_for_insert(entity)
rules = []
rules << FieldRequiredRule.new(entity, :name)
.if_valid_then_execute(lambda { |rule| puts "WOOT!" })
.if_valid_then_validate(FieldLengthRule.new(entity, :name, 10)
.if_invalid_then_execute(lambda { |rule| puts "ARGH!!" }))
rules
end
end
#customer_data_proxy = CustomerRestfulProxy.new
customer_data_proxy = CustomerDatabaseProxy.new
service = CustomerService.new(customer_data_proxy)
commands = []
commands << service.insert_command({:id => 1, :name => ""})
commands << service.insert_command({:id => 2, :name => "Foo Bar"})
commands << service.insert_command({:id => 3, :name => ""})
commands << service.insert_command({:id => 4, :name => "Mikasaessucasa"})
commands << service.insert_command({:id => 5, :name => "John"})
commands << service.insert_command({:id => 6, :name => ""})
commands << service.insert_command({:id => 7, :name => ""})
commands << service.insert_command({:id => 8, :name => "meh"})
commands << service.insert_command({:id => 9, :name => ""})
commands << service.insert_command({:id => 10, :name => "Aargssssssssss"})
commands << service.insert_command({:id => 11, :name => ""})
results = []
#threads = commands.map { |command| Thread.new(command) { |command| results << command.execute } }
mutex = Mutex.new
threads = commands.map do |command|
Thread.new(command) do |command|
mutex.synchronize do
results << command.execute
end
end
end
threads.each { |t| t.join }
results.each { |result| puts result.inspect }