thrift-client 0.1.0 → 0.3.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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,54 @@
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 "foobar/common_service"
7
+
8
+ class ClientTest < Test::Unit::TestCase
9
+ include Test::Unit::Assertions
10
+
11
+ def setup
12
+ super
13
+ @config = {
14
+ "protocol" => "binary",
15
+ "transport" => "socket",
16
+ "framed" => false,
17
+ "disconnect_exception_classes" => nil,
18
+ "application_exception_classes" => nil,
19
+ "size" => 1,
20
+ "timeout" => 10,
21
+ "multiplexed" => false,
22
+ "client_class" => nil,
23
+ "servers" => ""
24
+ }
25
+ puts "\nsetup"
26
+ end
27
+
28
+ def test_config
29
+ assert_raise ArgumentError do
30
+ client = ThriftClient.new(@config)
31
+ end
32
+ @config["client_class"] = "231"
33
+ assert_raise ArgumentError do
34
+ client = ThriftClient.new(@config)
35
+ end
36
+ @config["client_class"] = "Test::CommonService::"
37
+ assert_raise ArgumentError do
38
+ client = ThriftClient.new(@config)
39
+ end
40
+ @config["client_class"] = "Test::CommonService::Client"
41
+ assert_raise ArgumentError do
42
+ client = ThriftClient.new(@config)
43
+ end
44
+ @config["servers"] = "127.0.0.1:9000"
45
+ assert_nothing_raised ArgumentError do
46
+ client = ThriftClient.new(@config)
47
+ end
48
+ end
49
+
50
+ def teardown
51
+ super
52
+ puts "teardown"
53
+ end
54
+ end
@@ -0,0 +1,43 @@
1
+ $:.unshift File.expand_path("../../lib", __FILE__)
2
+ $:.unshift File.expand_path("../../test", __FILE__)
3
+
4
+ require "test/unit"
5
+ require "thrift_client/connection_pool"
6
+ require "thrift_client/pool/thread_connection_pool"
7
+ require "thrift_client/pool/fiber_connection_pool"
8
+
9
+ class ConnectionPoolTest < Test::Unit::TestCase
10
+ include Test::Unit::Assertions
11
+ def setup
12
+ super
13
+ puts "setup"
14
+ end
15
+
16
+ def test_thread_connection_pool
17
+ pool = ThreadConnectionPool.new(:size => 5){
18
+ # sleep(1)
19
+ Time.now.to_s
20
+ }
21
+ threads = Array.new(200){
22
+ Thread.new {
23
+ # sleep(1)
24
+ pool.execute { |conn|
25
+ puts conn
26
+ # sleep(1)
27
+ }
28
+ }
29
+ }
30
+ sleep(5)
31
+ # threads.each { | thread |
32
+ # p thread
33
+ # thread.run
34
+ # # thread.join
35
+ # }
36
+ # Thread.main.join
37
+ end
38
+
39
+ def teardown
40
+ super
41
+ puts "teardown"
42
+ end
43
+ end
@@ -0,0 +1,146 @@
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 'thrift'
7
+ require 'yaml'
8
+ require "foobar/handler"
9
+ require "helper"
10
+ require 'em-synchrony'
11
+ require 'em-synchrony/fiber_iterator'
12
+
13
+ class EventMachineTransprotTest < Test::Unit::TestCase
14
+ include Test::Unit::Assertions
15
+ def setup
16
+ super
17
+ root = File.expand_path("../..", __FILE__)
18
+ @config = YAML.load_file(File.join(root, 'test/thrift_config.yaml'))
19
+ puts "setup"
20
+ end
21
+
22
+ def test_eventmachine_transport_performence
23
+ em_use = 0
24
+ no_em_use = 0
25
+ EM.synchrony do
26
+ client = ThriftClient.new(@config['em_test'])
27
+ start = Time.now
28
+ EM::Synchrony::FiberIterator.new(0...100, 50).each do |i|
29
+ client.wait(1)
30
+ end
31
+ stop = Time.now
32
+ em_use = stop - start
33
+ EM.stop
34
+ end
35
+ EM.synchrony do
36
+ client = ThriftClient.new(@config['no_em_test'])
37
+ start = Time.now
38
+ EM::Synchrony::FiberIterator.new(0...100, 50).each do |i|
39
+ client.wait(1)
40
+ end
41
+ stop = Time.now
42
+ no_em_use = stop - start
43
+ EM.stop
44
+ end
45
+ # assert(em_use < no_em_use)
46
+ puts "eventmachine use #{em_use}"
47
+ puts "no eventmachine use #{no_em_use}"
48
+ end
49
+
50
+ def test_eventmachine_transport_availability
51
+ EM.synchrony do
52
+ client = ThriftClient.new(@config['em_availability_test'])
53
+ client.getBar
54
+ client.getBar
55
+ EM.stop
56
+ end
57
+ EM.synchrony do
58
+ client = ThriftClient.new(@config['em_availability_test'])
59
+ start = Time.now
60
+ EM::Synchrony::FiberIterator.new(0...1000, 50).each do |i|
61
+ puts "do"
62
+ bar = Test::Bar.new
63
+ bar.a = i.to_s
64
+ bar.b = i
65
+ bar.c = true
66
+ bar.d = i.to_f
67
+ bar.e = i
68
+ ret = client.putBar bar
69
+ assert(ret)
70
+ end
71
+ stop = Time.now
72
+ em_use = stop - start
73
+ puts em_use
74
+ EM.stop
75
+ end
76
+
77
+ EM.synchrony do
78
+ client = ThriftClient.new(@config['em_availability_test'])
79
+ start = Time.now
80
+ EM::Synchrony::FiberIterator.new(0...1000, 50).each do |i|
81
+ bar = client.getBar
82
+ assert_equal("1111111111",bar.a)
83
+ assert_equal(11111111,bar.b)
84
+ assert_equal(false,bar.c)
85
+ assert_equal(1111111.232323,bar.d)
86
+ assert_equal(111111111111111,bar.e)
87
+ end
88
+ stop = Time.now
89
+ em_use = stop - start
90
+ puts em_use
91
+ EM.stop
92
+ end
93
+ end
94
+
95
+ def test_eventmachine_reconnect
96
+ puts "should start new server on port 9001"
97
+ # sleep(5)
98
+ EM.synchrony do
99
+ client = ThriftClient.new(@config['em_availability_test'])
100
+
101
+ assert_nothing_raised do
102
+ bar = client.getBar
103
+ puts bar.inspect
104
+ assert_equal("1111111111",bar.a)
105
+ assert_equal(11111111,bar.b)
106
+ assert_equal(false,bar.c)
107
+ assert_equal(1111111.232323,bar.d)
108
+ assert_equal(111111111111111,bar.e)
109
+ end
110
+
111
+ puts "should shutdown new server on port 9001"
112
+ sleep(3)
113
+
114
+ assert_raise Thrift::TransportException do
115
+ client.getBar
116
+ end
117
+
118
+ puts "should start new server on port 9001"
119
+ sleep(5)
120
+
121
+ assert_nothing_raised do
122
+ client.getBar
123
+ end
124
+ EM.stop
125
+ end
126
+ end
127
+
128
+ def test_timer
129
+ puts "should start new server on port 9001"
130
+ # sleep(5)
131
+ EM.synchrony do
132
+ client = ThriftClient.new(@config['em_test'])
133
+ i = 0
134
+ # EM::Synchrony::FiberIterator.new(0...1000, 1000).each do |i|
135
+ client.timeout
136
+ # end
137
+ EM.stop
138
+ end
139
+ end
140
+
141
+ def teardown
142
+ super
143
+ # Test::Helper.shutdown_server
144
+ puts "teardown"
145
+ end
146
+ end
@@ -0,0 +1,62 @@
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 "foobar/handler"
8
+ require "helper"
9
+ require 'yaml'
10
+
11
+ class MultiplexedTest < Test::Unit::TestCase
12
+ include Test::Unit::Assertions
13
+ def setup
14
+ super
15
+ root = File.expand_path("../..", __FILE__)
16
+ @config = YAML.load_file(File.join(root, 'test/thrift_config.yaml'))
17
+ Test::Helper.start_server
18
+ puts "setup"
19
+ end
20
+
21
+ def test_multiplexed
22
+ client = ThriftClient.new(@config['multiplexed_test'])
23
+ puts "test start"
24
+
25
+ foo = client.getFoo
26
+ assert_equal(200000, foo.first)
27
+ assert_equal(222222222222222, foo.second)
28
+ assert_equal(2.2, foo.third)
29
+ assert_equal("222", foo.fourth)
30
+
31
+ assert_raise do
32
+ client.ping
33
+ end
34
+ puts "test stop"
35
+ end
36
+
37
+ def test_error_multiplexed
38
+ client = ThriftClient.new(@config['multiplexed_error_test'])
39
+ assert_raise do
40
+ client.getFoo
41
+ end
42
+ end
43
+
44
+ def test_profile
45
+ client = ThriftClient.new({"transport" => "eventmachine","size"=>50,"client_class" => "Passport::Thrift::RemoteUserProfileService::Client","servers" => "192.168.1.173:9100"})
46
+ EM.synchrony do
47
+ start = Time.now
48
+ EM::Synchrony::FiberIterator.new(0...100, 50).each do |i|
49
+ client.queryUserBasicInfo(3033)
50
+ end
51
+ stop = Time.now
52
+ em_use = stop - start
53
+ EM.stop
54
+ end
55
+ end
56
+
57
+ def teardown
58
+ super
59
+ Test::Helper.shutdown_server
60
+ puts "teardown"
61
+ end
62
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: thrift-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ted Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-09-24 00:00:00.000000000 Z
11
+ date: 2014-04-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thrift
@@ -16,34 +16,48 @@ dependencies:
16
16
  requirements:
17
17
  - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 0.9.0
19
+ version: 0.9.1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 0.9.0
26
+ version: 0.9.1
27
27
  description: Thrift client
28
28
  email: ted@ximalaya.com
29
29
  executables: []
30
30
  extensions: []
31
31
  extra_rdoc_files: []
32
32
  files:
33
- - lib/thrift_client/abstract_server.rb
34
- - lib/thrift_client/abstract_thrift_client.rb
35
- - lib/thrift_client/multi_client_server.rb
36
- - lib/thrift_client/single_client_server.rb
37
- - lib/thrift_client/thrift.rb
38
33
  - lib/thrift_client.rb
39
- - test/client_test.rb
40
- - test/foobar_constants.rb
41
- - test/foobar_service.rb
42
- - test/foobar_types.rb
43
- - test/multiplexed_protocol_test.rb
44
- - test/multiplexed_server.rb
34
+ - lib/thrift_client/abstract_thrift_client.rb
35
+ - lib/thrift_client/pool.rb
36
+ - lib/thrift_client/pool/fiber_connection_pool.rb
37
+ - lib/thrift_client/pool/thread_connection_pool.rb
38
+ - lib/thrift_client/server.rb
39
+ - lib/thrift_client/thrift/client.rb
40
+ - lib/thrift_client/thrift/processor.rb
41
+ - lib/thrift_client/thrift/protocol.rb
42
+ - lib/thrift_client/thrift/struct.rb
43
+ - lib/thrift_client/thrift/transport.rb
44
+ - test/em_test.rb
45
+ - test/foobar/bar_service.rb
46
+ - test/foobar/common_service.rb
47
+ - test/foobar/foo_service.rb
48
+ - test/foobar/foobar_constants.rb
49
+ - test/foobar/foobar_types.rb
50
+ - test/foobar/handler.rb
51
+ - test/helper.rb
45
52
  - test/options_config_test.rb
46
- - test/server.rb
53
+ - test/passport.rb
54
+ - test/start_server.rb
55
+ - test/test.rb
56
+ - test/test_client.rb
57
+ - test/test_config.rb
58
+ - test/test_connection_pool.rb
59
+ - test/test_eventmachine_transprot.rb
60
+ - test/test_multiplexed.rb
47
61
  homepage: http://www.ximalaya.com
48
62
  licenses:
49
63
  - MIT2.0
@@ -64,16 +78,25 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
78
  version: '0'
65
79
  requirements: []
66
80
  rubyforge_project:
67
- rubygems_version: 2.0.3
81
+ rubygems_version: 2.2.2
68
82
  signing_key:
69
83
  specification_version: 4
70
84
  summary: A Thrift client wrapper that encapsulates some common behavior.
71
85
  test_files:
72
- - test/client_test.rb
73
- - test/foobar_constants.rb
74
- - test/foobar_service.rb
75
- - test/foobar_types.rb
76
- - test/multiplexed_protocol_test.rb
77
- - test/multiplexed_server.rb
86
+ - test/em_test.rb
87
+ - test/foobar/bar_service.rb
88
+ - test/foobar/common_service.rb
89
+ - test/foobar/foo_service.rb
90
+ - test/foobar/foobar_constants.rb
91
+ - test/foobar/foobar_types.rb
92
+ - test/foobar/handler.rb
93
+ - test/helper.rb
78
94
  - test/options_config_test.rb
79
- - test/server.rb
95
+ - test/passport.rb
96
+ - test/start_server.rb
97
+ - test/test.rb
98
+ - test/test_client.rb
99
+ - test/test_config.rb
100
+ - test/test_connection_pool.rb
101
+ - test/test_eventmachine_transprot.rb
102
+ - test/test_multiplexed.rb
@@ -1,59 +0,0 @@
1
- require 'thread'
2
- require 'timeout'
3
- =begin
4
- multi client connection thrift server encapsulation
5
- =end
6
- class MultiClientServer < AbstractServer
7
-
8
- def initialize(connect_string, options = {})
9
- super
10
- @pool_timeout = @options[:pool_timeout]
11
- @pool = Array.new(@options[:size]) { client = create_client }
12
- @mutex = Mutex.new
13
- @resource = ConditionVariable.new
14
- end
15
-
16
- def active?
17
- active = false
18
- @pool.each {|client| active = client.open?; break if active}
19
- return active
20
- end
21
-
22
- def destroy
23
- @pool.each {|client| client.close if client}
24
- end
25
-
26
- def get_client
27
- client = pop
28
- if @test_on_borrow
29
- if check_client(client)
30
- return client
31
- else
32
- return nil
33
- end
34
- else
35
- client
36
- end
37
- end
38
-
39
- def return_client(client)
40
- client = create_client unless client.open?
41
- @mutex.synchronize do
42
- @pool.push client
43
- @resource.broadcast
44
- end
45
- end
46
-
47
- private
48
- def pop
49
- deadline = Time.now + @pool_timeout
50
- @mutex.synchronize do
51
- loop do
52
- return @pool.pop unless @pool.empty?
53
- to_wait = deadline - Time.now
54
- raise Timeout::Error, "Waited #{@pool_timeout} seconds" if to_wait <= 0 and @pool_timeout > 0
55
- @resource.wait(@mutex, to_wait)
56
- end
57
- end
58
- end
59
- end