Class: FakeJobServer

Inherits:
Object
  • Object
show all
Defined in:
lib/gearman/testlib.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#portObject (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_connectionObject



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_socketObject



15
16
17
# File 'lib/gearman/testlib.rb', line 15

def server_socket
  @serv
end

#startObject



23
24
25
# File 'lib/gearman/testlib.rb', line 23

def start
  @serv = TCPserver.open(@port)
end

#stopObject



19
20
21
# File 'lib/gearman/testlib.rb', line 19

def stop
  @serv.close
end