Salut 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/spec/Service_spec.rb CHANGED
@@ -7,11 +7,11 @@ describe Salut::Service do
7
7
 
8
8
  describe '#advertising?' do
9
9
  before do
10
- @service = Salut::Service.new({
11
- port:3000,
12
- instance_name:'Test',
13
- service_type:'_http._tcp.'
14
- })
10
+ @service = Salut::Service.new(
11
+ port:3000,
12
+ instance_name:'Test',
13
+ service_type:'_http._tcp.'
14
+ )
15
15
  end
16
16
 
17
17
  it 'should be initialized to false' do
@@ -25,24 +25,22 @@ describe Salut::Service do
25
25
 
26
26
  it 'should be false after I stop advertising' do
27
27
  @service.start_advertising
28
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
28
+ run_run_loop
29
29
  @service.stop_advertising
30
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
30
+ run_run_loop
31
31
  @service.advertising?.should.be.equal false
32
32
  end
33
33
 
34
34
  it 'should be false if advertising fails' do
35
- @other_service = @service.dup
35
+ @service.service_type = 'badname'
36
36
  @service.start_advertising
37
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
38
- @other_service.start_advertising
39
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
40
- @other_service.advertising?.should.be.equal false
37
+ run_run_loop
38
+ @service.advertising?.should.be.equal false
41
39
  end
42
40
 
43
41
  it 'should be true when advertising is successful' do
44
42
  @service.start_advertising
45
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
43
+ run_run_loop
46
44
  @service.advertising?.should.be.equal true
47
45
  end
48
46
  end
@@ -50,11 +48,11 @@ describe Salut::Service do
50
48
 
51
49
  describe '#service' do
52
50
  before do
53
- @service = Salut::Service.new({
54
- port:3000,
55
- instance_name:'Test',
56
- service_type:'_http._tcp.'
57
- })
51
+ @service = Salut::Service.new(
52
+ port:3000,
53
+ instance_name:'Test',
54
+ service_type:'_http._tcp.'
55
+ )
58
56
  end
59
57
 
60
58
  it 'is an NSNetService instance when not nil' do
@@ -67,7 +65,7 @@ describe Salut::Service do
67
65
  type:'_http._tcp.',
68
66
  name:'TEST',
69
67
  port:4000
70
- @service = Salut::Service.new({ service:new_service })
68
+ @service = Salut::Service.new service:new_service
71
69
  @service.service.should.be.equal new_service
72
70
  end
73
71
 
@@ -80,7 +78,7 @@ describe Salut::Service do
80
78
  it 'will be set to nil when advertising stops' do
81
79
  @service.service.should.be.equal nil
82
80
  @service.start_advertising
83
- NSRunLoop.currentRunLoop.runUntilDate (Time.now + 5)
81
+ run_run_loop 5
84
82
  @service.stop_advertising
85
83
  @service.service.should.be.equal nil
86
84
  end
@@ -89,15 +87,15 @@ describe Salut::Service do
89
87
 
90
88
  describe '#initialize' do
91
89
  it 'will let you initialize the port number' do
92
- Salut::Service.new({ port:4000 }).port.should.be.equal 4000
90
+ Salut::Service.new( port:4000 ).port.should.be.equal 4000
93
91
  end
94
92
 
95
93
  it 'will let you initialize the instance name' do
96
- Salut::Service.new({ instance_name:'TEST' }).instance_name.should.be.equal 'TEST'
94
+ Salut::Service.new( instance_name:'TEST' ).instance_name.should.be.equal 'TEST'
97
95
  end
98
96
 
99
97
  it 'will let you initialize the service type' do
100
- Salut::Service.new({ service_type:'_http._tcp.' }).service_type.should.be.equal '_http._tcp.'
98
+ Salut::Service.new( service_type:'_http._tcp.' ).service_type.should.be.equal '_http._tcp.'
101
99
  end
102
100
 
103
101
  it 'will let you initialize with a service' do
@@ -105,14 +103,10 @@ describe Salut::Service do
105
103
  type:'_http._tcp.',
106
104
  name:'TEST',
107
105
  port:4000
