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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 06e6dfcf106062c1fb84355a92286dd8a150adea
4
- data.tar.gz: 6ecef6ecc87eeceb42e1a44e693ec061ca7109ac
3
+ metadata.gz: 5c313933ddbf8bc22bfb69240a8447044cb03fd4
4
+ data.tar.gz: 3ec79fe55b174797095f8bda5a01a8df52e5272f
5
5
  SHA512:
6
- metadata.gz: a57cb90c9249979d227e2b9a704572609022a280b37f79901dc83382e2331b87c865c5ecd56333bbfa525bdd029e736bfb1f594a01d1af751e8f5a356e79183d
7
- data.tar.gz: e7386eb960f36c4fb240c993b4c2251f58bc67ff2cc06d30ceb52576cce4bf0e50914a43b26a0a52709b548defe4876a6e630080e9d0e686615d47401d8441c0
6
+ metadata.gz: b0f42ad92dcdbfba42102b977b2b9437c5c8ba29da1549b75b513e0322d13f56586c2884a68d918d25772b95dc3f05c27cc8af6d4f88f9748d3a4357357dd39c
7
+ data.tar.gz: 73fadebb9cb5d67dff05d0587f49f3c3d44d04fac5f7410d40919760aedf67fa0c605d15e0a42da34ee33987b73df347d615f001a99be58acefbbc889e0a131a
@@ -1,3 +1,8 @@
1
+ 1.5.1
2
+ -----
3
+
4
+ - Threadsafe strategy needed to included the persistence strategy
5
+
1
6
  1.5.0
2
7
  -----
3
8
 
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- lifen (1.5.0)
4
+ lifen (1.5.1)
5
5
  faraday (>= 0.9)
6
6
  inflecto
7
7
  virtus (>= 1.0)
@@ -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
- yield(self) if block_given?
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
@@ -1,3 +1,3 @@
1
1
  module Lifen
2
- VERSION = "1.5.0"
2
+ VERSION = "1.5.1"
3
3
  end
@@ -78,20 +78,47 @@ describe Lifen::Token do
78
78
 
79
79
  describe 'thread safe' do
80
80
 
81
- let(:user) { Lifen::User.new }
81
+ class PersistedUser < Struct.new(:token_value, :token_expires_at) ; end
82
82
 
83
- let(:expired_token) { Lifen::Token.new(user: user, value: "valid_token", expires_at: Time.now.to_i - 60) }
84
- let(:other_expired_token) { Lifen::Token.new(user: user, value: "valid_token", expires_at: Time.now.to_i - 60) }
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
- sleep 1
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 do |token|
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 do |token|
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.0
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 00:00:00.000000000 Z
11
+ date: 2017-01-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler