thrift-client 0.1.0 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. checksums.yaml +4 -4
  2. data/lib/thrift_client.rb +6 -5
  3. data/lib/thrift_client/abstract_thrift_client.rb +106 -69
  4. data/lib/thrift_client/pool.rb +34 -0
  5. data/lib/thrift_client/pool/fiber_connection_pool.rb +67 -0
  6. data/lib/thrift_client/pool/thread_connection_pool.rb +69 -0
  7. data/lib/thrift_client/{abstract_server.rb → server.rb} +62 -60
  8. data/lib/thrift_client/thrift/client.rb +15 -0
  9. data/lib/thrift_client/thrift/processor.rb +44 -0
  10. data/lib/thrift_client/{thrift.rb → thrift/protocol.rb} +1 -72
  11. data/lib/thrift_client/thrift/struct.rb +15 -0
  12. data/lib/thrift_client/thrift/transport.rb +208 -0
  13. data/test/em_test.rb +1 -0
  14. data/test/foobar/bar_service.rb +133 -0
  15. data/test/foobar/common_service.rb +388 -0
  16. data/test/foobar/foo_service.rb +133 -0
  17. data/test/{foobar_constants.rb → foobar/foobar_constants.rb} +3 -3
  18. data/test/{foobar_types.rb → foobar/foobar_types.rb} +12 -10
  19. data/test/foobar/handler.rb +74 -0
  20. data/test/helper.rb +50 -0
  21. data/test/passport.rb +51 -0
  22. data/test/start_server.rb +8 -0
  23. data/test/test.rb +27 -0
  24. data/test/test_client.rb +111 -0
  25. data/test/test_config.rb +54 -0
  26. data/test/test_connection_pool.rb +43 -0
  27. data/test/test_eventmachine_transprot.rb +146 -0
  28. data/test/test_multiplexed.rb +62 -0
  29. metadata +47 -24
  30. data/lib/thrift_client/multi_client_server.rb +0 -59
  31. data/lib/thrift_client/single_client_server.rb +0 -37
  32. data/test/client_test.rb +0 -172
  33. data/test/foobar_service.rb +0 -282
  34. data/test/multiplexed_protocol_test.rb +0 -60
  35. data/test/multiplexed_server.rb +0 -102
  36. data/test/server.rb +0 -97
@@ -0,0 +1,133 @@
1
+ #
2
+ # Autogenerated by Thrift Compiler (0.9.1)
3
+ #
4
+ # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
+ #
6
+
7
+ require 'thrift'
8
+ require 'foobar/foobar_types'
9
+
10
+ module Test
11
+ module FooService
12
+ class Client
13
+ include ::Thrift::Client
14
+
15
+ def getFoo()
16
+ send_getFoo()
17
+ return recv_getFoo()
18
+ end
19
+
20
+ def send_getFoo()
21
+ send_message('getFoo', GetFoo_args)
22
+ end
23
+
24
+ def recv_getFoo()
25
+ result = receive_message(GetFoo_result)
26
+ return result.success unless result.success.nil?
27
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'getFoo failed: unknown result')
28
+ end
29
+
30
+ def putFoo(foo)
31
+ send_putFoo(foo)
32
+ return recv_putFoo()
33
+ end
34
+
35
+ def send_putFoo(foo)
36
+ send_message('putFoo', PutFoo_args, :foo => foo)
37
+ end
38
+
39
+ def recv_putFoo()
40
+ result = receive_message(PutFoo_result)
41
+ return result.success unless result.success.nil?
42
+ raise ::Thrift::ApplicationException.new(::Thrift::ApplicationException::MISSING_RESULT, 'putFoo failed: unknown result')
43
+ end
44
+
45
+ end
46
+
47
+ class Processor
48
+ include ::Thrift::Processor
49
+
50
+ def process_getFoo(seqid, iprot, oprot)
51
+ args = read_args(iprot, GetFoo_args)
52
+ result = GetFoo_result.new()
53
+ result.success = @handler.getFoo()
54
+ write_result(result, oprot, 'getFoo', seqid)
55
+ end
56
+
57
+ def process_putFoo(seqid, iprot, oprot)
58
+ args = read_args(iprot, PutFoo_args)
59
+ result = PutFoo_result.new()
60
+ result.success = @handler.putFoo(args.foo)
61
+ write_result(result, oprot, 'putFoo', seqid)
62
+ end
63
+
64
+ end
65
+
66
+ # HELPER FUNCTIONS AND STRUCTURES
67
+
68
+ class GetFoo_args
69
+ include ::Thrift::Struct, ::Thrift::Struct_Union
70
+
71
+ FIELDS = {
72
+
73
+ }
74
+
75
+ def struct_fields; FIELDS; end
76
+
77
+ def validate
78
+ end
79
+
80
+ ::Thrift::Struct.generate_accessors self
81
+ end
82
+
83
+ class GetFoo_result
84
+ include ::Thrift::Struct, ::Thrift::Struct_Union
85
+ SUCCESS = 0
86
+
87
+ FIELDS = {
88
+ SUCCESS => {:type => ::Thrift::Types::STRUCT, :name => 'success', :class => ::Test::Foo}
89
+ }
90
+
91
+ def struct_fields; FIELDS; end
92
+
93
+ def validate
94
+ end
95
+
96
+ ::Thrift::Struct.generate_accessors self
97
+ end
98
+
99
+ class PutFoo_args
100
+ include ::Thrift::Struct, ::Thrift::Struct_Union
101
+ FOO = 1
102
+
103
+ FIELDS = {
104
+ FOO => {:type => ::Thrift::Types::STRUCT, :name => 'foo', :class => ::Test::Foo}
105
+ }
106
+
107
+ def struct_fields; FIELDS; end
108
+
109
+ def validate
110
+ end
111
+
112
+ ::Thrift::Struct.generate_accessors self
113
+ end
114
+
115
+ class PutFoo_result
116
+ include ::Thrift::Struct, ::Thrift::Struct_Union
117
+ SUCCESS = 0
118
+
119
+ FIELDS = {
120
+ SUCCESS => {:type => ::Thrift::Types::BOOL, :name => 'success'}
121
+ }
122
+
123
+ def struct_fields; FIELDS; end
124
+
125
+ def validate
126
+ end
127
+
128
+ ::Thrift::Struct.generate_accessors self
129
+ end
130
+
131
+ end
132
+
133
+ end
@@ -1,11 +1,11 @@
1
1
  #