108
- @service = Salut::Service.new({ service:new_service })
106
+ @service = Salut::Service.new service:new_service
109
107
  @service.service.should.be.equal new_service
110
108
  end
111
109
 
112
- it 'will initialize delegates to an empty hash' do
113
- Salut::Service.new.delegates.should.be.equal Hash.new
114
- end
115
-
116
110
  it 'will initialize @advertising to false' do
117
111
  Salut::Service.new.advertising?.should.be.equal false
118
112
  end
@@ -124,44 +118,31 @@ describe Salut::Service do
124
118
  end
125
119
 
126
120
 
127
- describe '#delegates' do
128
- it 'should be initialized to an empty hash' do
129
- Salut::Service.new.delegates.should.be.equal Hash.new
130
- end
121
+ describe '#delegate' do
122
+ before do @service = Salut::Service.new end
131
123
 
132
124
  it 'should be writable' do
133
- @service = Salut::Service.new
134
- @service.delegates[:test] = Proc.new { true }
135
- @service.delegates[:test].should.not.be.equal nil
136
- end
137
- end
125
+ @service.delegate :test do true end
126
+ @service.delegate( :test ).call.should.not.be.equal nil
138
127
 
139
-
140
- describe '#[]' do
141
- it 'should be equivalent to #delegates[]' do
142
- @service = Salut::Service.new
143
- @service.delegates[:test] = 'HEY'
144
- @service[:test].should.be.equal 'HEY'
128
+ @service.delegate :test do 'HEY' end
129
+ @service.delegate( :test ).call.should.be.equal 'HEY'
145
130
  end
146
- end
147
131
 
148
-
149
- describe '#[]=' do
150
- it 'should be equivalent to #delegates[]=' do
151
- @service = Salut::Service.new
152
- @service[:test] = 'HEY'
153
- @service.delegates[:test].should.be.equal 'HEY'
132
+ it 'should be readable' do
133
+ @service.delegate :test do 'HEY' end
134
+ @service.delegate( :test ).call.should.be.equal 'HEY'
154
135
  end
155
136
  end
156
137
 
157
138
 
158
139
  describe '#start_advertising' do
159
140
  before do
160
- @service = Salut::Service.new({
161
- instance_name:'TEST',
162
- service_type:'_test._tcp.',
163
- port:9001
164
- })
141
+ @service = Salut::Service.new(
142
+ instance_name:'TEST',
143
+ service_type:'_test._tcp.',
144
+ port:9001
145
+ )
165
146
  end
166
147
 
167
148
  it 'should create a new @service object' do
@@ -171,18 +152,17 @@ describe Salut::Service do
171
152
  end
172
153
 
173
154
  it 'should set the delegate for @service to self' do
174
- @service.delegates[:'netServiceWillPublish:'] = Proc.new { |sender|
175
- @service.service.should.be.equal sender
176
- }
155
+ @service.delegate :'netServiceWillPublish:' do |sender|
156
+ @service.should.be.equal sender
157
+ end
177
158
  @service.start_advertising
178
159
  end
179
160
 
180
- # a fragile test since it depends on one of the callbacks
181
- # being called
161
+ # a fragile test since it depends on one of the callbacks being called
182
162
  it 'should call #publish on @service' do
183
- @service.delegates[:'netServiceWillPublish:'] = Proc.new { |sender|
184
- @service.service.should.be.equal sender
185
- }
163
+ @service.delegate :'netServiceWillPublish:' do |sender|
164
+ @service.should.be.equal sender
165
+ end
186
166
  @service.start_advertising
187
167
  end
188
168
 
@@ -198,36 +178,35 @@ describe Salut::Service do
198
178
 
199
179
  it 'should fail if service type, instance name, or port are not set' do
200
180
  @service.service_type = nil
201
- should.raise(NoMethodError) { @service.start_advertising }
181
+ should.raise NoMethodError do @service.start_advertising end
202
182
  end
203
183
  end
204
184
 
205
185
 
206
186
  describe '#stop_advertising' do
207
187
  before do
208
- @service = Salut::Service.new({
209
- instance_name:'TEST',
210
- service_type:'_test._tcp.',
211
- port:9001
212
- })
188
+ @service = Salut::Service.new(
189
+ instance_name:'TEST',
190
+ service_type:'_test._tcp.',
191
+ port:9001
192
+ )
213
193
  @service.start_advertising
