Class: ADSP::Test::Stream::Reader

Inherits:
Abstract
  • Object
show all
Defined in:
lib/adsp/test/stream/reader.rb

Overview

ADSP::Test::Stream::Reader class.

Constant Summary collapse

Target =
Mock::Stream::Reader
Option =
Test::Option
String =
Mock::String
ARCHIVE_PATH =
Common::ARCHIVE_PATH
ENCODINGS =
Common::ENCODINGS
TRANSCODE_OPTIONS =
Common::TRANSCODE_OPTIONS
TEXTS =
Common::TEXTS
LARGE_TEXTS =
Common::LARGE_TEXTS
PORTION_LENGTHS =
Common::PORTION_LENGTHS
LARGE_PORTION_LENGTHS =
Common::LARGE_PORTION_LENGTHS
BUFFER_LENGTH_NAMES =
%i[source_buffer_length destination_buffer_length].freeze
BUFFER_LENGTH_MAPPING =
{
  :source_buffer_length      => :destination_buffer_length,
  :destination_buffer_length => :source_buffer_length
}
.freeze

Constants inherited from Abstract

Abstract::SOURCE_PATH

Instance Method Summary collapse

Methods inherited from Abstract

#test_invalid_set_encoding, #test_io_delegates, #test_stat, #test_to_io

Instance Method Details

#get_compatible_decompressor_options(compressor_options, &block) ⇒ Object



637
638
639
# File 'lib/adsp/test/stream/reader.rb', line 637

def get_compatible_decompressor_options(compressor_options, &block)
  option.get_compatible_decompressor_options compressor_options, BUFFER_LENGTH_MAPPING, &block
end

#get_invalid_decompressor_options(&block) ⇒ Object



629
630
631
# File 'lib/adsp/test/stream/reader.rb', line 629

def get_invalid_decompressor_options(&block)
  option.get_invalid_decompressor_options BUFFER_LENGTH_NAMES, &block
end

#optionObject



641
642
643
# File 'lib/adsp/test/stream/reader.rb', line 641

def option
  self.class::Option
end

#parallel_compressor_options(&block) ⇒ Object



633
634
635
# File 'lib/adsp/test/stream/reader.rb', line 633

def parallel_compressor_options(&block)
  Common.parallel_options option.get_compressor_options_generator(BUFFER_LENGTH_NAMES), &block
end

#stringObject



645
646
647
# File 'lib/adsp/test/stream/reader.rb', line 645

def string
  self.class::String
end

#test_encodingObject



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/adsp/test/stream/reader.rb', line 219

def test_encoding
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      external_encoding = text.encoding
      archive           = get_archive text, compressor_options

      PORTION_LENGTHS.each do |portion_length|
        get_compatible_decompressor_options compressor_options do |decompressor_options|
          instance          = target.new ::StringIO.new(archive), decompressor_options
          decompressed_text = "".b

          begin
            result = instance.read 0
            assert_equal Encoding::BINARY, result.encoding

            loop do
              result = instance.read portion_length
              break if result.nil?

              assert_equal Encoding::BINARY, result.encoding
              decompressed_text << result
            end
          ensure
            instance.close
          end

          decompressed_text.force_encoding external_encoding
          assert_equal text, decompressed_text
        end
      end

      # We don't need to transcode between same encodings.
      (ENCODINGS - [external_encoding]).each do |internal_encoding|
        target_text = text.encode internal_encoding, **TRANSCODE_OPTIONS

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          instance = target.new(
            ::StringIO.new(archive),
            decompressor_options,
            :external_encoding => external_encoding,
            :internal_encoding => internal_encoding,
            :transcode_options => TRANSCODE_OPTIONS
          )

          assert_equal external_encoding, instance.external_encoding
          assert_equal internal_encoding, instance.internal_encoding
          assert_equal TRANSCODE_OPTIONS, instance.transcode_options

          decompressed_text = nil

          begin
            instance.set_encoding external_encoding, internal_encoding, TRANSCODE_OPTIONS
            assert_equal external_encoding, instance.external_encoding
            assert_equal internal_encoding, instance.internal_encoding
            assert_equal TRANSCODE_OPTIONS, instance.transcode_options

            decompressed_text = instance.read
            assert_equal internal_encoding, decompressed_text.encoding
          ensure
            instance.close
          end

          assert_equal target_text, decompressed_text
          assert_predicate target_text, :valid_encoding?
        end
      end
    end
  end
end

#test_eofObject



332
333
334
335
336
337
338
339
340
341
342
343
344
345
# File 'lib/adsp/test/stream/reader.rb', line 332

def test_eof
  compressed_text = string.compress "ab"
  instance        = target.new ::StringIO.new(compressed_text)

  refute_predicate instance, :eof?

  byte = instance.read 1
  refute_predicate instance, :eof?
  assert_equal "a", byte

  byte = instance.read 1
  assert_predicate instance, :eof?
  assert_equal "b", byte
