polyphony 0.82 → 0.83

Sign up to get free protection for your applications and to get access to all the features.
data/test/test_io.rb CHANGED
@@ -279,6 +279,94 @@ class IOTest < MiniTest::Test
279
279
  sleep 0.01
280
280
  assert_equal ['foo', 'bar', 'baz'], receiver.buffer
281
281
  end
282
+
283
+ def test_splice_from
284
+ i1, o1 = IO.pipe
285
+ i2, o2 = IO.pipe
286
+ len = nil
287
+
288
+ spin {
289
+ len = o2.splice_from(i1, 1000)
290
+ o2.close
291
+ }
292
+
293
+ o1.write('foobar')
294
+ result = i2.read
295
+
296
+ assert_equal 'foobar', result
297
+ assert_equal 6, len
298
+ end
299
+
300
+ def test_splice_class_method
301
+ i1, o1 = IO.pipe
302
+ i2, o2 = IO.pipe
303
+ len = nil
304
+
305
+ spin {
306
+ len = IO.splice(i1, o2, 1000)
307
+ o2.close
308
+ }
309
+
310
+ o1.write('foobar')
311
+ result = i2.read
312
+
313
+ assert_equal 'foobar', result
314
+ assert_equal 6, len
315
+ end
316
+
317
+ def test_splice_to_eof_from
318
+ i1, o1 = IO.pipe
319
+ i2, o2 = IO.pipe
320
+ len = nil
321
+
322
+ f = spin {
323
+ len = o2.splice_to_eof_from(i1, 1000)
324
+ o2.close
325
+ }
326
+
327
+ o1.write('foo')
328
+ result = i2.readpartial(1000)
329
+ assert_equal 'foo', result
330
+
331
+ o1.write('bar')
332
+ result = i2.readpartial(1000)
333
+ assert_equal 'bar', result
334
+ o1.close
335
+ f.await
336
+ assert_equal 6, len
337
+ ensure
338
+ if f.alive?
339
+ f.interrupt
340
+ f.await
341
+ end
342
+ end
343
+
344
+ def test_splice_to_eof_class_method
345
+ i1, o1 = IO.pipe
346
+ i2, o2 = IO.pipe
347
+ len = nil
348
+
349
+ f = spin {
350
+ len = IO.splice_to_eof(i1, o2, 1000)
351
+ o2.close
352
+ }
353
+
354
+ o1.write('foo')
355
+ result = i2.readpartial(1000)
356
+ assert_equal 'foo', result
357
+
358
+ o1.write('bar')
359
+ result = i2.readpartial(1000)
360
+ assert_equal 'bar', result
361
+ o1.close
362
+ f.await
363
+ assert_equal 6, len
364
+ ensure
365
+ if f.alive?
366
+ f.interrupt
367
+ f.await
368
+ end
369
+ end
282
370
  end
283
371
 
284
372
  class IOWithRawBufferTest < MiniTest::Test
@@ -503,3 +591,247 @@ class IOClassMethodsTest < MiniTest::Test
503
591
  assert_equal ['foo', 'bar'], buf
504
592
  end
505
593
  end