214
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 3
194
+ run_run_loop
215
195
  end
216
196
 
217
- # a fragile test since it depends on one of the callbacks
218
- # being called
197
+ # a fragile test since it depends on one of the callbacks being called
219
198
  it 'should call #stop on @service' do
220
- @service.delegates[:'netServiceDidStop:'] = Proc.new { |sender|
199
+ @service.delegate :'netServiceDidStop:' do |sender|
221
200
  true.should.be.equal true
222
- }
201
+ end
223
202
  @service.stop_advertising
224
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 3
203
+ run_run_loop
225
204
  end
226
205
 
227
206
  it 'should set @service to nil' do
228
207
  @service.service.should.not.be.equal nil
229
208
  @service.stop_advertising
230
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 3
209
+ run_run_loop
231
210
  @service.service.should.be.equal nil
232
211
  end
233
212
  end
@@ -235,188 +214,350 @@ describe Salut::Service do
235
214
 
236
215
  describe '#resolve' do
237
216
  before do
238
- @service = Salut::Service.new({
239
- instance_name:'TEST',
240
- service_type:'_test._tcp.',
241
- port:9001
242
- })
217
+ @service = Salut::Service.new(
218
+ instance_name:'TEST',
219
+ service_type:'_test._tcp.',
220
+ port:9001
221
+ )
243
222
  @service.start_advertising
244
223
  @browser = Salut::Browser.new
245
- NSRunLoop.currentRunLoop.runUntilDate Time.now + 2
224
+ run_run_loop
246
225
  end
247
226
 
248
227
  # a fragile test since it depends on callbacks of callbacks being called
249
228
  it 'should cause the resolve callback to be called' do
250
- @browser.delegates[:'netServiceBrowser:didFindService:moreComing:'] = Proc.new {
229
+ @browser.delegate :'netServiceBrowser:didFindService:moreComing:' do
251
230
  |sender, service, more|
252
- service.delegates[:'netServiceWillResolve:'] = Proc.new { |sender|
231
+ service.delegate :'netServiceWillResolve:' do |sender|
253
232
  true.should.be.equal true
254
- }
233
+ end
255
234
  service.resolve
256
- }
257
- @browser.find_services '_test._tcp.', in_domain:''
258
- NSRunLoop.currentRunLoop.runUntilDate (Time.now + 5)
235
+ end
236
+ @browser.find_services '_test._tcp.'
237
+ run_run_loop
259
238
  end
260
239
 
261
240
  it 'should allow me to override the timeout' do
262
- @browser.delegates[:'netServiceBrowser:didFindService:moreComing:'] = Proc.new {
241
+ @browser.delegate :'netServiceBrowser:didFindService:moreComing:' do
263
242
  |sender, service, more|
264
- service.delegates[:'netServiceWillResolve:'] = Proc.new { |sender|
243
+ service.delegate :'netServiceWillResolve:' do |sender|
265
244
  true.should.be.equal true
266
- }
245
+ end
267
246
  service.resolve 2.0
268
- }
269
- @browser.find_services '_test._tcp.', in_domain:''
270
- NSRunLoop.currentRunLoop.runUntilDate (Time.now + 5)
247
+ end
248
+ @browser.find_services '_test._tcp.'
249
+ run_run_loop
271
250
  end
272
251
  end
273
252
 
274
253
 
275
- # describe 'callback skeletons' do
276
-
277
- # before do
278
- # # set the logger to log INFO and log to a stringIO object
279
- # end
280
-
281
-
282
- # describe '#netServiceWillPublish' do
283
- # it 'should call its proc if exists' do
284
- # end
285
-
286
- # it 'should not explode if the proc does not exist' do
287
- # end
288
-
289
- # it 'should log a message at the INFO level' do
290
- # end
291
-
292
- # it 'should pass self to the proc' do
293
- # end
294
- # end
295
-
296
-
297
- # describe '#netService:didNotPublish:' do
298
- # it 'should call its proc if exists' do
299
- # end
300
-
301
- # it 'should not explode if the proc does not exist' do
302
- # end
303
-
304
- # it 'should log a message at the INFO level' do
305
- # end
306
-
307
- # it 'should set @advertising to false' do
308
- # end
309
-
310
- # it 'should pass self to the proc' do
311
- # end
312
-
313
- # it 'should pass the error dict to the proc' do
314
- # end
315
- # end
316
-
254
+ describe 'callback skeletons' do
317
255
 
318
- # describe '#netServiceDidPublish' do
319
- # it 'should call its proc if exists' do
320
- # end
321
-
322
- # it 'should not explode if the proc does not exist' do
323
- # end
324
-
325
- # it 'should log a message at the INFO level' do
326
- # end
327
-
328
- # it 'should set @advertising to true' do
329
- # end
330
-
331
- # it 'should pass self to the proc' do
332
- # end
333
- # end
334
-
335
-
336
- # describe '#netServiceWillResolve' do
337
- # it 'should call its proc if exists' do
338
- # end
256
+ before do
257
+ @output = StringIO.new
258
+ Salut.log = Logger.new @output
259
+ Salut.log.level = Logger::INFO
339
260
 
340
- # it 'should not explode if the proc does not exist' do
341
- # end
261
+ @service = Salut::Service.new(
262
+ service_type:'_test._tcp.',
263
+ instance_name:'TEST',
264
+ port:9001
265
+ )
266
+ end
342
267
 
343
- # it 'should log a message at the INFO level' do
344
- # end
345
268
 
346
- # it 'should pass self to the proc' do
347
- # end
348
- # end
269
+ describe '#netServiceWillPublish' do
270
+ it 'should call its proc if exists' do
271
+ @service.delegate :'netServiceWillPublish:' do |sender|
272
+ true.should.be.equal true
273
+ end
274
+ @service.start_advertising
275
+ end
349
276
 
277
+ it 'should not explode if the proc does not exist' do
278
+ @service.start_advertising
279
+ true.should.be.equal true #if we got here we didn't explode
280
+ end
350
281
 
351
- # describe '#netService:didNotResolve:' do
352
- # it 'should call its proc if exists' do
353
- # end
282
+ it 'should log a message at the INFO level' do
283
+ @service.start_advertising
284
+ @output.string.should.match /Starting to advertise/
285
+ end
354
286
 
355
- # it 'should not explode if the proc does not exist' do
356
- # end
287
+ it 'should pass self to the proc' do
288
+ @service.delegate :'netServiceWillPublish:' do |sender|
289
+ sender.should.be.equal @service
290
+ end
291
+ @service.start_advertising
292
+ end
293
+ end
357
294
 
358
- # it 'should log a message at the INFO level' do
359
- # end
360
295
 
361
- # it 'should pass self to the proc' do
362
- # end
296
+ describe '#netService:didNotPublish:' do
297
+ before do
298
+ @service.service_type = 'badname' # this is how we make publishing fail
299
+ end
363
300
 
