polyphony 0.81 → 0.83

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/test/test_io.rb CHANGED
@@ -279,6 +279,129 @@ 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
370
+ end
371
+
372
+ class IOWithRawBufferTest < MiniTest::Test
373
+ def setup
374
+ super
375
+ @i, @o = IO.pipe
376
+ end
377
+
378
+ def test_write_with_raw_buffer
379
+ Polyphony.__with_raw_buffer__(64) do |b|
380
+ Polyphony.__raw_buffer_set__(b, 'foobar')
381
+ @o << b
382
+ @o.close
383
+ end
384
+
385
+ str = @i.read
386
+ assert_equal 'foobar', str
387
+ end
388
+
389
+ def test_read_with_raw_buffer
390
+ @o << '*' * 65
391
+ @o.close
392
+ chunks = []
393
+ Polyphony.__with_raw_buffer__(64) do |b|
394
+ res = @i.read(64, b)
395
+ assert_equal 64, res
396
+ chunks << Polyphony.__raw_buffer_get__(b, res)
397
+
398
+ res = @i.read(64, b)
399
+ assert_equal 1, res
400
+ assert_equal 64, Polyphony.__raw_buffer_size__(b)
401
+ chunks << Polyphony.__raw_buffer_get__(b, res)
402
+ end
403
+ assert_equal ['*' * 64, '*'], chunks
404
+ end
282
405
  end
283
406
 
284
407
  class IOClassMethodsTest < MiniTest::Test
@@ -468,3 +591,247 @@ class IOClassMethodsTest < MiniTest::Test
468
591
  assert_equal ['foo', 'bar'], buf
469
592
  end
470
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
 
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'helper'
4
+ require 'msgpack'
5
+
6
+ class RawBufferTest < MiniTest::Test
7
+ def test_with_raw_buffer
8
+ result = Polyphony.__with_raw_buffer__(64) do |b|
9
+ assert_kind_of Integer, b
10
+ assert_equal 64, Polyphony.__raw_buffer_size__(b)
11
+ :foo
12
+ end
13
+ assert_equal :foo, result
14
+ end
15
+
16
+ def test_raw_buffer_get_set
17
+ Polyphony.__with_raw_buffer__(64) do |b|
18
+ # should raise if buffer not big enough
19
+ assert_raises { Polyphony.__raw_buffer_set__(b, '*' * 65) }
20
+
21
+ Polyphony.__raw_buffer_set__(b, 'foobar')
22
+ assert_equal 6, Polyphony.__raw_buffer_size__(b)
23
+
24
+ str = Polyphony.__raw_buffer_get__(b)
25
+ assert_equal 'foobar', str
26
+
27
+ str = Polyphony.__raw_buffer_get__(b, 3)
28
+ assert_equal 'foo', str
29
+
30
+ Polyphony.__raw_buffer_set__(b, '')
31
+ assert_equal 0, Polyphony.__raw_buffer_size__(b)
32
+
33
+ str = Polyphony.__raw_buffer_get__(b)
34
+ assert_equal '', str
35
+ end
36
+ end
37
+ end
data/test/test_socket.rb CHANGED
@@ -193,6 +193,56 @@ class SocketTest < MiniTest::Test
193
193
  end
194
194
  end
195
195
 
196
+ class SocketWithRawBufferTest < MiniTest::Test
197
+ def start_tcp_server_on_random_port(host = '127.0.0.1')
198
+ port = rand(1100..60000)
199
+ server = TCPServer.new(host, port)
200
+ [port, server]
201
+ rescue Errno::EADDRINUSE
202
+ retry
203
+ end
204
+
205
+ def setup
206
+ super
207
+
208
+ port, server = start_tcp_server_on_random_port
209
+ connector = spin { @o = TCPSocket.new('127.0.0.1', port) }
210
+ @i = server.accept
211
+ connector.await
212
+ end
213
+
214
+ def test_send_with_raw_buffer
215
+ Polyphony.__with_raw_buffer__(64) do |b|
216
+ Polyphony.__raw_buffer_set__(b, 'foobar')
217
+ @o << b
218
+ @o.close
219
+ end
220
+
221
+ str = @i.read
222
+ assert_equal 'foobar', str
223
+ end
224
+
225
+ def test_recv_with_raw_buffer
226
+ @o << '*' * 65
227
+ @o.close
228
+ chunks = []
229
+ Polyphony.__with_raw_buffer__(64) do |b|
230
+ res = @i.recv(64, 0, b)
231
+ assert_equal 64, res
232
+ chunks << Polyphony.__raw_buffer_get__(b, res)
233
+
234
+ res = @i.recv(64, 0, b)
235
+ assert_equal 1, res
236
+ assert_equal 64, Polyphony.__raw_buffer_size__(b)
237
+ chunks << Polyphony.__raw_buffer_get__(b, res)
238
+
239
+ res = @i.recv(64, 0, b)
240
+ assert_nil res
241
+ end
242
+ assert_equal ['*' * 64, '*'], chunks
243
+ end
244
+ end
245
+
196
246
  if IS_LINUX
197
247
  class HTTPClientTest < MiniTest::Test
198
248
 
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.81'
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-03 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
@@ -232,6 +232,7 @@ files:
232
232
  - examples/core/nested.rb
233
233
  - examples/core/pingpong.rb
234
234
  - examples/core/queue.rb
235
+ - examples/core/raw_buffer_test.rb
235
236
  - examples/core/recurrent-timer.rb
236
237
  - examples/core/resource_delegate.rb
237
238
  - examples/core/ring.rb
@@ -333,9 +334,11 @@ files:
333
334
  - ext/polyphony/event.c
334
335
  - ext/polyphony/extconf.rb
335
336
  - ext/polyphony/fiber.c
337
+ - ext/polyphony/io_extensions.c
336
338
  - ext/polyphony/libev.c
337
339
  - ext/polyphony/libev.h
338
340
  - ext/polyphony/liburing.c
341
+ - ext/polyphony/pipe.c
339
342
  - ext/polyphony/playground.c
340
343
  - ext/polyphony/polyphony.c
341
344
  - ext/polyphony/polyphony.h
@@ -349,6 +352,7 @@ files:
349
352
  - ext/polyphony/runqueue_ring_buffer.h
350
353
  - ext/polyphony/socket_extensions.c
351
354
  - ext/polyphony/thread.c
355
+ - ext/polyphony/zlib_conf.rb
352
356
  - ext/test_eintr.c
353
357
  - lib/polyphony.rb
354
358
  - lib/polyphony/adapters/fs.rb
@@ -375,6 +379,7 @@ files:
375
379
  - lib/polyphony/extensions/kernel.rb
376
380
  - lib/polyphony/extensions/object.rb
377
381
  - lib/polyphony/extensions/openssl.rb
382
+ - lib/polyphony/extensions/pipe.rb
378
383
  - lib/polyphony/extensions/process.rb
379
384
  - lib/polyphony/extensions/socket.rb
380
385
  - lib/polyphony/extensions/thread.rb
@@ -396,8 +401,10 @@ files:
396
401
  - test/test_global_api.rb
397
402
  - test/test_io.rb
398
403
  - test/test_kernel.rb
404
+ - test/test_pipe.rb
399
405
  - test/test_process_supervision.rb
400
406
  - test/test_queue.rb
407
+ - test/test_raw_buffer.rb
401
408
  - test/test_resource_pool.rb
402
409
  - test/test_signal.rb
403
410
  - test/test_socket.rb