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.



43
44
45
46
47
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 43

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

Instance Attribute Details

#argsObject (readonly)



25
26
27
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 25

def args
  @args
end

#blockObject (readonly)



35
36
37
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 35

def block
  @block
end

#nObject (readonly)

Returns the value of attribute n.



15
16
17
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 15

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.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/convenient_service/rspec/primitive_helpers/classes/in_threads.rb', line 59

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