@@ -9,41 +9,53 @@ def capture_stderr
9
9
$stderr = stderr
10
10
end
11
11
12
+ # Fake socket for testing
13
+ #
14
+ # FakeTCPSocket.new("success", 636)
15
+ # FakeTCPSocket.new("fail.SocketError", 636) # raises SocketError
16
+ class FakeTCPSocket
17
+ def initialize ( host , port )
18
+ status , error = host . split ( "." )
19
+ if status == "fail"
20
+ raise Object . const_get ( error )
21
+ end
22
+ end
23
+ end
24
+
12
25
def test_list_of_hosts_with_first_host_successful
13
26
hosts = [
14
- [ 'test.mocked.com' , 636 ] ,
15
- [ 'test2.mocked.com' , 636 ] ,
16
- [ 'test3.mocked.com' , 636 ] ,
27
+ [ "success.host" , 636 ] ,
28
+ [ "fail.SocketError" , 636 ] ,
29
+ [ "fail.SocketError" , 636 ] ,
17
30
]
18
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 0 ] ) . once . and_return ( nil )
19
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . never
20
- Net ::LDAP ::Connection . new ( :hosts => hosts )
31
+
32
+ connection = Net ::LDAP ::Connection . new ( :hosts => hosts )
33
+ connection . socket_class = FakeTCPSocket
34
+ connection . socket
21
35
end
22
36
23
37
def test_list_of_hosts_with_first_host_failure
24
38
hosts = [
25
- [ 'test.mocked.com' , 636 ] ,
26
- [ 'test2.mocked.com' , 636 ] ,
27
- [ 'test3.mocked.com' , 636 ] ,
39
+ [ "fail.SocketError" , 636 ] ,
40
+ [ "success.host" , 636 ] ,
41
+ [ "fail.SocketError" , 636 ] ,
28
42
]
29
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 0 ] ) . once . and_raise ( SocketError )
30
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 1 ] ) . once . and_return ( nil )
31
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . never
32
- Net ::LDAP ::Connection . new ( :hosts => hosts )
43
+ connection = Net ::LDAP ::Connection . new ( :hosts => hosts )
44
+ connection . socket_class = FakeTCPSocket
45
+ connection . socket
33
46
end
34
47
35
48
def test_list_of_hosts_with_all_hosts_failure
36
49
hosts = [
37
- [ 'test.mocked.com' , 636 ] ,
38
- [ 'test2.mocked.com' , 636 ] ,
39
- [ 'test3.mocked.com' , 636 ] ,
50
+ [ "fail.SocketError" , 636 ] ,
51
+ [ "fail.SocketError" , 636 ] ,
52
+ [ "fail.SocketError" , 636 ] ,
40
53
]
41
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 0 ] ) . once . and_raise ( SocketError )
42
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 1 ] ) . once . and_raise ( SocketError )
43
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . with ( *hosts [ 2 ] ) . once . and_raise ( SocketError )
44
- flexmock ( TCPSocket ) . should_receive ( :new ) . ordered . never
54
+
55
+ connection = Net ::LDAP ::Connection . new ( :hosts => hosts )
56
+ connection . socket_class = FakeTCPSocket
45
57
assert_raise Net ::LDAP ::ConnectionError do
46
- Net :: LDAP :: Connection . new ( :hosts => hosts )
58
+ connection . socket
47
59
end
48
60
end
49
61
0 commit comments