dtmcli 0.0.2 → 0.0.3
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 +4 -4
- data/README.md +15 -1
- data/lib/dtmcli.rb +3 -0
- data/lib/dtmcli/dtm.rb +52 -0
- data/lib/dtmcli/errors.rb +3 -0
- data/lib/dtmcli/id_generator.rb +2 -5
- data/lib/dtmcli/saga.rb +36 -0
- data/lib/dtmcli/tcc.rb +17 -21
- data/lib/dtmcli/version.rb +1 -1
- data/test/test_id_generator.rb +10 -0
- data/test/test_saga.rb +136 -0
- metadata +7 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: bc819432a1d2b76dcf9081bad4b185394857bafdb9a658b54e5d3798de909636
|
4
|
+
data.tar.gz: 0e2fb36a0b38c4ecbaf9a96b40ee3b7ace2716b41ba8e54f4a5c171fd36bf7b6
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d674552f78936d9f1e04f3c86ca407f70577d789222544c579b08e8bba6efbd6cebff9bc3eedf5aa1a80387284c5d1911490d9d4f770a6c3eb8f110e52b3974e
|
7
|
+
data.tar.gz: 12732c4356bdc4b2e7ba801f438f87a295fb74a525d792914c09febb19f03a0fe39a15985e4ad329116038e4be92cfc6577b248e51fc6b80b3e737b80c092415
|
data/README.md
CHANGED
@@ -15,7 +15,7 @@ gem install dtmcli
|
|
15
15
|
dtm_url = '127.0.0.1:8080/api/dtm'
|
16
16
|
biz_url = '127.0.0.1:3000/api/biz'
|
17
17
|
|
18
|
-
# TCC
|
18
|
+
# Model TCC
|
19
19
|
res = Dtmcli::Tcc.tcc_global_transaction(dtm_url) do |tcc|
|
20
20
|
body = {amount: 30}
|
21
21
|
print "calling trans out\n"
|
@@ -23,4 +23,18 @@ res = Dtmcli::Tcc.tcc_global_transaction(dtm_url) do |tcc|
|
|
23
23
|
print "calling trans in\n"
|
24
24
|
tcc.call_branch(body, biz_url + '/TransInTry', biz_url + '/TransInConfirm', biz_url + '/TransInCancel')
|
25
25
|
end
|
26
|
+
|
27
|
+
# Model Saga
|
28
|
+
saga = Dtmcli::Saga.new(dtm_url)
|
29
|
+
saga.gen_gid
|
30
|
+
|
31
|
+
post_data = {
|
32
|
+
amount: 30,
|
33
|
+
transInResult: "SUCCESS",
|
34
|
+
transOutResult: "SUCCESS",
|
35
|
+
}
|
36
|
+
saga.add(biz_url + '/TransOut', biz_url + '/TransOutRevert', post_data)
|
37
|
+
saga.add(biz_url + '/TransIn', biz_url + '/TransInRevert', post_data)
|
38
|
+
|
39
|
+
saga.submit
|
26
40
|
```
|
data/lib/dtmcli.rb
CHANGED
@@ -1,7 +1,10 @@
|
|
1
1
|
require 'faraday'
|
2
2
|
|
3
|
+
require_relative 'dtmcli/dtm'
|
4
|
+
require_relative 'dtmcli/errors'
|
3
5
|
require_relative 'dtmcli/id_generator'
|
4
6
|
require_relative 'dtmcli/proxy'
|
7
|
+
require_relative 'dtmcli/saga'
|
5
8
|
require_relative 'dtmcli/tcc'
|
6
9
|
require_relative 'dtmcli/version'
|
7
10
|
|
data/lib/dtmcli/dtm.rb
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module Dtmcli
|
2
|
+
class Dtm
|
3
|
+
attr_reader :dtm_url
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def succ?(data)
|
7
|
+
data['dtm_result'] == 'SUCCESS'
|
8
|
+
end
|
9
|
+
|
10
|
+
def parse
|
11
|
+
lambda do |resp|
|
12
|
+
body = JSON.parse(resp.body)
|
13
|
+
raise DtmSvrError, "dtm server error: data = #{body}" if !succ?(body)
|
14
|
+
body
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def new_gid(dtm_url)
|
19
|
+
data = Proxy.execute(:get, dtm_url + '/newGid', {}, &parse)
|
20
|
+
return data["gid"]
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def initialize(dtm_url)
|
25
|
+
@dtm_url = dtm_url
|
26
|
+
end
|
27
|
+
|
28
|
+
def prepare(body)
|
29
|
+
Proxy.execute(:post, dtm_url + '/prepare', {body: body}, &Dtm::parse)
|
30
|
+
end
|
31
|
+
|
32
|
+
def submit(body)
|
33
|
+
Proxy.execute(:post, dtm_url + '/submit', {body: body}, &Dtm::parse)
|
34
|
+
end
|
35
|
+
|
36
|
+
def abort(body)
|
37
|
+
Proxy.execute(:post, dtm_url + '/abort', {body: body}, &Dtm::parse)
|
38
|
+
end
|
39
|
+
|
40
|
+
def register_xa_branch(body)
|
41
|
+
Proxy.execute(:post, dtm_url + '/registerXaBranch',{body: body}, &Dtm::parse)
|
42
|
+
end
|
43
|
+
|
44
|
+
def register_tcc_branch(body)
|
45
|
+
Proxy.execute(:post, dtm_url + '/registerTccBranch',{body: body}, &Dtm::parse)
|
46
|
+
end
|
47
|
+
|
48
|
+
def query(params)
|
49
|
+
Proxy.execute(:get, dtm_url + '/query',{params: params}, &Dtm::parse)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/lib/dtmcli/id_generator.rb
CHANGED
@@ -4,11 +4,8 @@ module Dtmcli
|
|
4
4
|
attr_accessor :parent_id, :branch_id
|
5
5
|
|
6
6
|
class << self
|
7
|
-
def gen_gid(
|
8
|
-
|
9
|
-
JSON.parse(resp.body)
|
10
|
-
end
|
11
|
-
return body["gid"]
|
7
|
+
def gen_gid(dtm_url)
|
8
|
+
return Dtm.new_gid(dtm_url)
|
12
9
|
end
|
13
10
|
end
|
14
11
|
|
data/lib/dtmcli/saga.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Dtmcli
|
2
|
+
class Saga
|
3
|
+
attr_accessor :gid, :steps
|
4
|
+
attr_reader :dtm, :dtm_url
|
5
|
+
|
6
|
+
TRANS_TYPE = 'saga'
|
7
|
+
|
8
|
+
def initialize(dtm_url)
|
9
|
+
@dtm_url = dtm_url
|
10
|
+
@dtm = Dtm.new(dtm_url)
|
11
|
+
@steps = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def gen_gid
|
15
|
+
@gid = IdGenerator.gen_gid(dtm_url)
|
16
|
+
end
|
17
|
+
|
18
|
+
def add(action, compensate, post_data)
|
19
|
+
step = {
|
20
|
+
action: action,
|
21
|
+
compensate: compensate,
|
22
|
+
data: post_data.to_json,
|
23
|
+
}
|
24
|
+
@steps << step
|
25
|
+
end
|
26
|
+
|
27
|
+
def submit
|
28
|
+
tbody = {
|
29
|
+
gid: gid,
|
30
|
+
trans_type: TRANS_TYPE,
|
31
|
+
steps: steps
|
32
|
+
}
|
33
|
+
dtm.submit(tbody)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/dtmcli/tcc.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
module Dtmcli
|
2
2
|
class Tcc
|
3
|
-
attr_accessor :id_gen
|
4
|
-
|
3
|
+
attr_accessor :id_gen, :gid
|
4
|
+
attr_reader :dtm, :dtm_url
|
5
5
|
|
6
6
|
TRANS_TYPE = 'tcc'
|
7
7
|
|
@@ -16,12 +16,13 @@ module Dtmcli
|
|
16
16
|
}
|
17
17
|
|
18
18
|
begin
|
19
|
-
|
19
|
+
tcc.dtm.prepare(tbody)
|
20
|
+
|
20
21
|
yield tcc if block
|
21
22
|
|
22
|
-
|
23
|
+
tcc.dtm.submit(tbody)
|
23
24
|
rescue => e
|
24
|
-
|
25
|
+
tcc.dtm.abort(tbody)
|
25
26
|
return ''
|
26
27
|
end
|
27
28
|
|
@@ -33,27 +34,22 @@ module Dtmcli
|
|
33
34
|
@dtm_url = dtm_url
|
34
35
|
@gid = gid
|
35
36
|
@id_gen = IdGenerator.new
|
37
|
+
@dtm = Dtm.new(dtm_url)
|
36
38
|
end
|
37
39
|
|
38
40
|
def call_branch(body, try_url, confirm_url, cancel_url)
|
39
41
|
branch_id = id_gen.gen_branch_id
|
40
42
|
|
41
|
-
|
42
|
-
:
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
try: try_url,
|
52
|
-
confirm: confirm_url,
|
53
|
-
cancel: cancel_url
|
54
|
-
}
|
55
|
-
}
|
56
|
-
)
|
43
|
+
dtm.register_tcc_branch({
|
44
|
+
gid: gid,
|
45
|
+
branch_id: branch_id,
|
46
|
+
trans_type: TRANS_TYPE,
|
47
|
+
status: 'prepared',
|
48
|
+
data: body.to_json,
|
49
|
+
try: try_url,
|
50
|
+
confirm: confirm_url,
|
51
|
+
cancel: cancel_url
|
52
|
+
})
|
57
53
|
|
58
54
|
Proxy.execute(
|
59
55
|
:post,
|
data/lib/dtmcli/version.rb
CHANGED
data/test/test_id_generator.rb
CHANGED
@@ -65,4 +65,14 @@ class TestIdGenerator < Minitest::Test
|
|
65
65
|
Dtmcli::IdGenerator.gen_gid(@dtm_url)
|
66
66
|
}
|
67
67
|
end
|
68
|
+
|
69
|
+
def test_gen_gid_fail3
|
70
|
+
stub_request(:get, @dtm_url + "/newGid").
|
71
|
+
with(headers: @headers).
|
72
|
+
to_return(status: 200, body: {dtm_result: 'FAILURE', gid: ''}.to_json)
|
73
|
+
|
74
|
+
assert_raises(Dtmcli::DtmSvrError) {
|
75
|
+
Dtmcli::IdGenerator.gen_gid(@dtm_url)
|
76
|
+
}
|
77
|
+
end
|
68
78
|
end
|
data/test/test_saga.rb
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSaga < Minitest::Test
|
4
|
+
def setup
|
5
|
+
mocking!
|
6
|
+
end
|
7
|
+
|
8
|
+
def test_initialize
|
9
|
+
saga = Dtmcli::Saga.new('dtmurl')
|
10
|
+
assert_equal 'dtmurl', saga.dtm_url
|
11
|
+
assert_equal [], saga.steps
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_gen_gid_succ
|
15
|
+
stub_request(:get, @dtm_url + "/newGid").
|
16
|
+
with(headers: @headers).
|
17
|
+
to_return(
|
18
|
+
status: 200,
|
19
|
+
body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
|
20
|
+
headers: {}
|
21
|
+
)
|
22
|
+
|
23
|
+
saga = Dtmcli::Saga.new(@dtm_url)
|
24
|
+
saga.gen_gid
|
25
|
+
assert_equal 'test_gid', saga.gid
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_gen_gid_fail
|
29
|
+
stub_request(:get, @dtm_url + "/newGid").
|
30
|
+
with(headers: @headers).
|
31
|
+
to_return(status: 200, body: {dtm_result: 'FAILURE', gid: ''}.to_json)
|
32
|
+
|
33
|
+
saga = Dtmcli::Saga.new(@dtm_url)
|
34
|
+
assert_raises(Dtmcli::DtmSvrError) {
|
35
|
+
saga.gen_gid
|
36
|
+
}
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_add_succ
|
40
|
+
stub_request(:get, @dtm_url + "/newGid").
|
41
|
+
with(headers: @headers).
|
42
|
+
to_return(
|
43
|
+
status: 200,
|
44
|
+
body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
|
45
|
+
headers: {}
|
46
|
+
)
|
47
|
+
|
48
|
+
saga = Dtmcli::Saga.new(@dtm_url)
|
49
|
+
assert_equal [], saga.steps
|
50
|
+
saga.gen_gid
|
51
|
+
|
52
|
+
post_data = {
|
53
|
+
amount: 30,
|
54
|
+
transInResult: "SUCCESS",
|
55
|
+
transOutResult: "SUCCESS",
|
56
|
+
}
|
57
|
+
saga.add(@host + '/TransOut1', @host + '/TransOutRevert1', post_data)
|
58
|
+
assert_equal 1, saga.steps.size
|
59
|
+
step1 = saga.steps[0]
|
60
|
+
assert_equal @host + '/TransOut1', step1[:action]
|
61
|
+
assert_equal @host + '/TransOutRevert1', step1[:compensate]
|
62
|
+
assert step1[:data].include?('SUCCESS')
|
63
|
+
|
64
|
+
saga.add(@host + '/TransOut2', @host + '/TransOutRevert2', post_data)
|
65
|
+
assert_equal 2, saga.steps.size
|
66
|
+
step2 = saga.steps[1]
|
67
|
+
assert_equal @host + '/TransOut2', step2[:action]
|
68
|
+
assert_equal @host + '/TransOutRevert2', step2[:compensate]
|
69
|
+
assert step1[:data].include?('SUCCESS')
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_submit_succ
|
73
|
+
stub_request(:get, @dtm_url + "/newGid").
|
74
|
+
with(headers: @headers).
|
75
|
+
to_return(
|
76
|
+
status: 200,
|
77
|
+
body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
|
78
|
+
headers: {}
|
79
|
+
)
|
80
|
+
|
81
|
+
stub_request(:post, @dtm_url + "/submit").
|
82
|
+
with(headers: @headers).
|
83
|
+
to_return(
|
84
|
+
status: 200,
|
85
|
+
body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
|
86
|
+
)
|
87
|
+
|
88
|
+
saga = Dtmcli::Saga.new(@dtm_url)
|
89
|
+
assert_equal [], saga.steps
|
90
|
+
saga.gen_gid
|
91
|
+
|
92
|
+
post_data = {
|
93
|
+
amount: 30,
|
94
|
+
transInResult: "SUCCESS",
|
95
|
+
transOutResult: "SUCCESS",
|
96
|
+
}
|
97
|
+
saga.add(@host + '/TransOut', @host + '/TransOutRevert', post_data)
|
98
|
+
assert_equal 1, saga.steps.size
|
99
|
+
|
100
|
+
res = saga.submit
|
101
|
+
assert_equal 'SUCCESS', res['dtm_result']
|
102
|
+
end
|
103
|
+
|
104
|
+
def test_submit_succ
|
105
|
+
stub_request(:get, @dtm_url + "/newGid").
|
106
|
+
with(headers: @headers).
|
107
|
+
to_return(
|
108
|
+
status: 200,
|
109
|
+
body: {dtm_result: 'SUCCESS', gid: 'test_gid'}.to_json,
|
110
|
+
headers: {}
|
111
|
+
)
|
112
|
+
|
113
|
+
stub_request(:post, @dtm_url + "/submit").
|
114
|
+
with(headers: @headers).
|
115
|
+
to_return(
|
116
|
+
status: 200,
|
117
|
+
body: {dtm_result: 'FAILURE', gid: ''}.to_json,
|
118
|
+
)
|
119
|
+
|
120
|
+
saga = Dtmcli::Saga.new(@dtm_url)
|
121
|
+
assert_equal [], saga.steps
|
122
|
+
saga.gen_gid
|
123
|
+
|
124
|
+
post_data = {
|
125
|
+
amount: 30,
|
126
|
+
transInResult: "SUCCESS",
|
127
|
+
transOutResult: "SUCCESS",
|
128
|
+
}
|
129
|
+
saga.add(@host + '/TransOut', @host + '/TransOutRevert', post_data)
|
130
|
+
assert_equal 1, saga.steps.size
|
131
|
+
|
132
|
+
assert_raises(Dtmcli::DtmSvrError) {
|
133
|
+
saga.submit
|
134
|
+
}
|
135
|
+
end
|
136
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dtmcli
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jedhu0
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-08-
|
11
|
+
date: 2021-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: faraday
|
@@ -85,12 +85,16 @@ files:
|
|
85
85
|
- README.md
|
86
86
|
- dtmcli.gemspec
|
87
87
|
- lib/dtmcli.rb
|
88
|
+
- lib/dtmcli/dtm.rb
|
89
|
+
- lib/dtmcli/errors.rb
|
88
90
|
- lib/dtmcli/id_generator.rb
|
89
91
|
- lib/dtmcli/proxy.rb
|
92
|
+
- lib/dtmcli/saga.rb
|
90
93
|
- lib/dtmcli/tcc.rb
|
91
94
|
- lib/dtmcli/version.rb
|
92
95
|
- test/helper.rb
|
93
96
|
- test/test_id_generator.rb
|
97
|
+
- test/test_saga.rb
|
94
98
|
- test/test_tcc.rb
|
95
99
|
homepage: https://github.com/jedhu0/dtmcli
|
96
100
|
licenses:
|
@@ -118,4 +122,5 @@ summary: client for dtm
|
|
118
122
|
test_files:
|
119
123
|
- test/helper.rb
|
120
124
|
- test/test_id_generator.rb
|
125
|
+
- test/test_saga.rb
|
121
126
|
- test/test_tcc.rb
|