Class: ADSP::Test::Stream::Writer

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

Overview

ADSP::Test::Stream::Writer class.

Constant Summary collapse

Target =
Mock::Stream::Writer
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[destination_buffer_length].freeze
BUFFER_LENGTH_MAPPING =
{ :destination_buffer_length => :destination_buffer_length }.freeze
FINISH_MODES =
OCG.new(
  :flush_nonblock => Option::BOOLS,
  :close_nonblock => Option::BOOLS
)
.freeze
NONBLOCK_SERVER_MODES =
{
  :request  => 0,
  :response => 1
}
.freeze
NONBLOCK_SERVER_TIMEOUT =
0.1

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

Constructor Details

#initialize(*args) ⇒ Writer

Returns a new instance of Writer.



48
49
50
51
52
53
# File 'lib/adsp/test/stream/writer.rb', line 48

def initialize(*args)
  super(*args)

  @nonblock_client_lock = ::Mutex.new
  @nonblock_client_id   = 0
end

Instance Method Details

#get_compatible_decompressor_options(compressor_options, &block) ⇒ Object



604
605
606
# File 'lib/adsp/test/stream/writer.rb', line 604

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

#get_invalid_compressor_options(&block) ⇒ Object



596
597
598
# File 'lib/adsp/test/stream/writer.rb', line 596

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

#parallel_compressor_options(&block) ⇒ Object



600
601
602
# File 'lib/adsp/test/stream/writer.rb', line 600

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

#test_encodingObject



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/adsp/test/stream/writer.rb', line 147

def test_encoding
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      # We don't need to transcode between same encodings.
      (ENCODINGS - [text.encoding]).each do |external_encoding|
        target_text = text.encode external_encoding, **TRANSCODE_OPTIONS
        io          = ::StringIO.new

        instance = target.new(
          io,
          compressor_options,
          :external_encoding => external_encoding,
          :transcode_options => TRANSCODE_OPTIONS
        )

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

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

          instance.write text
        ensure
          instance.close
        end

        compressed_text = io.string

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text target_text, compressed_text, decompressor_options
          assert_predicate target_text, :valid_encoding?
        end
      end
    end
  end
end

#test_invalid_initializeObject



55
56
57
58
59
60
61
62
63
# File 'lib/adsp/test/stream/writer.rb', line 55

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

  super
end

#test_invalid_rewind_nonblockObject



392
393
394
395
396
397
398
# File 'lib/adsp/test/stream/writer.rb', line 392

def test_invalid_rewind_nonblock
  instance = target.new Validation::StringIOWithoutWriteNonblock.new

  assert_raises ValidateError do
    instance.rewind_nonblock
  end
end

#test_invalid_writeObject

– synchronous –



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/adsp/test/stream/writer.rb', line 67

def test_invalid_write
  instance = target.new Validation::StringIOWithoutWrite.new

  assert_raises ValidateError do
    instance.write ""
  end

  assert_raises ValidateError do
    instance.flush
  end

  assert_raises ValidateError do
    instance.rewind
  end

  assert_raises ValidateError do
    instance.close
  end
end

#test_invalid_write_nonblockObject

– asynchronous –



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/adsp/test/stream/writer.rb', line 229

def test_invalid_write_nonblock
  instance = target.new Validation::StringIOWithoutWriteNonblock.new

  assert_raises ValidateError do
    instance.write_nonblock ""
  end

  assert_raises ValidateError do
    instance.flush_nonblock
  end

  assert_raises ValidateError do
    instance.close_nonblock
  end
end

#test_rewindObject



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
218
219
220
221
222
223
224
225
# File 'lib/adsp/test/stream/writer.rb', line 186

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

    compressed_texts = []

    ::File.open archive_path, "wb" do |file|
      instance = target.new file, compressor_options

      begin
        TEXTS.each do |text|
          instance.write text
          instance.flush

          assert_equal instance.pos, text.bytesize
          assert_equal instance.pos, instance.tell

          assert_equal 0, instance.rewind

          compressed_texts << ::File.read(archive_path, :mode => "rb")

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

          file.truncate 0
        end
      ensure
        instance.close
      end
    end

    TEXTS.each.with_index do |text, index|
      compressed_text = compressed_texts[index]

      get_compatible_decompressor_options compressor_options do |decompressor_options|
        check_text text, compressed_text, decompressor_options
      end
    end
  end
end

#test_rewind_nonblockObject



400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
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
# File 'lib/adsp/test/stream/writer.rb', line 400