364
- # it 'should pass the error dict to the proc' do
365
- # end
366
- # end
301
+ it 'should call its proc if exists' do
302
+ @service.delegate :'netService:didNotPublish:' do |sender, dict|
303
+ true.should.be.equal true
304
+ end
305
+ @service.start_advertising
306
+ run_run_loop
307
+ end
308
+
309
+ it 'should not explode if the proc does not exist' do
310
+ @service.start_advertising
311
+ run_run_loop
312
+ true.should.be.equal true
313
+ end
314
+
315
+ it 'should log a message at the INFO level' do
316
+ @service.start_advertising
317
+ run_run_loop
318
+ @output.string.should.match /ERROR: could not advertise/
319
+ end
320
+
321
+ it '@advertising will still be false' do
322
+ @service.start_advertising
323
+ run_run_loop
324
+ @service.advertising.should.be.equal false
325
+ end
326
+
327
+ it 'should pass self to the proc' do
328
+ @service.delegate :'netService:didNotPublish:' do |sender, dict|
329
+ sender.should.be.equal @service
330
+ end
331
+ @service.start_advertising
332
+ run_run_loop
333
+ end
334
+
335
+ it 'should pass the error dict to the proc' do
336
+ @service.delegate :'netService:didNotPublish:' do |sender, dict|
337
+ dict.class.should.be.equal Hash
338
+ dict['NSNetServicesErrorCode'].should.not.be.equal nil
339
+ end
340
+ @service.start_advertising
341
+ run_run_loop
342
+ end
343
+ end
344
+
345
+
346
+ describe '#netServiceDidPublish' do
347
+ it 'should call its proc if exists' do
348
+ @service.delegate :'netServiceDidPublish:' do |sender|
349
+ true.should.be.equal true
350
+ end
351
+ @service.start_advertising
352
+ run_run_loop
353
+ end
354
+
355
+ it 'should not explode if the proc does not exist' do
356
+ @service.start_advertising
357
+ run_run_loop
358
+ true.should.be.equal true
359
+ end
360
+
361
+ it 'should log a message at the INFO level' do
362
+ @service.start_advertising
363
+ run_run_loop
364
+ @output.string.should.match /Successfully advertising/
365
+ end
366
+
367
+ it 'should set @advertising to true' do
368
+ @service.start_advertising
369
+ run_run_loop
370
+ @service.advertising.should.be.equal true
371
+ end
372
+
373
+ it 'should pass self to the proc' do
374
+ @service.delegate :'netServiceDidPublish:' do |sender|
375
+ sender.should.be.equal @service
376
+ end
377
+ @service.start_advertising
378
+ run_run_loop
379
+ end
380
+ end
381
+
382
+
383
+ describe '#netServiceWillResolve' do
384
+ before do
385
+ @service.start_advertising
386
+ @browser = Salut::Browser.new
387
+ @browser.find_services '_test._tcp.'
388
+ run_run_loop
389
+ @found_service = @browser.services.first
390
+ end
391
+
392
+ it 'should call its proc if it exists' do
393
+ @found_service.delegate :'netServiceWillResolve:' do |sender|
394
+ true.should.be.equal true
395
+ end
396
+ @found_service.resolve
397
+ end
367
398
 
399
+ it 'should not explode if the proc does not exist' do
400
+ @found_service.resolve
401
+ true.should.be.equal true
402
+ end
368
403
 
369
- # describe '#netServiceDidResolveAddress' do
370
- # it 'should call its proc if exists' do
371
- # end
404
+ it 'should log a message at the INFO level' do
405
+ @found_service.resolve
406
+ @output.string.should.match /Resolving service/
407
+ end
372
408
 
373
- # it 'should not explode if the proc does not exist' do
374
- # end
409
+ it 'should pass self to the proc' do
410
+ @found_service.delegate :'netServiceWillResolve:' do |sender|
411
+ @found_service.should.be.equal sender
412
+ end
413
+ @found_service.resolve
414
+ end
415
+ end
375
416
 
376
- # it 'should log a message at the INFO level' do
377
- # end
378
417
 
379
- # it 'should pass self to the proc' do
380
- # end
381
- # end
418
+ # Spitting in the face of my own documentation. Why? Because I want the
419
+ # the code to fail and this is the easiest way to make it happen.
420
+ describe '#netService:didNotResolve:' do
421
+ before do @service.start_advertising end
382
422
 
423
+ it 'should call its proc if it exists' do
424
+ @service.delegate :'netService:didNotResolve:' do |sender, dict|
425
+ true.should.be.equal true
426
+ end
427
+ @service.resolve 1
428
+ run_run_loop
429
+ end
430
+
431
+ it 'should not explode if the proc does not exist' do
432
+ @service.resolve 1
433
+ run_run_loop
434
+ true.should.be.equal true
435
+ end
436
+
437
+ it 'should log a message at the INFO level' do
438
+ @service.resolve 1
439
+ run_run_loop
440
+ @output.string.should.match /ERROR: could not resolve/
441
+ end
442
+
443
+ it 'should pass self to the proc' do
444
+ @service.delegate :'netService:didNotResolve:' do |sender, dict|
445
+ sender.should.be.equal @service
446
+ end
447
+ @service.resolve 1
448
+ run_run_loop
449
+ end
450
+
451
+ it 'should pass the error dict to the proc' do
452
+ @service.delegate :'netService:didNotResolve:' do |sender, dict|
453
+ dict.class.should.be.equal Hash
454
+ end
455
+ @service.resolve 1
456
+ run_run_loop
457
+ end
458
+ end
459
+
460
+
461
+ describe '#netServiceDidResolveAddress' do
462
+ before do
463
+ @service.start_advertising
464
+ @browser = Salut::Browser.new
465
+ @browser.find_services '_test._tcp.'
466
+ run_run_loop
467
+ @found_service = @browser.services.first
468
+ end
469
+
470
+ it 'should call its proc if it exists' do
471
+ @found_service.delegate :'netServiceDidResolveAddress:' do |sender|
472
+ true.should.be.equal true
473
+ end
474
+ @found_service.resolve
475
+ run_run_loop
476
+ end
477
+
478
+ it 'should not explode if the proc does not exist' do
479
+ @found_service.resolve
480
+ run_run_loop
481
+ true.should.be.equal true
482
+ end
383
483
 
