Module: ADSP::Test::Common

Defined in:
lib/adsp/test/common.rb

Overview

ADSP::Test::Common module.

Constant Summary collapse

BASE_PATH =
::File.expand_path(::File.join(::File.dirname(__FILE__), "..", "..", "..")).freeze
TEMP_PATH =
::File.join(BASE_PATH, "tmp").freeze
SOURCE_PATH =
::File.join(TEMP_PATH, "source").freeze
ARCHIVE_PATH =
::File.join(TEMP_PATH, "archive").freeze
ENCODINGS =
%w[
  binary
  UTF-8
  UTF-16LE
]
.map { |encoding_name| ::Encoding.find encoding_name }
.freeze
TRANSCODE_OPTIONS =
{
  :invalid => :replace,
  :undef   => :replace,
  :replace => "?"
}
.freeze
TEXTS =
generate_texts(
  "",
  ::SecureRandom.random_bytes(1 << 8) # 256 B
)
.freeze
LARGE_TEXTS =
generate_texts(
  ::SecureRandom.random_bytes(1 << 21) # 2 MB
)
.freeze
PORTION_LENGTHS =

It is better to have text lengths not divisible by portion lengths.

[
  10**2,
  5 * (10**2)
]
.freeze
LARGE_PORTION_LENGTHS =

It is better to have large text lengths not divisible by large portion lengths.

[
  10**6,
  5 * (10**6)
]
.freeze
THREADS_COUNT =

We need at least 2 threads for testing multiple threads support.

[Parallel.processor_count, 2].max.freeze

Class Method Summary collapse

Class Method Details

.file_can_be_used_nonblock?Boolean

Returns:

  • (Boolean)


97
98
99
100
101
102
103
104
105
106
# File 'lib/adsp/test/common.rb', line 97

def self.file_can_be_used_nonblock?
  ::File.open(::Tempfile.new, "w") do |file|
    file.write_nonblock "text"
  end
rescue Errno::EBADF
  # Nonblock operations with files may not be available.
  false
else
  true
end

.generate_texts(*sources) ⇒ Object



45
46
47
48
49
50
51
# File 'lib/adsp/test/common.rb', line 45

def self.generate_texts(*sources)
  sources.flat_map do |source|
    ENCODINGS.map do |encoding|
      source.encode encoding, **TRANSCODE_OPTIONS
    end
  end
end

.get_path(path, suffix) ⇒ Object



26
27
28
# File 'lib/adsp/test/common.rb', line 26

def self.get_path(path, suffix)
  "#{path}_#{suffix}"
end

.parallel(producer, &_block) ⇒ Object



81
82
83
84
85
# File 'lib/adsp/test/common.rb', line 81

def self.parallel(producer, &_block)
  Parallel.each producer, :in_threads => THREADS_COUNT do |item|
    yield item, Parallel.worker_number
  end
end

.parallel_options(generator, &block) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/adsp/test/common.rb', line 87

def self.parallel_options(generator, &block)
  producer = proc do
    next Parallel::Stop if generator.finished?

    generator.next
  end

  parallel producer, &block
end