594
+
595
+ class IOExtensionsTest < MiniTest::Test
596
+ def test_deflate
597
+ i, o = IO.pipe
598
+ r, w = IO.pipe
599
+
600
+ spin {
601
+ IO.deflate(i, w)
602
+ w.close
603
+ }
604
+
605
+ o << 'foobar' * 20
606
+ o.close
607
+
608
+ data = r.read
609
+ msg = Zlib::Inflate.inflate(data)
610
+ assert_equal 'foobar' * 20, msg
611
+ end
612
+
613
+ def test_inflate
614
+ i, o = IO.pipe
615
+ r, w = IO.pipe
616
+
617
+ spin {
618
+ data = Zlib::Deflate.deflate('foobar', 9)
619
+ o << data
620
+ o.close
621
+ }
622
+
623
+ IO.inflate(i, w)
624
+ w.close
625
+ msg = r.read
626
+ assert_equal 'foobar', msg
627
+ end
628
+
629
+ def test_gzip
630
+ src = Polyphony.pipe
631
+ dest = Polyphony.pipe
632
+ now = nil
633
+
634
+ spin {
635
+ now = Time.now
636
+ IO.gzip(src, dest)
637
+ dest.close
638
+ }
639
+
640
+ src << IO.read(__FILE__)
641
+ src.close
642
+
643
+ gz = Zlib::GzipReader.new(dest)
644
+ data = gz.read
645
+ assert_equal IO.read(__FILE__), data
646
+ assert_in_range (now-1)..(now+1), gz.mtime
647
+ assert_nil gz.orig_name
648
+ assert_nil gz.comment
649
+ end
650
+
651
+ def test_gzip_with_mtime_int
652
+ src = Polyphony.pipe
653
+ dest = Polyphony.pipe
654
+
655
+ spin {
656
+ IO.gzip(src, dest, mtime: 42)
657
+ dest.close
658
+ }
659
+
660
+ src << IO.read(__FILE__)
661
+ src.close
662
+
663
+ gz = Zlib::GzipReader.new(dest)
664
+ data = gz.read
665
+ assert_equal IO.read(__FILE__), data
666
+ assert_equal Time.at(42), gz.mtime
667
+ end
668
+
669
+ def test_gzip_with_mtime_false
670
+ src = Polyphony.pipe
671
+ dest = Polyphony.pipe
672
+
673
+ spin {
674
+ IO.gzip(src, dest, mtime: false)
675
+ dest.close
676
+ }
677
+
678
+ src << IO.read(__FILE__)
679
+ src.close
680
+
681
+ gz = Zlib::GzipReader.new(dest)
682
+ data = gz.read
683
+ assert_equal IO.read(__FILE__), data
684
+ assert_equal Time.at(0), gz.mtime
685
+ end
686
+
687
+ def test_gzip_with_mtime_time
688
+ src = Polyphony.pipe
689
+ dest = Polyphony.pipe
690
+ t = Time.at(Time.now.to_i) - rand(300000)
691
+
692
+ spin {
693
+ IO.gzip(src, dest, mtime: t)
694
+ dest.close
695
+ }
696
+
697
+ src << IO.read(__FILE__)
698
+ src.close
699
+
700
+ gz = Zlib::GzipReader.new(dest)
701
+ data = gz.read
702
+ assert_equal IO.read(__FILE__), data
703
+ assert_equal t, gz.mtime
704
+ end
705
+
706
+ def test_gzip_with_orig_name
707
+ src = Polyphony.pipe
708
+ dest = Polyphony.pipe
709
+
710
+ spin {
711
+ IO.gzip(src, dest, orig_name: '/foo/bar')
712
+ dest.close
713
+ }
714
+
715
+ src << IO.read(__FILE__)
716
+ src.close
717
+
718
+ gz = Zlib::GzipReader.new(dest)
719
+ data = gz.read
720
+ assert_equal IO.read(__FILE__), data
721
+ assert_equal '/foo/bar', gz.orig_name
722
+ end
723
+
724
+ def test_gzip_with_comment
725
+ src = Polyphony.pipe
726
+ dest = Polyphony.pipe
727
+
728
+ spin {
729
+ IO.gzip(src, dest, comment: 'hello!')
730
+ dest.close
731
+ }
732
+
733
+ src << IO.read(__FILE__)
734
+ src.close
735
+
736
+ gz = Zlib::GzipReader.new(dest)
737
+ data = gz.read
738
+ assert_equal IO.read(__FILE__), data
739
+ assert_equal 'hello!', gz.comment
740
+ end
741
+
742
+ def test_gunzip
743
+ src = Polyphony.pipe
744
+ dest = Polyphony.pipe
745
+
746
+ spin {
747
+ IO.gunzip(src, dest)
748
+ dest.close
749
+ }
750
+
751
+ gz = Zlib::GzipWriter.new(src, 9)
752
+ gz << 'foobar'#IO.read(__FILE__)
753
+ gz.close
754
+
755
+ data = dest.read
756
+ # assert_equal IO.read(__FILE__), data
757
+ assert_equal 'foobar', data
758
+ end
759
+
760
+ def test_gunzip_multi
761
+ src1 = Polyphony.pipe
762
+ src2 = Polyphony.pipe
763
+ dest = Polyphony.pipe
764
+
765
+ spin {
766
+ IO.gunzip(src1, dest)
767
+ IO.gunzip(src2, dest)
768
+ dest.close
769
+ }
770
+
771
+ gz1 = Zlib::GzipWriter.new(src1)
772
+ gz1 << 'foobar'
773
+ gz1.close
774
+
775
+ gz1 = Zlib::GzipWriter.new(src2)
776
+ gz1 << 'raboof'
777
+ gz1.close
778
+
779
+ data = dest.read
780
+ assert_equal 'foobarraboof', data
781
+ end
782
+
783
+ def test_gzip_gunzip
784
+ gzipped = Polyphony.pipe
785
+ gunzipped = Polyphony.pipe
786
+
787
+ spin { File.open(__FILE__, 'r') { |f| IO.gzip(f, gzipped) }; gzipped.close }
788
+ spin { IO.gunzip(gzipped, gunzipped); gunzipped.close }
789
+
790
+ data = gunzipped.read
791
+ assert_equal IO.read(__FILE__), data
792
+ end
793
+
794
+ def test_gunzip_with_empty_info
795
+ gzipped = Polyphony.pipe
796
+ gunzipped = Polyphony.pipe
797
+ info = {}
798
+
799
+ spin {
800
+ File.open(__FILE__, 'r') { |f| IO.gzip(f, gzipped, mtime: false) }
801
+ gzipped.close
802
+ }
803
+ spin { IO.gunzip(gzipped, gunzipped, info); gunzipped.close }
804
+
805
+ data = gunzipped.read
806
+ assert_equal IO.read(__FILE__), data
807
+ assert_equal Time.at(0), info[:mtime]
808
+ assert_nil info[:orig_name]
809
+ assert_nil info[:comment]
810
+ end
811
+
812
+ def test_gunzip_with_info
813
+ src = Polyphony.pipe
814
+ gzipped = Polyphony.pipe
815
+ gunzipped = Polyphony.pipe
816
+
817
+ src_info = {
818
+ mtime: 42,
819
+ orig_name: 'foo.bar',
820
+ comment: 'hello!'
821
+ }
822
+
823
+ dest_info = {}
824
+
825
+ spin { IO.gzip(src, gzipped, src_info); gzipped.close }
826
+ spin { IO.gunzip(gzipped, gunzipped, dest_info); gunzipped.close }
827
+
828
+ src << 'foobar'
829
+ src.close
830
+
831
+ data = gunzipped.read
832
+ assert_equal 'foobar', data
833
+ assert_equal Time.at(42), dest_info[:mtime]
834
+ assert_equal 'foo.bar', dest_info[:orig_name]
835
+ assert_equal 'hello!', dest_info[:comment]
836
+ end
837
+ end
data/test/test_pipe.rb ADDED
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+
5
+ class PipeTest < MiniTest::Test
6
+ def test_pipe_creation
7
+ pipe = Polyphony::Pipe.new
8
+
9
+ fds = pipe.fds
10
+ assert_equal 2, fds.size
11
+ assert_kind_of Integer, fds[0]
12
+ assert_kind_of Integer, fds[1]
13
+ assert_equal false, pipe.closed?
14
+ end
15
+
16
+ def test_polyphony_pipe_method
17
+ pipe = Polyphony.pipe
18
+
19
+ fds = pipe.fds
20
+ assert_equal 2, fds.size
21
+ assert_kind_of Integer, fds[0]
22
+ assert_kind_of Integer, fds[1]
23
+ assert_equal false, pipe.closed?
24
+ end
25
+
26
+ def test_pipe_splice
27
+ src = Polyphony::Pipe.new
28
+ dest = Polyphony::Pipe.new
29
+
30
+ spin {
31
+ IO.splice(src, dest, 8192)
32
+ dest.close
33
+ }
34
+
35
+ src << IO.read(__FILE__)
36
+ src.close
37
+
38
+ data = dest.read
39
+ assert_equal IO.read(__FILE__), data
40
+ end
41
+ end
@@ -65,7 +65,7 @@ class ProcessSupervisionTest < MiniTest::Test
65
65
 
