Class: ConvenientService::RSpec::PrimitiveHelpers::Classes::InThreads

Inherits:
Support::Command
  • Object
show all
Defined in:
lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(n, *args, &block) ⇒ void

Parameters:

  • n (Integer)

    Amount of threads.

  • args (Array<Object>)

    Args passed to Thread.new.

  • block (Proc, nil)

    Block passed to Thread.new.



48
49
50
51
52
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 48

def initialize(n, *args, &block)
  @n = n
  @args = args
  @block = block
end

Instance Attribute Details

#argsObject (readonly)



30
31
32
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 30

def args
  @args
end

#blockObject (readonly)



40
41
42
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 40

def block
  @block
end

#nObject (readonly)

Returns the value of attribute n.



20
21
22
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 20

def n
  @n
end

Instance Method Details

#callArray<Object>

Returns Thread values. Can be any type.

Returns:

  • (Array<Object>)

    Thread values. Can be any type.



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 64

def call
  threads = []

  ##
  # NOTE: CRuby has GIL, so the specs are almost always successful for simple cases.
  # NOTE: JRuby or TruffleRuby use real threads, so NOT thread-safe code starts to fail even for the simplest cases.
  #
  n.times { threads << ::Thread.new(*args, &block) }

  threads.map(&:value)
end