end

#test_invalid_eofObject



325
326
327
328
329
330
# File 'lib/adsp/test/stream/reader.rb', line 325

def test_invalid_eof
  assert_raises ValidateError do
    instance = target.new Validation::StringIOWithoutEOF.new
    instance.eof?
  end
end

#test_invalid_initializeObject



40
41
42
43
44
45
46
47
48
# File 'lib/adsp/test/stream/reader.rb', line 40

def test_invalid_initialize
  get_invalid_decompressor_options do |invalid_options|
    assert_raises ValidateError do
      target.new ::StringIO.new, invalid_options
    end
  end

  super
end

#test_invalid_readObject

– synchronous –



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/adsp/test/stream/reader.rb', line 52

def test_invalid_read
  instance = target.new ::StringIO.new

  (Validation::INVALID_NOT_NEGATIVE_INTEGERS - [nil]).each do |invalid_integer|
    assert_raises ValidateError do
      instance.read invalid_integer
    end
  end

  (Validation::INVALID_STRINGS - [nil]).each do |invalid_string|
    assert_raises ValidateError do
      instance.read nil, invalid_string
    end
  end

  assert_raises ValidateError do
    instance = target.new Validation::StringIOWithoutRead.new
    instance.read
  end

  assert_raises ValidateError do
    instance = target.new Validation::StringIOWithoutEOF.new
    instance.read
  end
end

#test_invalid_readpartial_and_read_nonblockObject

– asynchronous –



349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'lib/adsp/test/stream/reader.rb', line 349

def test_invalid_readpartial_and_read_nonblock
  instance = target.new ::StringIO.new

  Validation::INVALID_NOT_NEGATIVE_INTEGERS.each do |invalid_integer|
    assert_raises ValidateError do
      instance.readpartial invalid_integer
    end
    assert_raises ValidateError do
      instance.read_nonblock invalid_integer
    end
  end

  (Validation::INVALID_STRINGS - [nil]).each do |invalid_string|
    assert_raises ValidateError do
      instance.readpartial 0, invalid_string
    end
    assert_raises ValidateError do
      instance.read_nonblock 0, invalid_string
    end
  end

  assert_raises ValidateError do
    instance = target.new Validation::StringIOWithoutReadpartial.new
    instance.readpartial 1
  end

  assert_raises ValidateError do
    instance = target.new Validation::StringIOWithoutReadNonblock.new
    instance.read_nonblock 1
  end
end

#test_readObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/adsp/test/stream/reader.rb', line 78

def test_read
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      archive     = get_archive text, compressor_options
      prev_result = "".b

      Option::BOOLS.each do |with_buffer|
        PORTION_LENGTHS.each do |portion_length|
          get_compatible_decompressor_options compressor_options do |decompressor_options|
            instance          = target.new ::StringIO.new(archive), decompressor_options
            decompressed_text = "".b

            begin
              result = instance.read 0
              assert_equal "", result

              loop do
                prev_eof = instance.eof?

                result =
                  if with_buffer
                    instance.read portion_length, prev_result
                  else
                    instance.read portion_length
                  end

                if result.nil?
                  assert_predicate instance, :eof?
                  break
                end

                refute prev_eof unless archive.bytesize.zero?

                assert_equal prev_result, result if with_buffer
                decompressed_text << result
              end

              assert_equal instance.pos, decompressed_text.bytesize
              assert_equal instance.pos, instance.tell
            ensure
              refute_predicate instance, :closed?
              instance.close
              assert_predicate instance, :closed?
            end

            decompressed_text.force_encoding text.encoding
            assert_equal text, decompressed_text
          end
        end

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          instance          = target.new ::StringIO.new(archive), decompressor_options
          decompressed_text = nil

          begin
            prev_eof = instance.eof?

            if with_buffer
              decompressed_text = instance.read nil, prev_result
              assert_equal prev_result, decompressed_text
            else
              decompressed_text = instance.read
            end

            assert_predicate instance, :eof?
            refute prev_eof unless archive.bytesize.zero?

            assert_equal instance.pos, decompressed_text.bytesize
            assert_equal instance.pos, instance.tell
          ensure
            refute_predicate instance, :closed?
            instance.close
            assert_predicate instance, :closed?
          end

          decompressed_text.force_encoding text.encoding
          assert_equal text, decompressed_text
        end
      end
    end
  end
end

#test_read_nonblockObject



422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/adsp/test/stream/reader.rb', line 422

