dtmcli 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 6953ed9f606e7b4fd1f8e3796b2054d933c70d61de91e6f82304da53fed7d5fe
4
+ data.tar.gz: cbecb6cdc192e93192860fa2b715acfa45c3beede2f8b0b93ca3e5440dfd1708
5
+ SHA512:
6
+ metadata.gz: 474abe7b7968720c0bd81182f47541abe82fe99dca08be9ecbca8dffe4eeec063c96de939da79c4b7b51c1d351a859f66c0ae3a1ae7892b56afa171644ee1551
7
+ data.tar.gz: afaf34ffa5f66b7b970c460c4b6d5ffb24042c4f85443bc7ae72a8eebe1f4544cee404e205103c2ca12be78c236c4f916997b34f2780f9c9c443888f3f5f0312
data/.gitignore ADDED
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in broker.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Jed Huo
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,26 @@
1
+ # dtmcli
2
+
3
+ A Ruby SDK for distributed transaction manager [dtm](https://github.com/yedf/dtm)
4
+
5
+ ## Usage
6
+
7
+ ```ruby
8
+ gem install dtmcli
9
+ ```
10
+
11
+ ## Example
12
+ [Business case description](http://dtm.pub/summary/code.html#%E4%BE%8B%E5%AD%90%E8%AF%B4%E6%98%8E)
13
+
14
+ ```ruby
15
+ dtm_url = '127.0.0.1:8080/api/dtm'
16
+ biz_url = '127.0.0.1:3000/api/biz'
17
+
18
+ # TCC version
19
+ res = Dtmcli::Tcc.tcc_global_transaction(dtm_url) do |tcc|
20
+ body = {amount: 30}
21
+ print "calling trans out\n"
22
+ tcc.call_branch(body, biz_url + '/TransOutTry', biz_url + '/TransOutConfirm', biz_url + '/TransOutCancel')
23
+ print "calling trans in\n"
24
+ tcc.call_branch(body, biz_url + '/TransInTry', biz_url + '/TransInConfirm', biz_url + '/TransInCancel')
25
+ end
26
+ ```
data/dtmcli.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'dtmcli/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "dtmcli"
8
+ spec.version = Dtmcli::VERSION
9
+ spec.authors = ["jedhu0"]
10
+ spec.email = ["huoshiqiu@gmail.com"]
11
+ spec.summary = %q{ client for dtm }
12
+ spec.description = %q{ dtm is a lightweight distributed transaction manager }
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'faraday'
22
+
23
+ spec.add_development_dependency "bundler", ">= 2.2.10"
24
+ spec.add_development_dependency 'minitest', '~> 5.8', '>= 5.8.4'
25
+ spec.add_development_dependency 'webmock'
26
+
27
+ end
data/lib/dtmcli.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'faraday'
2
+
3
+ require_relative 'dtmcli/id_generator'
4
+ require_relative 'dtmcli/proxy'
5
+ require_relative 'dtmcli/tcc'
6
+ require_relative 'dtmcli/version'
7
+
8
+ module Dtmcli
9
+ end
@@ -0,0 +1,27 @@
1
+ module Dtmcli
2
+ class IdGenerator
3
+ attr_reader :id_gen
4
+ attr_accessor :parent_id, :branch_id
5
+
6
+ class << self
7
+ def gen_gid(dtmUrl)
8
+ body = Proxy.execute(:get, dtmUrl + '/newGid') do |resp|
9
+ JSON.parse(resp.body)
10
+ end
11
+ return body["gid"]
12
+ end
13
+ end
14
+
15
+ def initialize(parent_id=nil, branch_id=nil)
16
+ @parent_id = parent_id || ''
17
+ @branch_id = branch_id || 0
18
+ end
19
+
20
+ def gen_branch_id
21
+ raise 'branch id is lager than 99' if branch_id >= 99
22
+ raise 'total branch id is longer than 20' if parent_id.size >= 20
23
+ @branch_id = branch_id + 1
24
+ return "#{parent_id}#{branch_id}"
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ module Dtmcli
2
+ module Proxy
3
+ extend self
4
+
5
+ def execute(method, url, opts={}, &block)
6
+ resp = Faraday.send(method, url) do |req|
7
+ req.headers = opts[:headers] || {'Content-Type'=>'application/json'}
8
+ req.params = opts[:params] || {}
9
+ req.body = opts[:body] || {}
10
+ end
11
+
12
+ check_status(resp)
13
+ yield resp if block
14
+ end
15
+
16
+ def check_status(resp)
17
+ code = resp.status
18
+ if code != 200
19
+ raise "Dtmcli rpc error: bad http response status = #{code} resp = #{resp.to_hash}"
20
+ end
21
+ return resp
22
+ end
23
+ end
24
+ end
data/lib/dtmcli/tcc.rb ADDED
@@ -0,0 +1,73 @@
1
+ module Dtmcli
2
+ class Tcc
3
+ attr_accessor :id_gen
4
+ attr_accessor :dtm_url, :gid
5
+
6
+ TRANS_TYPE = 'tcc'
7
+
8
+ class << self
9
+ def tcc_global_transaction(dtm_url, &block)
10
+ gid = IdGenerator.gen_gid(dtm_url)
11
+ tcc = Tcc.new(dtm_url, gid)
12
+
13
+ tbody = {
14
+ gid: gid,
15
+ trans_type: TRANS_TYPE
16
+ }
17
+
18
+ begin
19
+ Proxy.execute(:post, dtm_url + '/prepare', {body: tbody})
20
+ yield tcc if block
21
+
22
+ Proxy.execute(:post, dtm_url + '/submit', {body: tbody})
23
+ rescue => e
24
+ Proxy.execute(:post, dtm_url + '/abort', {body: tbody})
25
+ return ''
26
+ end
27
+
28
+ return tcc.gid
29
+ end
30
+ end
31
+
32
+ def initialize(dtm_url, gid)
33
+ @dtm_url = dtm_url
34
+ @gid = gid
35
+ @id_gen = IdGenerator.new
36
+ end
37
+
38
+ def call_branch(body, try_url, confirm_url, cancel_url)
39
+ branch_id = id_gen.gen_branch_id
40
+
41
+ Proxy.execute(
42
+ :post,
43
+ dtm_url + '/registerTccBranch',
44
+ {
45
+ body: {
46
+ gid: gid,
47
+ branch_id: branch_id,
48
+ trans_type: TRANS_TYPE,
49
+ status: 'prepared',
50
+ data: body.to_json,
51
+ try: try_url,
52
+ confirm: confirm_url,
53
+ cancel: cancel_url
54
+ }
55
+ }
56
+ )
57
+
58
+ Proxy.execute(
59
+ :post,
60
+ try_url,
61
+ {
62
+ body: body,
63
+ params: {
64
+ gid: gid,
65
+ trans_type: TRANS_TYPE,
66
+ branch_id: branch_id,
67
+ branch_type: 'try'
68
+ }
69
+ }
70
+ )
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,3 @@
1
+ module Dtmcli
2
+ VERSION = "0.0.1"
3
+ end
data/test/helper.rb ADDED
@@ -0,0 +1,33 @@
1
+ gem 'minitest'
2
+ require 'minitest/pride'
3
+ require 'minitest/autorun'
4
+ require 'webmock/minitest'
5
+ require_relative '../lib/dtmcli'
6
+
7
+ class Minitest::Test
8
+ def mocking!
9
+ @dtm_host = 'http://127.0.0.1:8080'
10
+ @dtm_url = @dtm_host + '/api/dtm'
11
+
12
+ @host = 'http://127.0.0.1:3000'
13
+ @try_url = @host + '/api/try'
14
+ @confirm_url = @host + '/api/confirm'
15
+ @cancel_url = @host + '/api/cancel'
16
+
17
+ @out_try_url = @host + '/api/TransOutTry'
18
+ @out_confirm_url = @host + '/api/TransOutConfirm'
19
+ @out_cancel_url = @host + '/api/TransOutCancel'
20
+ @in_try_url = @host + '/api/TransInTry'
21
+ @in_confirm_url = @host + '/api/TransInConfirm'
22
+ @in_cancel_url = @host + '/api/TransInCancel'
23
+
24
+ @headers = {
25
+ 'Accept'=>'*/*',
26
+ 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
27
+ 'Content-Type'=>'application/json',
28
+ 'User-Agent'=>'Ruby'
29
+ }
30
+ @protocol_http_success = {dtm_result: 'SUCCESS'}.to_json
31
+ @protocol_http_failure = {dtm_result: 'FAILURE'}.to_json
32
+ end
33
+ end
@@ -0,0 +1,68 @@
1
+ require 'helper'
2
+
3
+ class TestIdGenerator < Minitest::Test
4
+ def setup
5
+ mocking!
6
+ end
7
+
8
+ def test_initialize
9
+ id_gen = Dtmcli::IdGenerator.new
10
+ assert_equal '', id_gen.parent_id
11
+ assert_equal 0, id_gen.branch_id
12
+ end
13
+
14
+ def test_gen_branch_id
15
+ id_gen = Dtmcli::IdGenerator.new
16
+
17
+ id_gen.gen_branch_id
18
+ assert_equal 1, id_gen.branch_id
19
+
20
+ res = id_gen.gen_branch_id
21
+ assert_equal 2, id_gen.branch_id
22
+ assert_equal '2', res
23
+
24
+ id_gen.branch_id = 99
25
+ assert_raises {
26
+ id_gen.gen_branch_id
27
+ }
28
+
29
+ id_gen2 = Dtmcli::IdGenerator.new
30
+ id_gen2.parent_id = '012345678901234567891'
31
+ assert_raises {
32
+ id_gen2.gen_branch_id
33
+ }
34
+ end
35
+
36
+ def test_gen_gid_succ
37
+ stub_request(:get, @dtm_url + "/newGid").
38
+ with(headers: @headers).
39
+ to_return(
40
+ status: 200,
41
+ body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
42
+ headers: {}
43
+ )
44
+
45
+ res = Dtmcli::IdGenerator.gen_gid(@dtm_url)
46
+ assert_equal 'test_gid', res
47
+ end
48
+
49
+ def test_gen_gid_fail
50
+ stub_request(:get, @dtm_url + "/newGid").
51
+ with(headers: @headers).
52
+ to_return(status: 500, body: "Internal Server Error")
53
+
54
+ assert_raises {
55
+ Dtmcli::IdGenerator.gen_gid(@dtm_url)
56
+ }
57
+ end
58
+
59
+ def test_gen_gid_fail2
60
+ stub_request(:get, @dtm_url + "/newGid").
61
+ with(headers: @headers).
62
+ to_return(status: 200, body: '')
63
+
64
+ assert_raises {
65
+ Dtmcli::IdGenerator.gen_gid(@dtm_url)
66
+ }
67
+ end
68
+ end
data/test/test_tcc.rb ADDED
@@ -0,0 +1,353 @@
1
+ require 'helper'
2
+
3
+ class TestTcc < Minitest::Test
4
+ def setup
5
+ mocking!
6
+ end
7
+
8
+ def test_initialize
9
+ tcc = Dtmcli::Tcc.new('dtmurl', 'testgid')
10
+ assert_equal 'dtmurl', tcc.dtm_url
11
+ assert_equal 'testgid', tcc.gid
12
+ assert_equal '', tcc.id_gen.parent_id
13
+ assert_equal 0, tcc.id_gen.branch_id
14
+ end
15
+
16
+ def test_call_branch_succ
17
+ tcc = Dtmcli::Tcc.new(@dtm_url, 'testgid0')
18
+ body = {a: 'a', b: 'b'}
19
+ url_encoded = '?branch_id=1&branch_type=try&gid=testgid0&trans_type=tcc'
20
+
21
+ stub_request(:post, @dtm_url + "/registerTccBranch").
22
+ with(headers: @headers).
23
+ to_return(
24
+ status: 200,
25
+ body: @protocol_http_success
26
+ )
27
+
28
+ stub_request(:post, @try_url + url_encoded).
29
+ with(headers: @headers).
30
+ to_return(
31
+ status: 200,
32
+ body: @protocol_http_success
33
+ )
34
+
35
+ tcc.call_branch(body, @try_url, @confirm_url, @cancel_url)
36
+ assert_equal 1, tcc.id_gen.branch_id
37
+ end
38
+
39
+ def test_call_branch_fail1
40
+ tcc = Dtmcli::Tcc.new(@dtm_url, 'testgid1')
41
+ body = {a: 'a', b: 'b'}
42
+
43
+ stub_request(:post, @dtm_url + "/registerTccBranch").
44
+ with(headers: @headers).
45
+ to_return(
46
+ status: 500,
47
+ body: 'Internal Server Error'
48
+ )
49
+
50
+ assert_raises {
51
+ tcc.call_branch(body, @try_url, @confirm_url, @cancel_url)
52
+ }
53
+ assert_equal 1, tcc.id_gen.branch_id
54
+ end
55
+
56
+ def test_call_branch_fail2
57
+ tcc = Dtmcli::Tcc.new(@dtm_url, 'testgid1')
58
+ body = {a: 'a', b: 'b'}
59
+ url_encoded = '?branch_id=1&branch_type=try&gid=testgid1&trans_type=tcc'
60
+
61
+ stub_request(:post, @dtm_url + "/registerTccBranch").
62
+ with(headers: @headers).
63
+ to_return(
64
+ status: 200,
65
+ body: @protocol_http_success
66
+ )
67
+
68
+ stub_request(:post, @try_url + url_encoded).
69
+ with(headers: @headers).
70
+ to_return(
71
+ status: 500,
72
+ body: 'Internal Server Error'
73
+ )
74
+
75
+ assert_raises {
76
+ tcc.call_branch(body, @try_url, @confirm_url, @cancel_url)
77
+ }
78
+ assert_equal 1, tcc.id_gen.branch_id
79
+ end
80
+
81
+ def test_tcc_global_transaction_succ_with_2_branchs
82
+ gid = 'tcc_global_transaction_succ'
83
+ url_encoded1 = "?branch_id=1&branch_type=try&gid=#{gid}&trans_type=tcc"
84
+ url_encoded2 = "?branch_id=2&branch_type=try&gid=#{gid}&trans_type=tcc"
85
+
86
+ stub_request(:get, @dtm_url + "/newGid").
87
+ with(headers: @headers).
88
+ to_return(
89
+ status: 200,
90
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
91
+ )
92
+
93
+ stub_request(:post, @dtm_url + "/prepare").
94
+ with(headers: @headers).
95
+ to_return(
96
+ status: 200,
97
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
98
+ )
99
+
100
+ stub_request(:post, @dtm_url + "/submit").
101
+ with(headers: @headers).
102
+ to_return(
103
+ status: 200,
104
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
105
+ )
106
+
107
+ stub_request(:post, @dtm_url + "/registerTccBranch").
108
+ with(headers: @headers).
109
+ to_return(
110
+ status: 200,
111
+ body: @protocol_http_success
112
+ )
113
+
114
+ stub_request(:post, @out_try_url + url_encoded1).
115
+ with(headers: @headers).
116
+ to_return(
117
+ status: 200,
118
+ body: @protocol_http_success
119
+ )
120
+
121
+ stub_request(:post, @in_try_url + url_encoded2).
122
+ with(headers: @headers).
123
+ to_return(
124
+ status: 200,
125
+ body: @protocol_http_success
126
+ )
127
+
128
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
129
+ body = {amount: 30}
130
+ print "calling trans out\n"
131
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
132
+ print "calling trans in\n"
133
+ tcc.call_branch(body, @in_try_url, @in_confirm_url, @in_cancel_url)
134
+
135
+ assert_equal 2, tcc.id_gen.branch_id
136
+ end
137
+
138
+ assert_equal gid, res
139
+ end
140
+
141
+ def test_tcc_global_transaction_fail1
142
+ gid = 'tcc_global_transaction_fail1'
143
+
144
+ stub_request(:get, @dtm_url + "/newGid").
145
+ with(headers: @headers).
146
+ to_return(
147
+ status: 500,
148
+ body: 'Internal Server Error'
149
+ )
150
+
151
+ assert_raises {
152
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
153
+ body = {amount: 30}
154
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
155
+ end
156
+ }
157
+ end
158
+
159
+ def test_tcc_global_transaction_fail2
160
+ gid = 'tcc_global_transaction_fail2'
161
+
162
+ stub_request(:get, @dtm_url + "/newGid").
163
+ with(headers: @headers).
164
+ to_return(
165
+ status: 200,
166
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
167
+ )
168
+
169
+ stub_request(:post, @dtm_url + "/prepare").
170
+ with(headers: @headers).
171
+ to_return(
172
+ status: 500,
173
+ body: 'Internal Server Error'
174
+ )
175
+
176
+ stub_request(:post, @dtm_url + "/abort").
177
+ with(headers: @headers).
178
+ to_return(
179
+ status: 200,
180
+ body: @protocol_http_success,
181
+ )
182
+
183
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
184
+ body = {amount: 30}
185
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
186
+ end
187
+
188
+ assert_equal '', res
189
+ end
190
+
191
+ def test_tcc_global_transaction_fail3
192
+ gid = 'tcc_global_transaction_fail3'
193
+
194
+ stub_request(:get, @dtm_url + "/newGid").
195
+ with(headers: @headers).
196
+ to_return(
197
+ status: 200,
198
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
199
+ )
200
+
201
+ stub_request(:post, @dtm_url + "/prepare").
202
+ with(headers: @headers).
203
+ to_return(
204
+ status: 200,
205
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
206
+ )
207
+
208
+ stub_request(:post, @dtm_url + "/abort").
209
+ with(headers: @headers).
210
+ to_return(
211
+ status: 200,
212
+ body: @protocol_http_success,
213
+ )
214
+
215
+ stub_request(:post, @dtm_url + "/registerTccBranch").
216
+ with(headers: @headers).
217
+ to_return(
218
+ status: 500,
219
+ body: 'Internal Server Error'
220
+ )
221
+
222
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
223
+ body = {amount: 30}
224
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
225
+ assert_equal 1, tcc.id_gen.branch_id
226
+ end
227
+
228
+ assert_equal '', res
229
+ end
230
+
231
+ def test_tcc_global_transaction_fail4
232
+ gid = 'tcc_global_transaction_fail4'
233
+ url_encoded1 = "?branch_id=1&branch_type=try&gid=#{gid}&trans_type=tcc"
234
+
235
+ stub_request(:get, @dtm_url + "/newGid").
236
+ with(headers: @headers).
237
+ to_return(
238
+ status: 200,
239
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
240
+ )
241
+
242
+ stub_request(:post, @dtm_url + "/prepare").
243
+ with(headers: @headers).
244
+ to_return(
245
+ status: 200,
246
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
247
+ )
248
+
249
+ stub_request(:post, @dtm_url + "/abort").
250
+ with(headers: @headers).
251
+ to_return(
252
+ status: 200,
253
+ body: @protocol_http_success,
254
+ )
255
+
256
+ stub_request(:post, @dtm_url + "/registerTccBranch").
257
+ with(headers: @headers).
258
+ to_return(
259
+ status: 500,
260
+ body: 'Internal Server Error'
261
+ )
262
+
263
+ stub_request(:post, @dtm_url + "/registerTccBranch").
264
+ with(headers: @headers).
265
+ to_return(
266
+ status: 200,
267
+ body: @protocol_http_success
268
+ )
269
+
270
+ stub_request(:post, @out_try_url + url_encoded1).
271
+ with(headers: @headers).
272
+ to_return(
273
+ status: 500,
274
+ body: 'Internal Server Error'
275
+ )
276
+
277
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
278
+ body = {amount: 30}
279
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
280
+ assert_equal 1, tcc.id_gen.branch_id
281
+ end
282
+
283
+ assert_equal '', res
284
+ end
285
+
286
+ def test_tcc_global_transaction_fail5
287
+ gid = 'tcc_global_transaction_fail5'
288
+ url_encoded1 = "?branch_id=1&branch_type=try&gid=#{gid}&trans_type=tcc"
289
+ url_encoded2 = "?branch_id=2&branch_type=try&gid=#{gid}&trans_type=tcc"
290
+
291
+ stub_request(:get, @dtm_url + "/newGid").
292
+ with(headers: @headers).
293
+ to_return(
294
+ status: 200,
295
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
296
+ )
297
+
298
+ stub_request(:post, @dtm_url + "/prepare").
299
+ with(headers: @headers).
300
+ to_return(
301
+ status: 200,
302
+ body: {dtm_result: 'SUCCESS', gid: gid}.to_json,
303
+ )
304
+
305
+ stub_request(:post, @dtm_url + "/submit").
306
+ with(headers: @headers).
307
+ to_return(
308
+ status: 500,
309
+ body: 'Internal Server Error'
310
+ )
311
+
312
+ stub_request(:post, @dtm_url + "/abort").
313
+ with(headers: @headers).
314
+ to_return(
315
+ status: 200,
316
+ body: @protocol_http_success,
317
+ )
318
+
319
+ stub_request(:post, @dtm_url + "/registerTccBranch").
320
+ with(headers: @headers).
321
+ to_return(
322
+ status: 200,
323
+ body: @protocol_http_success
324
+ )
325
+
326
+ stub_request(:post, @out_try_url + url_encoded1).
327
+ with(headers: @headers).
328
+ to_return(
329
+ status: 200,
330
+ body: @protocol_http_success
331
+ )
332
+
333
+ stub_request(:post, @in_try_url + url_encoded2).
334
+ with(headers: @headers).
335
+ to_return(
336
+ status: 200,
337
+ body: @protocol_http_success
338
+ )
339
+
340
+ res = Dtmcli::Tcc.tcc_global_transaction(@dtm_url) do |tcc|
341
+ body = {amount: 30}
342
+ print "calling trans out\n"
343
+ tcc.call_branch(body, @out_try_url, @out_confirm_url, @out_cancel_url)
344
+ print "calling trans in\n"
345
+ tcc.call_branch(body, @in_try_url, @in_confirm_url, @in_cancel_url)
346
+
347
+ assert_equal 2, tcc.id_gen.branch_id
348
+ end
349
+
350
+ assert_equal '', res
351
+ end
352
+
353
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dtmcli
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - jedhu0
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2021-08-04 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.10
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 2.2.10
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.8'
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: 5.8.4
51
+ type: :development
52
+ prerelease: false
53
+ version_requirements: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - "~>"
56
+ - !ruby/object:Gem::Version
57
+ version: '5.8'
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: 5.8.4
61
+ - !ruby/object:Gem::Dependency
62
+ name: webmock
63
+ requirement: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ type: :development
69
+ prerelease: false
70
+ version_requirements: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - ">="
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ description: " dtm is a lightweight distributed transaction manager "
76
+ email:
77
+ - huoshiqiu@gmail.com
78
+ executables: []
79
+ extensions: []
80
+ extra_rdoc_files: []
81
+ files:
82
+ - ".gitignore"
83
+ - Gemfile
84
+ - LICENSE
85
+ - README.md
86
+ - dtmcli.gemspec
87
+ - lib/dtmcli.rb
88
+ - lib/dtmcli/id_generator.rb
89
+ - lib/dtmcli/proxy.rb
90
+ - lib/dtmcli/tcc.rb
91
+ - lib/dtmcli/version.rb
92
+ - test/helper.rb
93
+ - test/test_id_generator.rb
94
+ - test/test_tcc.rb
95
+ homepage: ''
96
+ licenses:
97
+ - MIT
98
+ metadata: {}
99
+ post_install_message:
100
+ rdoc_options: []
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ required_rubygems_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ requirements: []
114
+ rubygems_version: 3.0.3
115
+ signing_key:
116
+ specification_version: 4
117
+ summary: client for dtm
118
+ test_files:
119
+ - test/helper.rb
120
+ - test/test_id_generator.rb
121
+ - test/test_tcc.rb