66
66
  supervisor = spin { supervise(watcher) }
67
67
 
68
- sleep 0.1
68
+ sleep 0.2
69
69
  supervisor.terminate
70
70
  supervisor.await
71
71
 
data/test/test_socket.rb CHANGED
@@ -208,6 +208,7 @@ class SocketWithRawBufferTest < MiniTest::Test
208
208
  port, server = start_tcp_server_on_random_port
209
209
  connector = spin { @o = TCPSocket.new('127.0.0.1', port) }
210
210
  @i = server.accept
211
+ connector.await
211
212
  end
212
213
 
213
214
  def test_send_with_raw_buffer
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polyphony
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.82'
4
+ version: '0.83'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-04 00:00:00.000000000 Z
11
+ date: 2022-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake-compiler
@@ -334,9 +334,11 @@ files:
334
334
  - ext/polyphony/event.c
335
335
  - ext/polyphony/extconf.rb
336
336
  - ext/polyphony/fiber.c
337
+ - ext/polyphony/io_extensions.c
337
338
  - ext/polyphony/libev.c
338
339
  - ext/polyphony/libev.h
339
340
  - ext/polyphony/liburing.c
341
+ - ext/polyphony/pipe.c
340
342
  - ext/polyphony/playground.c
341
343
  - ext/polyphony/polyphony.c
342
344
  - ext/polyphony/polyphony.h
@@ -350,6 +352,7 @@ files:
350
352
  - ext/polyphony/runqueue_ring_buffer.h
351
353
  - ext/polyphony/socket_extensions.c
352
354
  - ext/polyphony/thread.c
355
+ - ext/polyphony/zlib_conf.rb
353
356
  - ext/test_eintr.c
354
357
  - lib/polyphony.rb
355
358
  - lib/polyphony/adapters/fs.rb
@@ -376,6 +379,7 @@ files:
376
379
  - lib/polyphony/extensions/kernel.rb
377
380
  - lib/polyphony/extensions/object.rb
378
381
  - lib/polyphony/extensions/openssl.rb
382
+ - lib/polyphony/extensions/pipe.rb
379
383
  - lib/polyphony/extensions/process.rb
380
384
  - lib/polyphony/extensions/socket.rb
381
385
  - lib/polyphony/extensions/thread.rb
@@ -397,6 +401,7 @@ files:
397
401
  - test/test_global_api.rb
398
402
  - test/test_io.rb
399
403
  - test/test_kernel.rb
404
+ - test/test_pipe.rb
400
405
  - test/test_process_supervision.rb
401
406
  - test/test_queue.rb
402
407
  - test/test_raw_buffer.rb