dbg_tags 1.1.1 → 1.1.2

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 (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/dbg_tags.rb +36 -4
  3. data/spec/01_tag_spec.rb +41 -9
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d23d3bfe9f58ffa0b26481b6aa296208c6febff871649c197506ee836a54f36e
4
- data.tar.gz: 669f9963e7dcdb9bffceabff6c7f45ba48d7b75c0d7d7293d9c65d16d772eb1d
3
+ metadata.gz: 269e2882bb82fb4a015a17417744067bcbdbd6b98a6f5e799f4f57a332b6eadf
4
+ data.tar.gz: 288f736d512ba40960086f54413d149b2b7558e5223cbf5ff545e15dab79b8f6
5
5
  SHA512:
6
- metadata.gz: 1b2f84b3808a68011ae0a489c294e20f81d10ebc8a99f28d620191dd862b9ae399046ddfd5e9952334730377a11c6b49c8912aaca3a91c9617b24838899b5785
7
- data.tar.gz: db37f12455aef1520cd5a0c1e93dbe2f352b07f3cd345fc92b367e26da649b6eb817e66550b90d64a46ebc9fa7feeb971edd0344292854b756f7e2bff23fab1d
6
+ metadata.gz: 072f1320bae3b4900d895370e17f717f125a721318a6e5a5bc393abef86507b18ca4de3c7b0f39ee4d12aa050dbd6829ceae3eb43bcdd44fe592b39da510114c
7
+ data.tar.gz: cfa63312d3ed1e16ab82d7e73737f6e87b5fe6df12dac8a11d8cec3ad6f0731c6029d43a223ed2d73e7cd2af9e07299175e2c5662854e6d725afaa51cce694ea
data/lib/dbg_tags.rb CHANGED
@@ -187,14 +187,31 @@ module Tag
187
187
 
188
188
  class << self
189
189
 
190
+ private # class methods of Tag
191
+
192
+ # @param value [Bool] Value to set
193
+ # Primarily for rspec stuff. Use {disable_fiber_local_state!} instead
194
+ def no_fiber_local_state= value
195
+ @no_fiber_local_state = value
196
+ end # Tag::no_fiber_local_state=
197
+
190
198
  public # class methods of Tag
191
199
 
192
- # @return [GlobalState] Thread local data
200
+ # @return [Bool,nil] True if we should store global state inside the Tag class itself.
201
+ attr :no_fiber_local_state
202
+ alias no_fiber_local_state? no_fiber_local_state
203
+
204
+ # @return [Bool] True (the default) if we should store global state
205
+ # in each fiber/thread of the application.
206
+ def use_fiber_local_state?; !no_fiber_local_state? end
207
+
208
+ # @return [GlobalState] Either fiber local data (default) or truly global.
193
209
  def global_state
194
- if gs = Thread.current[:dbg_tags_global_state]
195
- gs
210
+ if @no_fiber_local_state # testing undefined ivar here. But no warnings... That is good,
211
+ # at least for performance
212
+ @global_state ||= GlobalState.new
196
213
  else
197
- Thread.current[:dbg_tags_global_state] = GlobalState.new
214
+ Thread.current[:dbg_tags_global_state] ||= GlobalState.new
198
215
  end
199
216
  end # Tag::global_state
200
217
 
@@ -278,5 +295,20 @@ module Tag
278
295
  # @return [bool] Reflects explicit enable calls only. The :all feature is IGNORED
279
296
  def enabled? feature; (global_state.enabled[feature] || NONE) > NONE; end
280
297
 
298
+ # shortcut for no_fiber_local_state := true
299
+ # By default fiber local state is enabled but this means that
300
+ # 'enabling' of tags in some fiber does not work in others.
301
+ # Or more specific, that changing the Tag state outside of any fiber
302
+ # does not effect any fibers (already) created.
303
+ # Now it is possible to transfer data into the fiber using 'resume'
304
+ # but this is a hassle.
305
+ # So: when using threads leave this enabled, as it will cause race conditions.
306
+ # when using fibers, but no threads it is probably convenient to disable it.
307
+ def disable_fiber_local_state; self.no_fiber_local_state = true end
308
+
309
+ # shortcut for no_fiber_local_state := false
310
+ # For rspec use mostly.
311
+ def enable_fiber_local_state; self.no_fiber_local_state = false end
312
+
281
313
  end # singleton class Tag
282
314
  end # module Tag
data/spec/01_tag_spec.rb CHANGED
@@ -255,6 +255,7 @@ describe 'tag' do
255
255
  end # context 'Nested levels'
256
256
 
257
257
  it 'has thread local data to prevent mix ups (tag_300)' do
258
+ Tag.enable_fiber_local_state # NOTE enabled by default, but other examples may botch it
258
259
  t1 = Thread.new do
259
260
  Tag.enable threads: :trc do
260
261
  Tag.trc(:threads) {
@@ -274,44 +275,74 @@ describe 'tag' do
274
275
  end # it
275
276
 
276
277
  it 'each thread has a private tag system (tag_301)' do
278
+ Tag.enable_fiber_local_state # NOTE enabled by default, but other examples may botch it
279
+ executed = false
277
280
  Tag.enable threads: :trc do
278
- expect(Tag.enabled).to eq({threads: 3})
281
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
279
282
  t1 = Thread.new do
280
283
  expect(Tag.enabled).to eq({})
281
284
  Tag.enable threads: :log do
282
- expect(Tag.enabled).to eq({threads: 2})
283
- Tag.trc(:threads) {
285
+ expect(Tag.enabled).to eq({threads: Tag::LOG})
286
+ Tag.log(:threads) {
284
287
  expect(Tag.inside?).to be true
285
288
  sleep 1
289
+ executed = true
286
290
  nil
287
291
  }
288
292
  end
289
293
  end
290
294
  t1.join
291
- expect(Tag.enabled).to eq({threads: 3})
295
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
292
296
  end
297
+ expect(executed).to be true
293
298
  end # it
294
299
 
295
300
  it 'each fiber has a private tag system (tag_302)' do
301
+ Tag.enable_fiber_local_state # NOTE enabled by default, but other examples may botch it
302
+ executed = false
296
303
  Tag.enable threads: :trc do
297
- expect(Tag.enabled).to eq({threads: 3})
304
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
298
305
  t1 = Fiber.new do
299
306
  expect(Tag.enabled).to eq({})
300
307
  Tag.enable threads: :log do
301
- expect(Tag.enabled).to eq({threads: 2})
302
- Tag.trc(:threads) {
308
+ expect(Tag.enabled).to eq({threads: Tag::LOG})
309
+ Tag.log(:threads) {
303
310
  expect(Tag.inside?).to be true
304
- sleep 1
311
+ executed = true
312
+ nil
313
+ }
314
+ end
315
+ end
316
+ t1.resume
317
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
318
+ end
319
+ expect(executed).to be true
320
+ end # it
321
+
322
+ it 'allows to disable fiber local state (tag_305)' do
323
+ Tag.disable_fiber_local_state
324
+ executed = false
325
+ Tag.enable threads: :trc do
326
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
327
+ t1 = Fiber.new do
328
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
329
+ Tag.enable threads: :log do
330
+ expect(Tag.enabled).to eq({threads: Tag::LOG})
331
+ Tag.log(:threads) {
332
+ expect(Tag.inside?).to be true
333
+ executed = true
305
334
  nil
306
335
  }
307
336
  end
308
337
  end
309
338
  t1.resume
310
- expect(Tag.enabled).to eq({threads: 3})
339
+ expect(Tag.enabled).to eq({threads: Tag::TRC})
340
+ expect(executed).to be true
311
341
  end
312
342
  end # it
313
343
 
314
344
  it 'does allow restore_state to transfer state through a Fiber barrier (tag_310)' do
345
+ Tag.enable_fiber_local_state
315
346
  did_something = false
316
347
  Tag.enable example: :dtl, fiber: :trc do
317
348
  state = Tag.state # same as Tag.enabled
@@ -331,6 +362,7 @@ describe 'tag' do
331
362
  end # it
332
363
 
333
364
  it 'does allow a nil-state to transfer state through a Fiber barrier (tag_311)' do
365
+ Tag.enable_fiber_local_state
334
366
  did_something = false
335
367
  state = Tag.state
336
368
  expect(state).to eq({})
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbg_tags
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eugene Brazwick
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-06-04 00:00:00.000000000 Z
11
+ date: 2024-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec