-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBPTest.rb
97 lines (88 loc) · 2.93 KB
/
BPTest.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require 'test/unit'
require_relative 'bp'
class BpTest < Test::Unit::TestCase
attr_accessor :prog
def setup
$line = []
arb = Arbiter.new
@prog = BProgram.new arb
arb.program = @prog
end
# class TestSanity < BpTest
# def test_add_run_bt
# bt = BThread.new do |e|
# p "printing hello to line"
# $line << "hello!"
# end
#
# @prog.bthreads.push bt
# @prog.start
# assert_equal ["hello!"], $line, "bthread didn't print hello!"
# end
# end
class TestAlternator < BpTest
class Tap1 < BThread
def initialize(args = {})
super ({:bodyfunc =>
lambda {
p "bt1 started!"
sleep(2)
for i in 1..3 do
bsync(:request => [:e1],
:wait => [],
:block => [])
puts 1
sleep(1)
$line << 1
end
}}.merge(args))
end
end
class Tap2 < BThread
def initialize(args={})
super(args)
self.bodyfunc = lambda {
puts 'bt2 started!'
sleep(2)
for i in 1..3 do
bsync(:request => [:e2],
:wait => [],
:block => [])
puts 2
sleep(1)
$line << 2
end
}
end
end
def test_alternating
bt1 = Tap1.new({:name => "Tap1",
:program => @prog})
bt2 = Tap2.new({:name => "Tap2",
:program => @prog})
btAlternator = BThread.new({:name => "Alternator",
:program => @prog,
:bodyfunc => lambda {
p "alternator started!"
sleep(1)
for i in 1..3 do
p 'alternator blocking 2'
sleep(1)
btAlternator.bsync(:request => [],
:wait => BaseEvents.any,
:block => [:e2])
p 'alternator blocking 1'
sleep(1)
btAlternator.bsync(:request => [],
:wait => BaseEvents.any,
:block => [:e1])
end
p "alternator done!"
}
})
@prog.bthreads.push bt1, bt2, btAlternator
@prog.start
assert_equal [1, 2, 1, 2, 1, 2], $line, "bthreads must alternate"
end
end
end