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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
# frozen_string_literal: false
begin
require 'win32ole'
rescue LoadError
end
require 'test/unit'
ado_installed =
if defined?(WIN32OLE)
db = nil
begin
db = WIN32OLE.new('ADODB.Connection')
db.connectionString = "Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=.;"
db.open
db.close
db = nil
true
rescue
end
end
swbemsink_available =
if defined?(WIN32OLE)
begin
WIN32OLE.new('WbemScripting.SWbemSink')
true
rescue
end
end
if defined?(WIN32OLE_EVENT)
class TestWIN32OLE_EVENT < Test::Unit::TestCase
def test_s_new_exception
assert_raise(TypeError) {
WIN32OLE_EVENT.new("A")
}
end
def test_s_new_non_exist_event
dict = WIN32OLE.new('Scripting.Dictionary')
assert_raise(RuntimeError) {
WIN32OLE_EVENT.new(dict)
}
end
end
if swbemsink_available
class TestWIN32OLE_EVENT_SWbemSink < Test::Unit::TestCase
def setup
@wmi = WIN32OLE.connect('winmgmts://localhost/root/cimv2')
@sws = WIN32OLE.new('WbemScripting.SWbemSink')
@event = @event1 = @event2 = ""
@sql = "SELECT * FROM __InstanceModificationEvent WITHIN 1 WHERE TargetInstance ISA 'Win32_LocalTime'"
end
def message_loop(watch_ivar = nil)
if watch_ivar
orig_ivar = instance_variable_get(watch_ivar)
end
2.times do
WIN32OLE_EVENT.message_loop
sleep 1
end
if watch_ivar
# wait until event is proceeded
tries = 0
seconds = EnvUtil.apply_timeout_scale(1)
while tries < 5 && instance_variable_get(watch_ivar) == orig_ivar
$stderr.puts "test_win32ole_event.rb: retrying and sleeping #{seconds}s until #{watch_ivar} is changed from #{orig_ivar.inspect}..."
WIN32OLE_EVENT.message_loop
sleep(seconds)
tries += 1
seconds *= 2 # sleep at most 31s in total
end
end
end
def default_handler(event, *args)
@event += event
end
def handler1
@event1 = "handler1"
end
def test_s_new_non_exist_event
assert_raise(RuntimeError) {
WIN32OLE_EVENT.new(@sws, 'XXXXX')
}
end
def test_s_new
obj = WIN32OLE_EVENT.new(@sws, 'ISWbemSinkEvents')
assert_instance_of(WIN32OLE_EVENT, obj)
obj = WIN32OLE_EVENT.new(@sws)
assert_instance_of(WIN32OLE_EVENT, obj)
end
def test_s_new_loop
exec_notification_query_async
ev = WIN32OLE_EVENT.new(@sws)
ev.on_event {|*args| default_handler(*args)}
message_loop
10.times do |i|
WIN32OLE_EVENT.new(@sws)
message_loop
GC.start
end
# @event randomly becomes "OnCompleted" here. Try to wait until it matches.
# https://ci.appveyor.com/project/ruby/ruby/builds/19963142/job/8gaxepksa0i3b998
assert_match_with_retries(/OnObjectReady/, :@event)
end
def test_on_event
exec_notification_query_async
ev = WIN32OLE_EVENT.new(@sws, 'ISWbemSinkEvents')
ev.on_event {|*args| default_handler(*args)}
message_loop(:@event)
assert_match(/OnObjectReady/, @event)
end
def test_on_event_symbol
exec_notification_query_async
ev = WIN32OLE_EVENT.new(@sws)
ev.on_event(:OnObjectReady) {|*args|
handler1
}
message_loop(:@event1)
assert_equal("handler1", @event1)
end
private
def exec_notification_query_async
@wmi.ExecNotificationQueryAsync(@sws, @sql)
rescue => e
if /OLE error code:80041008 in SWbemServicesEx/ =~ e.message
omit "No administrator privilege?"
end
raise
end
def assert_match_with_retries(regexp, ivarname)
ivar = instance_variable_get(ivarname)
tries = 0
while tries < 6 && !ivar.match(regexp)
$stderr.puts "test_win32ole_event.rb: retrying until #{ivarname} (#{ivar}) matches #{regexp} (tries: #{tries})..."
sleep(2 ** tries) # sleep at most 63s in total
ivar = instance_variable_get(ivarname)
tries += 1
end
assert_match(regexp, ivar)
end
end
end
if ado_installed
class TestWIN32OLE_EVENT_ADO < Test::Unit::TestCase
CONNSTR="Driver={Microsoft Text Driver (*.txt; *.csv)};DefaultDir=.;"
module ADO
end
def message_loop
WIN32OLE_EVENT.message_loop
end
def default_handler(event, *args)
@event += event
end
def setup
@db = WIN32OLE.new('ADODB.Connection')
if !defined?(ADO::AdStateOpen)
WIN32OLE.const_load(@db, ADO)
end
@db.connectionString = CONNSTR
@event = ""
@event2 = ""
@event3 = ""
end
def test_on_event2
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
ev.on_event('WillConnect') {|*args| handler1}
ev.on_event('WillConnect') {|*args| handler2}
@db.open
message_loop
assert_equal("handler2", @event2)
end
def test_on_event4
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
ev.on_event{|*args| handler1}
ev.on_event{|*args| handler2}
ev.on_event('WillConnect'){|*args| handler3(*args)}
@db.open
message_loop
assert_equal(CONNSTR, @event3)
assert("handler2", @event2)
end
def test_on_event5
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
ev.on_event {|*args| default_handler(*args)}
ev.on_event('WillConnect'){|*args| handler3(*args)}
@db.open
message_loop
assert_match(/ConnectComplete/, @event)
assert(/WillConnect/ !~ @event)
assert_equal(CONNSTR, @event3)
end
def test_unadvise
ev = WIN32OLE_EVENT.new(@db, 'ConnectionEvents')
ev.on_event {|*args| default_handler(*args)}
@db.open
message_loop
assert_match(/WillConnect/, @event)
ev.unadvise
@event = ""
@db.close
@db.open
message_loop
assert_equal("", @event);
assert_raise(WIN32OLERuntimeError) {
ev.on_event {|*args| default_handler(*args)}
}
end
def test_on_event_with_outargs
ev = WIN32OLE_EVENT.new(@db)
@db.connectionString = 'XXX' # set illegal connection string
assert_raise(WIN32OLERuntimeError) {
@db.open
}
ev.on_event_with_outargs('WillConnect'){|*args|
args.last[0] = CONNSTR # ConnectionString = CONNSTR
}
@db.open
message_loop
assert(true)
end
def test_on_event_hash_return
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){|*args|
{:return => 1, :ConnectionString => CONNSTR}
}
@db.connectionString = 'XXX'
@db.open
assert(true)
end
def test_on_event_hash_return2
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){|*args|
{:ConnectionString => CONNSTR}
}
@db.connectionString = 'XXX'
@db.open
assert(true)
end
def test_on_event_hash_return3
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){|*args|
{'ConnectionString' => CONNSTR}
}
@db.connectionString = 'XXX'
@db.open
assert(true)
end
def test_on_event_hash_return4
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){|*args|
{'return' => 1, 'ConnectionString' => CONNSTR}
}
@db.connectionString = 'XXX'
@db.open
assert(true)
end
def test_on_event_hash_return5
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){|*args|
{0 => CONNSTR}
}
@db.connectionString = 'XXX'
@db.open
assert(true)
end
def test_off_event
ev = WIN32OLE_EVENT.new(@db)
ev.on_event{handler1}
ev.off_event
@db.open
message_loop
assert_equal("", @event2)
end
def test_off_event_arg
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){handler1}
ev.off_event('WillConnect')
@db.open
message_loop
assert_equal("", @event2)
end
def test_off_event_arg2
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){handler1}
ev.on_event('ConnectComplete'){handler1}
ev.off_event('WillConnect')
@db.open
message_loop
assert_equal("handler1", @event2)
end
def test_off_event_sym_arg
ev = WIN32OLE_EVENT.new(@db)
ev.on_event('WillConnect'){handler1}
ev.off_event(:WillConnect)
@db.open
message_loop
assert_equal("", @event2)
end
def handler1
@event2 = "handler1"
end
def handler2
@event2 = "handler2"
end
def handler3(*arg)
@event3 += arg[0]
end
def teardown
if @db && @db.state == ADO::AdStateOpen
@db.close
end
message_loop
@db = nil
end
class Handler1
attr_reader :val1, :val2, :val3, :val4
def initialize
@val1 = nil
@val2 = nil
@val3 = nil
@val4 = nil
end
def onWillConnect(conn, uid, pwd, opts, stat, pconn)
@val1 = conn
end
def onConnectComplete(err, stat, pconn)
@val2 = err
@val3 = stat
end
def onInfoMessage(err, stat, pconn)
@val4 = stat
end
end
class Handler2
attr_reader :ev
def initialize
@ev = ""
end
def method_missing(ev, *arg)
@ev += ev
end
end
def test_handler1
ev = WIN32OLE_EVENT.new(@db)
h1 = Handler1.new
ev.handler = h1
@db.open
message_loop
assert_equal(CONNSTR, h1.val1)
assert_equal(h1.val1, ev.handler.val1)
assert_equal(nil, h1.val2)
assert_equal(ADO::AdStateOpen, h1.val3)
assert_equal(ADO::AdStateOpen, h1.val4)
end
def test_handler2
ev = WIN32OLE_EVENT.new(@db)
h2 = Handler2.new
ev.handler = h2
@db.open
message_loop
assert(h2.ev != "")
end
end
end
end
|