Class: FakeJobServer
- Inherits:
-
Object
- Object
- FakeJobServer
- Defined in:
- lib/gearman/testlib.rb
Instance Attribute Summary collapse
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #expect_any_request(sock) ⇒ Object
- #expect_anything_and_close_socket(sock) ⇒ Object
- #expect_closed(sock) ⇒ Object
- #expect_connection ⇒ Object
- #expect_request(sock, exp_type, exp_data = '', size = 12) ⇒ Object
-
#initialize(tester, port = nil) ⇒ FakeJobServer
constructor
A new instance of FakeJobServer.
- #send_response(sock, type, data = '', bogus_size = nil) ⇒ Object
- #server_socket ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
Constructor Details
#initialize(tester, port = nil) ⇒ FakeJobServer
Returns a new instance of FakeJobServer.
7 8 9 10 11 12 |
# File 'lib/gearman/testlib.rb', line 7 def initialize(tester,port=nil) @tester = tester @serv = TCPserver.open(0) if port.nil? @serv = TCPserver.open('localhost',port) unless port.nil? @port = @serv.addr[1] end |
Instance Attribute Details
#port ⇒ Object (readonly)
Returns the value of attribute port.
13 14 15 |
# File 'lib/gearman/testlib.rb', line 13 def port @port end |
Instance Method Details
#expect_any_request(sock) ⇒ Object
45 46 47 |
# File 'lib/gearman/testlib.rb', line 45 def expect_any_request(sock) head = sock.recv(12) end |
#expect_anything_and_close_socket(sock) ⇒ Object
49 50 51 52 |
# File 'lib/gearman/testlib.rb', line 49 def expect_anything_and_close_socket(sock) head = sock.recv(12) sock.close end |
#expect_closed(sock) ⇒ Object
32 33 34 |
# File 'lib/gearman/testlib.rb', line 32 def expect_closed(sock) @tester.assert_true(sock.closed?) end |
#expect_connection ⇒ Object
27 28 29 30 |
# File 'lib/gearman/testlib.rb', line 27 def expect_connection sock = @serv.accept return sock end |
#expect_request(sock, exp_type, exp_data = '', size = 12) ⇒ Object
36 37 38 39 40 41 42 43 |
# File 'lib/gearman/testlib.rb', line 36 def expect_request(sock, exp_type, exp_data='', size=12) head = sock.recv(size) magic, type, len = head.unpack('a4NN') @tester.assert("\0REQ" == magic || "\000REQ" == magic) @tester.assert_equal(Gearman::Util::NUMS[exp_type.to_sym], type) data = len > 0 ? sock.recv(len) : '' @tester.assert_equal(exp_data, data) end |
#send_response(sock, type, data = '', bogus_size = nil) ⇒ Object
54 55 56 57 58 |
# File 'lib/gearman/testlib.rb', line 54 def send_response(sock, type, data='', bogus_size=nil) type_num = Gearman::Util::NUMS[type.to_sym] || 0 response = "\0RES" + [type_num, (bogus_size or data.size)].pack('NN') + data sock.write(response) end |
#server_socket ⇒ Object
15 16 17 |
# File 'lib/gearman/testlib.rb', line 15 def server_socket @serv end |
#start ⇒ Object
23 24 25 |
# File 'lib/gearman/testlib.rb', line 23 def start @serv = TCPserver.open(@port) end |
#stop ⇒ Object
19 20 21 |
# File 'lib/gearman/testlib.rb', line 19 def stop @serv.close end |