lifen 1.5.0 → 1.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +1 -1
- data/lib/lifen/token.rb +6 -2
- data/lib/lifen/version.rb +1 -1
- data/spec/token_spec.rb +39 -16
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5c313933ddbf8bc22bfb69240a8447044cb03fd4
|
4
|
+
data.tar.gz: 3ec79fe55b174797095f8bda5a01a8df52e5272f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b0f42ad92dcdbfba42102b977b2b9437c5c8ba29da1549b75b513e0322d13f56586c2884a68d918d25772b95dc3f05c27cc8af6d4f88f9748d3a4357357dd39c
|
7
|
+
data.tar.gz: 73fadebb9cb5d67dff05d0587f49f3c3d44d04fac5f7410d40919760aedf67fa0c605d15e0a42da34ee33987b73df347d615f001a99be58acefbbc889e0a131a
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
data/lib/lifen/token.rb
CHANGED
@@ -10,6 +10,9 @@ module Lifen
|
|
10
10
|
attribute :value, String
|
11
11
|
attribute :expires_at, Integer
|
12
12
|
|
13
|
+
attribute :load_from_db, Proc
|
14
|
+
attribute :save_to_db, Proc
|
15
|
+
|
13
16
|
def to_s
|
14
17
|
value
|
15
18
|
end
|
@@ -44,12 +47,13 @@ module Lifen
|
|
44
47
|
def refresh_once_if_needed
|
45
48
|
@@lock.synchronize do
|
46
49
|
|
47
|
-
|
50
|
+
load_from_db.call(self) if load_from_db.is_a? Proc
|
48
51
|
|
49
52
|
return if active?
|
50
|
-
|
51
53
|
refresh
|
52
54
|
|
55
|
+
save_to_db.call(self) if save_to_db.is_a? Proc
|
56
|
+
|
53
57
|
raise Error, "Token can't be refreshed" if !active?
|
54
58
|
end
|
55
59
|
end
|
data/lib/lifen/version.rb
CHANGED
data/spec/token_spec.rb
CHANGED
@@ -78,20 +78,47 @@ describe Lifen::Token do
|
|
78
78
|
|
79
79
|
describe 'thread safe' do
|
80
80
|
|
81
|
-
|
81
|
+
class PersistedUser < Struct.new(:token_value, :token_expires_at) ; end
|
82
82
|
|
83
|
-
let(:
|
84
|
-
let(:
|
83
|
+
let(:user) { Lifen::User.new }
|
84
|
+
let(:persisted_user) { PersistedUser.new("expired_token", Time.now.to_i - 60) }
|
85
|
+
|
86
|
+
let(:expired_token) {
|
87
|
+
Lifen::Token.new( user: user,
|
88
|
+
value: persisted_user.token_value,
|
89
|
+
expires_at: persisted_user.token_expires_at,
|
90
|
+
load_from_db: ->(token) {
|
91
|
+
# Loading data from DB
|
92
|
+
|
93
|
+
token.value = persisted_user.token_value
|
94
|
+
token.expires_at = persisted_user.token_expires_at
|
95
|
+
},
|
96
|
+
save_to_db: ->(token) {
|
97
|
+
# Loading data from DB
|
98
|
+
|
99
|
+
persisted_user.token_value = token.value
|
100
|
+
persisted_user.token_expires_at = token.expires_at
|
101
|
+
}
|
102
|
+
) }
|
103
|
+
|
104
|
+
|
105
|
+
let(:other_expired_token) {
|
106
|
+
Lifen::Token.new( user: user,
|
107
|
+
value: persisted_user.token_value,
|
108
|
+
expires_at: persisted_user.token_expires_at,
|
109
|
+
load_from_db: ->(token) {
|
110
|
+
# Loading data from DB
|
111
|
+
|
112
|
+
token.value = persisted_user.token_value
|
113
|
+
token.expires_at = persisted_user.token_expires_at
|
114
|
+
}
|
115
|
+
) }
|
85
116
|
|
86
117
|
before do
|
87
118
|
allow(expired_token).to receive(:refresh) do
|
88
|
-
|
119
|
+
expired_token.value = "valid_token"
|
89
120
|
expired_token.expires_at = Time.now.to_i + 60
|
90
121
|
end
|
91
|
-
|
92
|
-
allow(other_expired_token).to receive(:refresh) do
|
93
|
-
sleep 1
|
94
|
-
end
|
95
122
|
end
|
96
123
|
|
97
124
|
it 'queues refresh calls' do
|
@@ -101,17 +128,13 @@ describe Lifen::Token do
|
|
101
128
|
threads = []
|
102
129
|
|
103
130
|
threads << Thread.new do
|
104
|
-
expired_token.refresh_once_if_needed
|
105
|
-
token.value = "expired_token"
|
106
|
-
token.expires_at = Time.now.to_i - 60
|
107
|
-
end
|
131
|
+
expired_token.refresh_once_if_needed
|
108
132
|
end
|
109
133
|
|
134
|
+
sleep 0.1 # Making sure the mocked call is received first
|
135
|
+
|
110
136
|
threads << Thread.new do
|
111
|
-
other_expired_token.refresh_once_if_needed
|
112
|
-
token.value = "valid_token"
|
113
|
-
token.expires_at = Time.now.to_i + 60
|
114
|
-
end
|
137
|
+
other_expired_token.refresh_once_if_needed
|
115
138
|
end
|
116
139
|
|
117
140
|
threads.map { |t| t.join } # waiting for all threads to finish
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lifen
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.5.
|
4
|
+
version: 1.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Etienne Depaulis
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-01-
|
11
|
+
date: 2017-01-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|