2
- # Autogenerated by Thrift Compiler (0.9.0)
2
+ # Autogenerated by Thrift Compiler (0.9.1)
3
3
  #
4
4
  # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
5
  #
6
6
 
7
7
  require 'thrift'
8
- require 'foobar_types'
8
+ require 'foobar/foobar_types'
9
9
 
10
- module Foo
10
+ module Test
11
11
  end
@@ -1,12 +1,12 @@
1
1
  #
2
- # Autogenerated by Thrift Compiler (0.9.0)
2
+ # Autogenerated by Thrift Compiler (0.9.1)
3
3
  #
4
4
  # DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
5
5
  #
6
6
 
7
7
  require 'thrift'
8
8
 
9
- module Foo
9
+ module Test
10
10
  class Foo
11
11
  include ::Thrift::Struct, ::Thrift::Struct_Union
12
12
  FIRST = 1
@@ -31,16 +31,18 @@ module Foo
31
31
 
32
32
  class Bar
33
33
  include ::Thrift::Struct, ::Thrift::Struct_Union
34
- FOURTH = 1
35
- THIRD = 2
36
- SECOND = 3
37
- FIRST = 4
34
+ A = 1
35
+ B = 2
36
+ C = 3
37
+ D = 4
38
+ E = 5
38
39
 
39
40
  FIELDS = {
40
- FOURTH => {:type => ::Thrift::Types::STRING, :name => 'fourth'},
41
- THIRD => {:type => ::Thrift::Types::DOUBLE, :name => 'third'},
42
- SECOND => {:type => ::Thrift::Types::I64, :name => 'second'},
43
- FIRST => {:type => ::Thrift::Types::I32, :name => 'first'}
41
+ A => {:type => ::Thrift::Types::STRING, :name => 'a'},
42
+ B => {:type => ::Thrift::Types::I32, :name => 'b'},
43
+ C => {:type => ::Thrift::Types::BOOL, :name => 'c'},
44
+ D => {:type => ::Thrift::Types::DOUBLE, :name => 'd'},
45
+ E => {:type => ::Thrift::Types::I64, :name => 'e'}
44
46
  }
45
47
 
46
48
  def struct_fields; FIELDS; end
@@ -0,0 +1,74 @@
1
+ module Test
2
+ class FooHandler
3
+ def getFoo
4
+ foo = Test::Foo.new
5
+ foo.fourth = "222"
6
+ foo.third = 2.2
7
+ foo.second = 222222222222222
8
+ foo.first = 200000
9
+ return foo
10
+ end
11
+
12
+ def putFoo(foo)
13
+ puts foo.inspect
14
+ return true
15
+ end
16
+ end
17
+
18
+ class BarHandler
19
+ def getBar
20
+ bar = Test::Bar.new
21
+ bar.a = "1111111111"
22
+ bar.b = 11111111
23
+ bar.c = false
24
+ bar.d = 1111111.232323
25
+ bar.e = 111111111111111
26
+ return bar
27
+ end
28
+
29
+ def putBar(bar)
30
+ puts bar.inspect
31
+ return true
32
+ end
33
+ end
34
+
35
+ class CommonHandler
36
+ def ping
37
+ puts "ping"
38
+ end
39
+
40
+ def wait(seconds)
41
+ sleep(seconds / 100.0)
42
+ return true
43
+ end
44
+
45
+ def timeout
46
+ sleep(200000)
47
+ return false
48
+ end
49
+
50
+ def apperror()
51
+ puts "error"
52
+ return Thrift::ApplicationException.new("apperr")
53
+ end
54
+
55
+ def ptoerror()
56
+ puts "error"
57
+ raise Thrift::ProtocolException.new("ptoerror")
58
+ return true
59
+ end
60
+
61
+ def tranerror()
62
+ puts "error"
63
+ raise Thrift::TransportException.new("tranerror")
64
+ return true
65
+ end
66
+
67
+ def ioerror()
68
+ puts "error"
69
+ raise IOError.new("ioerror")
70
+ return true
71
+ end
72
+
73
+ end
74
+ end
@@ -0,0 +1,50 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "thrift_client"
5
+ require 'thrift'
6
+ require 'foobar/common_service'
7
+ require 'foobar/foo_service'
8
+ require 'foobar/bar_service'
9
+ require 'foobar/handler'
10
+
11
+ module Test
12
+ module Helper
13
+ include Thrift
14
+
15
+ def self.start_server(host = "127.0.0.1", port = 9000)
16
+ common_handler = Test::CommonHandler.new
17
+ common_processor = Test::CommonService::Processor.new(common_handler)
18
+ foo_handler = Test::FooHandler.new
19
+ foo_processor = Test::FooService::Processor.new(foo_handler)
20
+ bar_handler = Test::BarHandler.new
21
+ bar_processor = Test::BarService::Processor.new(bar_handler)
22
+ processor = Thrift::MultiplexedProcessor.new
23
+ processor.register(common_processor.class.name.split("::")[-2].downcase.gsub("service",""), common_processor)
24
+ processor.register(foo_processor.class.name.split("::")[-2].downcase.gsub("service",""), foo_processor)
25
+ processor.register(bar_processor.class.name.split("::")[-2].downcase.gsub("service",""), bar_processor)
26
+ transport = Thrift::ServerSocket.new(host, port)
27
+ transport_factory = Thrift::FramedTransportFactory.new
28
+ protocol_factory = Thrift::CompactProtocolFactory.new
29
+ args = [processor, transport, transport_factory, protocol_factory]
30
+ server = Thrift::NonblockingServer.new(*args)
31
+ @server = server
32
+ sleep(1)
33
+ @server_thread = Thread.new do
34
+ @server.serve
35
+ end
36
+ sleep(1)
37
+ puts "server start listen on #{host}:#{port}"
38
+ end
39
+
40
+ def self.shutdown_server
41
+ return if @server.nil?
42
+ if @server.respond_to? :shutdown
43
+ @server.shutdown
44
+ else
45
+ @server_thread.kill
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,51 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "test/unit"
5
+ require 'thrift'
6
+ require "thrift_client"
7
+ require "passport_thrift_client"
8
+ require "em-synchrony"
9
+ require 'em-synchrony/fiber_iterator'
10
+
11
+ class ProfileTest < Test::Unit::TestCase
12
+ include Test::Unit::Assertions
13
+ def setup
14
+ super
15
+ puts "setup"
16
+ end
17
+
18
+ def test_profile
19
+ EM.synchrony do
20
+ client = ThriftClient.new({"multiplexed" => true, "timeout" => 10, "transport" => "eventmachine","size" => 100,"client_class" => "Passport::Thrift::RemoteUserProfileService::Client","servers" => "vm173:9100"})
21
+ # client.queryUserBasicInfo(3033)
22
+ start = Time.now
23
+ EM::Synchrony::FiberIterator.new(0...2000, 1000).each do |i|
24
+ client.queryUserBasicInfo(3033)
25
+ end
26
+ stop = Time.now
27
+ em_use = stop - start
28
+ puts em_use
29
+ client.destroy
30
+ EM.stop
31
+ end
32
+
33
+ # client = ThriftClient.new({"multiplexed" => true, "timeout" => 10, "transport" => "socket","size"=>1,"client_class" => "Passport::Thrift::RemoteUserProfileService::Client","servers" => "vm173:9100"})
34
+ # i = 0
35
+ # client.queryUserBasicInfo(3033)
36
+ # start = Time.now
37
+ # while i < 10000
38
+ # client.queryUserBasicInfo(3033)
39
+ # i+=1
40
+ # end
41
+ # stop = Time.now
42
+ # em_use = stop - start
43
+ # puts em_use
44
+ # client.destroy
45
+ end
46
+
47
+ def teardown
48
+ super
49
+ puts "teardown"
50
+ end
51
+ end
@@ -0,0 +1,8 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "helper"
5
+ port = ARGV[0]
6
+ Test::Helper.start_server("127.0.0.1",port)
7
+ Marshal.load(STDIN)
8
+
@@ -0,0 +1,27 @@
1
+ class RetryTest
2
+ def test
3
+ puts "method begin"
4
+ try = 0
5
+ begin
6
+ puts "try begin"
7
+ raise "error"
8
+ rescue Exception => e
9
+ puts "Exception"
10
+ if try < 1
11
+ try += 1
12
+ retry
13
+ end
14
+ puts "after retry"
15
+ end
16
+ end
17
+ end
18
+
19
+ RetryTest.new.test
20
+
21
+ object = 2
22
+ case object
23
+ when 2
24
+ puts object
25
+ when 3
26
+ puts object
27
+ end
@@ -0,0 +1,111 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ # $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "test/unit"
5
+ require "thrift_client"
6
+ require "yaml"
7
+ require "feed"
8
+ require 'em-synchrony'
9
+ require 'em-synchrony/fiber_iterator'
10
+ require 'stat-analysis-query'
11
+
12
+ class ClientTest < Test::Unit::TestCase
13
+ include Test::Unit::Assertions
14
+ def setup
15
+ super
16
+ root = File.expand_path("../..", __FILE__)
17
+ @config = YAML.load_file(File.join(root, 'test/thrift_config.yaml'))
18
+ puts "setup"
19
+ end
20
+
21
+ def test_thread_client
22
+ client = ThriftClient.new(@config['client_feed'])
23
+ while 1
24
+ Thread.new {
25
+ client.getWebUnread('3428')
26
+ }
27
+ sleep(0.01)
28
+ end
29
+ end
30
+
31
+ def test_fiber_client
32
+ EM.synchrony do
33
+ client = ThriftClient.new(@config['client_em_feed'])
34
+ start = Time.now
35
+ number = 0
36
+ while number < 10
37
+ EM::Synchrony::FiberIterator.new(0...5, 5).each do |i|
38
+ client.getWebSoundRange('12', 0, 100)
39
+ puts i
40
+ end
41
+ number += 1
42
+ end
43
+ stop = Time.now
44
+ em_use = stop - start
45
+ EM.stop
46
+ end
47
+ end
48
+
49
+ def test_thread_return_nil
50
+ client = ThriftClient.new(@config['client_query'])
51
+ sleep(3)
52
+ begin
53
+ p client.selectWebHumanCategory
54
+ rescue Exception => e
55
+ backtrace = ""
56
+ e.backtrace.each { | s |
57
+ backtrace.concat(s).concat("\n")
58
+ }
59
+ # puts "#{e} #{e.backtrace}"
60
+ puts "#{e.class} #{e.message} #{backtrace}"
61
+ end
62
+ sleep(1)
63
+ begin
64
+ p client.selectWebAlbumCategoryAndTag
65
+ rescue Exception => e
66
+ puts "#{e} #{e.backtrace}"
67
+ end
68
+ begin
69
+ p client.selectWebAlbumCategoryAndTag
70
+ rescue Exception => e
71
+ puts "#{e} #{e.backtrace}"
72
+ end
73
+ end
74
+
75
+ def test_fiber_return_nil
76
+ EM.synchrony do
77
+ client = ThriftClient.new(@config['client_em_query'])
78
+ start = Time.now
79
+ EM::Synchrony::FiberIterator.new(0...1, 1).each do |i|
80
+ begin
81
+ p client.selectWebHumanCategory
82
+ rescue Exception => e
83
+ puts "#{e.class} #{e.message} #{e.backtrace}"
84
+ end
85
+ sleep(1)
86
+ begin
87
+ p client.selectWebAlbumCategoryAndTag
88
+ rescue Exception => e
89
+ backtrace = ""
90
+ e.backtrace.each { | s |
91
+ backtrace.concat(s).concat("\n")
92
+ }
93
+ puts "#{e} #{e.backtrace}"
94
+ end
95
+ begin
96
+ p client.selectWebAlbumCategoryAndTag
97
+ rescue Exception => e
98
+ puts "#{e} #{backtrace}"
99
+ end
100
+ end
101
+ stop = Time.now
102
+ em_use = stop - start
103
+ EM.stop
104
+ end
105
+ end
106
+
107
+ def teardown
108
+ super
109
+ puts "teardown"
110
+ end
111
+ end