384
- # describe '#netService:didUpdateTXTRecordData:' do
385
- # it 'should call its proc if exists' do
386
- # end
484
+ it 'should log a message at the INFO level' do
485
+ @found_service.resolve
486
+ run_run_loop
487
+ @output.string.should.match /Resolved address for service/
488
+ end
387
489
 
388
- # it 'should not explode if the proc does not exist' do
389
- # end
490
+ it 'should pass self to the proc' do
491
+ @found_service.delegate :'netServiceDidResolveAddress:' do |sender|
492
+ @found_service.should.be.equal sender
493
+ end
494
+ @found_service.resolve
495
+ run_run_loop
496
+ end
497
+ end
390
498
 
391
- # it 'should log a message at the INFO level' do
392
- # end
393
499
 
394
- # it 'should pass self to the proc' do
395
- # end
500
+ # describe '#netService:didUpdateTXTRecordData:' do
501
+ # it 'should call its proc if exists' do
502
+ # end
396
503
 
397
- # it 'should pass the TXT record data to the proc' do
398
- # end
399
- # end
504
+ # it 'should not explode if the proc does not exist' do
505
+ # end
400
506
 
507
+ # it 'should log a message at the INFO level' do
508
+ # end
401
509
 
402
- # describe '#netServiceDidStop' do
403
- # it 'should call its proc if exists' do
404
- # end
510
+ # it 'should pass self to the proc' do
511
+ # end
405
512
 
406
- # it 'should not explode if the proc does not exist' do
407
- # end
513
+ # it 'should pass the TXT record data to the proc' do
514
+ # end
515
+ # end
408
516
 
409
- # it 'should log a message at the INFO level' do
410
- # end
411
517
 
412
- # it 'should set @advertising to false' do
413
- # end
518
+ describe '#netServiceDidStop' do
519
+ before do
520
+ @service.start_advertising
521
+ run_run_loop 1
522
+ end
414
523
 
415
- # it 'should pass self to the proc' do
416
- # end
417
- # end
524
+ it 'should call its proc if exists' do
525
+ @service.delegate :'netServiceDidStop:' do |sender|
526
+ true.should.be.equal true
527
+ end
528
+ @service.stop_advertising
529
+ run_run_loop
530
+ end
531
+
532
+ it 'should not explode if the proc does not exist' do
533
+ @service.stop_advertising
534
+ run_run_loop
535
+ true.should.be.equal true
536
+ end
537
+
538
+ it 'should log a message at the INFO level' do
539
+ @service.stop_advertising
540
+ run_run_loop
541
+ @output.string.should.match /Stopped advertising/
542
+ end
543
+
544
+ it 'should set @advertising to false' do
545
+ @service.advertising?.should.be.equal true
546
+ @service.stop_advertising
547
+ run_run_loop
548
+ @service.advertising?.should.be.equal false
549
+ end
550
+
551
+ it 'should pass self to the proc' do
552
+ @service.delegate :'netServiceDidStop:' do |sender|
553
+ sender.should.be.equal @service
554
+ end
555
+ @service.stop_advertising
556
+ run_run_loop
557
+ end
558
+ end
418
559
 
419
- # end
560
+ end
420
561
 
421
562
 
422
563
  end