def test_rewind_nonblock
  return unless Common.file_can_be_used_nonblock?

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

    compressed_texts = []

    ::File.open archive_path, "wb" do |file|
      instance = target.new file, compressor_options

      begin
        TEXTS.each do |text|
          instance.write text
          instance.flush

          assert_equal instance.pos, text.bytesize
          assert_equal instance.pos, instance.tell

          loop do
            begin
              is_rewinded = instance.rewind_nonblock
            rescue ::IO::WaitWritable
              file.wait_writable
              retry
            end

            break if is_rewinded
          end

          compressed_texts << ::File.read(archive_path, :mode => "rb")

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

          file.truncate 0
        end
      ensure
        instance.close
      end
    end

    TEXTS.each.with_index do |text, index|
      compressed_text = compressed_texts[index]

      get_compatible_decompressor_options compressor_options do |decompressor_options|
        check_text text, compressed_text, decompressor_options
      end
    end
  end
end

#test_writeObject



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
# File 'lib/adsp/test/stream/writer.rb', line 87

def test_write
  parallel_compressor_options do |compressor_options|
    TEXTS.each do |text|
      PORTION_LENGTHS.each do |portion_length|
        sources  = get_sources text, portion_length
        io       = ::StringIO.new
        instance = target.new io, compressor_options

        begin
          sources.each_slice 2 do |current_sources|
            instance.write(*current_sources)
            instance.flush
          end

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

        compressed_text = io.string

        get_compatible_decompressor_options compressor_options do |decompressor_options|
          check_text text, compressed_text, decompressor_options
        end
      end
    end
  end
end

#test_write_nonblockObject



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
288
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
# File 'lib/adsp/test/stream/writer.rb', line 245

def test_write_nonblock
  nonblock_server do |server|
    parallel_compressor_options do |compressor_options|
      TEXTS.each do |text|
        PORTION_LENGTHS.each do |portion_length|
          sources = get_sources text, portion_length

          FINISH_MODES.each do |finish_mode|
            nonblock_test server, text, portion_length, compressor_options do |instance, socket|
              # write

              sources.each.with_index do |source, index|
                if index.even?
                  loop do
                    begin
                      bytes_written = instance.write_nonblock source
                    rescue ::IO::WaitWritable
                      socket.wait_writable
                      retry
                    end

                    source = source.byteslice bytes_written, source.bytesize - bytes_written
                    break if source.bytesize.zero?
                  end
                else
                  instance.write source
                end
              end

              # flush

              if finish_mode[:flush_nonblock]
                loop do
                  begin
                    is_flushed = instance.flush_nonblock
                  rescue ::IO::WaitWritable
                    socket.wait_writable
                    retry
                  end

                  break if is_flushed
                end
              else
                instance.flush
              end

              assert_equal instance.pos, text.bytesize
              assert_equal instance.pos, instance.tell

            ensure
              # close

              refute_predicate instance, :closed?

              if finish_mode[:close_nonblock]
                loop do
                  begin
                    is_closed = instance.close_nonblock
                  rescue ::IO::WaitWritable
                    socket.wait_writable
                    retry
                  end

                  break if is_closed
                end
              else
                instance.close
              end

              assert_predicate instance, :closed?
            end
          end
        end
      end
    end
  end
end

#test_write_nonblock_with_large_textsObject



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
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
380
381
382
383
384
385
386
387
388
389
390
# File 'lib/adsp/test/stream/writer.rb', line 323

def test_write_nonblock_with_large_texts
  nonblock_server do |server|
    Common.parallel LARGE_TEXTS do |text|
      LARGE_PORTION_LENGTHS.each do |portion_length|
        sources = get_sources text, portion_length

        FINISH_MODES.each do |finish_mode|
          nonblock_test server, text, portion_length do |instance, socket|
            # write

            sources.each.with_index do |source, index|
              if index.even?
                loop do
                  begin
                    bytes_written = instance.write_nonblock source
                  rescue ::IO::WaitWritable
                    socket.wait_writable
                    retry
                  end

                  source = source.byteslice bytes_written, source.bytesize - bytes_written
                  break if source.bytesize.zero?
                end
              else
                instance.write source
              end
            end

            # flush

            if finish_mode[:flush_nonblock]
              loop do
                begin
                  is_flushed = instance.flush_nonblock
                rescue ::IO::WaitWritable
                  socket.wait_writable
                  retry
                end

                break if is_flushed
              end
            else
              instance.flush
            end

          ensure
            # close

            if finish_mode[:close_nonblock]
              loop do
                begin
                  is_closed = instance.close_nonblock
                rescue ::IO::WaitWritable
                  socket.wait_writable
                  retry
                end

                break if is_closed
              end
            else
              instance.close
            end
          end
        end
      end
    end
  end
end

#test_write_with_large_textsObject



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
# File 'lib/adsp/test/stream/writer.rb', line 119

def test_write_with_large_texts
  options_generator = OCG.new(
    :text           => LARGE_TEXTS,
    :portion_length => LARGE_PORTION_LENGTHS
  )

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

    sources  = get_sources text, portion_length
    io       = ::StringIO.new
    instance = target.new io

    begin
      sources.each_slice 2 do |current_sources|
        instance.write(*current_sources)
        instance.flush
      end
    ensure
      instance.close
    end

    compressed_text = io.string
    check_text text, compressed_text
  end
end