-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtry2.rb
128 lines (104 loc) · 1.9 KB
/
try2.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
class Writer
def put(key); @file = File.open("key-#{key}", 'w'); end
def add_data(data); @file.write data; end
def finish_data(data); @file.close data; end
def get(key)
File.read("key-#{key}")
rescue Errno::ENOENT
nil
end
end
class IO
def write_it(string)
unless $TRY2 and self != STDOUT
return write_old(string)
end
if $TRY2_DIE
if $TRY2_DIE == 0
Thread.exit
else
$TRY2_DIE -= 1
end
end
require 'debugger'; debugger
$TRY2[@key] << string
end
def read_it
unless $TRY2
return read_old
end
$TRY2[@key]
end
def close_it
unless $TRY2
return close_old
end
end
alias_method :read_old, :read
alias_method :write_old, :write
alias_method :close_old, :close
alias_method :read, :read_it
alias_method :write, :write_it
alias_method :close, :close_it
end
class File
def open_it(*args)
puts "in open_it"
unless $TRY2 and self != STDOUT
return open_old(*args)
end
if $TRY2_DIE
if $TRY2_DIE == 0
Thread.exit
else
$TRY2_DIE -= 1
end
end
require 'debugger'; debugger
@key = args.first
$TRY2[@key] = ''
end
alias_method :open_old, :open
alias_method :open, :open_it
end
class Try2
def simulate(&proc)
@simulated = proc
end
def check(&proc)
@check = proc
end
def work
puts "in work"
try = 4
(1..10).each do |trial|
puts "in trial #{trial}"
$TRY2 = {}
puts "did try2"
$TRY2_DIE = try
k = Thread.new { @simulated.call }.join
@check.call
try += 1
end
end
def callit
@simulated.call
end
end
try2 = Try2.new
try2.simulate do
puts "in simulate"
f = File.open('wut', 'w')
f.write '1'
f.write '2'
f.write '3'
f.write '4'
f.close
end
try2.check do
puts "in check"
f = File.open('wut', 'r')
puts f.read
f.close
end
try2.work