def test_read_nonblock
  IO.pipe do |read_io, write_io|
    instance = target.new read_io

    assert_raises ::IO::WaitReadable do
      instance.read_nonblock 1
    end

    write_io.close

    assert_raises ::EOFError do
      instance.read_nonblock 1
    end
  end

  nonblock_server do |server|
    parallel_compressor_options do |compressor_options|
      TEXTS.each do |text|
        PORTION_LENGTHS.each do |portion_length|
          nonblock_test server, text, portion_length, compressor_options do |instance, socket|
            decompressed_text = "".b

            loop do
              begin
                decompressed_text << instance.read_nonblock(portion_length)
              rescue ::IO::WaitReadable
                socket.wait_readable
                retry
              rescue ::EOFError
                break
              end

              begin
                decompressed_text << instance.readpartial(portion_length)
              rescue ::EOFError
                break
              end

              result = instance.read portion_length
              break if result.nil?

              decompressed_text << result
            end

            decompressed_text
          end
        end
      end
    end
  end
end

#test_read_nonblock_with_large_textsObject



474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/adsp/test/stream/reader.rb', line 474

def test_read_nonblock_with_large_texts
  nonblock_server do |server|
    Common.parallel LARGE_TEXTS do |text|
      LARGE_PORTION_LENGTHS.each do |portion_length|
        nonblock_test server, text, portion_length do |instance, socket|
          decompressed_text = "".b

          loop do
            begin
              decompressed_text << instance.read_nonblock(portion_length)
            rescue ::IO::WaitReadable
              socket.wait_readable
              retry
            rescue ::EOFError
              break
            end

            begin
              decompressed_text << instance.readpartial(portion_length)
            rescue ::EOFError
              break
            end

            result = instance.read portion_length
            break if result.nil?

            decompressed_text << result
          end

          decompressed_text
        end
      end
    end
  end
end

#test_read_with_large_textsObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/adsp/test/stream/reader.rb', line 161

def test_read_with_large_texts
  options_generator = OCG.new(
    :text        => LARGE_TEXTS,
    :with_buffer => Option::BOOLS
  )

  Common.parallel_options options_generator do |options|
    text        = options[:text]
    with_buffer = options[:with_buffer]

    archive     = get_archive text
    prev_result = "".b

    LARGE_PORTION_LENGTHS.each do |portion_length|
      instance          = target.new ::StringIO.new(archive)
      decompressed_text = "".b

      begin
        loop do
          result =
            if with_buffer
              instance.read portion_length, prev_result
            else
              instance.read portion_length
            end

          break if result.nil?

          assert_equal prev_result, result if with_buffer
          decompressed_text << result
        end
      ensure
        instance.close
      end

      decompressed_text.force_encoding text.encoding
      assert_equal text, decompressed_text
    end

    instance          = target.new ::StringIO.new(archive)
    decompressed_text = nil

    begin
      if with_buffer
        decompressed_text = instance.read nil, prev_result
        assert_equal prev_result, decompressed_text
      else
        decompressed_text = instance.read
      end
    ensure
      instance.close
    end

    decompressed_text.force_encoding text.encoding
    assert_equal text, decompressed_text
  end
end

#test_readpartialObject



381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
# File 'lib/adsp/test/stream/reader.rb', line 381

def test_readpartial
  IO.pipe do |read_io, write_io|
    instance = target.new read_io
    write_io.close

    assert_raises ::EOFError do
      instance.readpartial 1
    end
  end

  nonblock_server do |server|
    parallel_compressor_options do |compressor_options|
      TEXTS.each do |text|
        PORTION_LENGTHS.each do |portion_length|
          Option::BOOLS.each do |with_buffer|
            nonblock_test server, text, portion_length, compressor_options do |instance|
              prev_result       = "".b
              decompressed_text = "".b

              loop do
                if with_buffer
                  result = instance.readpartial portion_length, prev_result
                  assert_equal prev_result, result
                else
                  result = instance.readpartial portion_length
                end

                decompressed_text << result
              rescue ::EOFError
                break
              end

              decompressed_text
            end
          end
        end
      end
    end
  end
end

#test_rewindObject



289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/adsp/test/stream/reader.rb', line 289

def test_rewind
  parallel_compressor_options do |compressor_options, worker_index|
    archive_path = Common.get_path ARCHIVE_PATH, worker_index

    TEXTS.each do |text|
      write_archive archive_path, text, compressor_options

      get_compatible_decompressor_options compressor_options do |decompressor_options|
        decompressed_text = nil

        ::File.open archive_path, "rb" do |file|
          instance = target.new file, decompressor_options

          begin
            result_1 = instance.read

            assert_equal 0, instance.rewind
            assert_equal 0, instance.pos
            assert_equal instance.pos, instance.tell

            result_2 = instance.read
            assert_equal result_1, result_2

            decompressed_text = result_1
          ensure
            instance.close
          end
        end

        decompressed_text.force_encoding text.encoding
        assert_equal text, decompressed_text
      end
    end
  end
end