xchan.rb 0.17.1 → 0.17.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5509107cf1cda5e847f3c15e19c81ba9323c2dbb524a7ef808eeb820d5bcdeb9
4
- data.tar.gz: 43deff543ee38ace3ca8a083a043eb1c6bbedec9143bd2c8e17de50f13127031
3
+ metadata.gz: 9ce716f918994e2481e8fbd333dbdb7e0b258341a09e32d464a54a2331edbd09
4
+ data.tar.gz: f17d86bf4004ca1ebef87950ff9d20c7aaad29940e8c031ff84d817ade3adeb9
5
5
  SHA512:
6
- metadata.gz: d5455dd6c714bc4060ec1f563b46d8a9eaee32e5aa58c2a2508de99f53fc29e346d07ac04e719822bc394b66ac6ba6ddf5585bf7958c606a0f1f375c113b4310
7
- data.tar.gz: '09bd28506d18a63e8e917ad1189c708cc6643b852707508ee0edf563064bd3618d3ae251d85e06f618672c414537b4da3db0538b61e4c11c2a6c64fff3394f38'
6
+ metadata.gz: c77e364ea0f6365d4fddae810cd14e999cf584dd301e5a651a5565d78fcce157909f0b96a155f67f8f22bb50f1c7d5c2535653e4871f2e5dab862c1e38f207f3
7
+ data.tar.gz: a4f2d57cf1843407d7056c4528d7e41372ed84378e33f6ca2777b6b2dde93b80fe5e57d33d2b01337a089d808b11e3e6d92c91b4f57f86aeefc4a3e4782c6afa
data/README.md CHANGED
@@ -52,13 +52,14 @@ the parent process writes to the channel:
52
52
  require "xchan"
53
53
 
54
54
  ch = xchan(:marshal)
55
- Process.detach fork {
55
+ fork do
56
56
  print "Received a random number (child process): ", ch.recv, "\n"
57
- }
57
+ end
58
58
  sleep(1)
59
59
  print "Send a random number (from parent process)", "\n"
60
60
  ch.send(rand(21))
61
61
  ch.close
62
+ Process.wait
62
63
 
63
64
  ##
64
65
  # Send a random number (from parent process)
data/lib/xchan/bytes.rb CHANGED
@@ -16,7 +16,7 @@ class Chan::Bytes
16
16
  #
17
17
  # @return [Chan::Bytes]
18
18
  def initialize(tmpdir)
19
- @io = Chan.temporary_file("xchan.bytes", tmpdir:)
19
+ @io = Chan.temporary_file(%w[bytes .json], tmpdir:)
20
20
  @io.sync = true
21
21
  write(@io, [])
22
22
  end
data/lib/xchan/counter.rb CHANGED
@@ -13,7 +13,7 @@ class Chan::Counter
13
13
  #
14
14
  # @return [Chan::Counter]
15
15
  def initialize(tmpdir)
16
- @io = Chan.temporary_file("xchan.counter", tmpdir:)
16
+ @io = Chan.temporary_file(%w[counter .json], tmpdir:)
17
17
  write(@io, {"bytes_read" => 0, "bytes_written" => 0})
18
18
  end
19
19
 
@@ -17,6 +17,12 @@ class Chan::UNIXSocket
17
17
  # Returns a socket used for write operations
18
18
  attr_reader :w
19
19
 
20
+ ##
21
+ # @return [<#dump, #load>]
22
+ # Returns the serializer used by the channel
23
+ attr_reader :s
24
+ alias_method :serializer, :s
25
+
20
26
  ##
21
27
  # @example
22
28
  # ch = Chan::UNIXSocket.new(:marshal)
@@ -24,7 +30,7 @@ class Chan::UNIXSocket
24
30
  # ch.recv.pop # => 3
25
31
  # ch.close
26
32
  #
27
- # @param [Symbol, <#dump, #load>] serializer
33
+ # @param [Symbol, <#dump, #load>] s
28
34
  # The name of a serializer
29
35
  #
30
36
  # @param [Integer] sock_type
@@ -35,19 +41,12 @@ class Chan::UNIXSocket
35
41
  #
36
42
  # @return [Chan::UNIXSocket]
37
43
  # Returns an instance of {Chan::UNIXSocket Chan::UNIXSocket}
38
- def initialize(serializer, sock_type: Socket::SOCK_DGRAM, tmpdir: Dir.tmpdir)
39
- @serializer = Chan.serializers[serializer]&.call || serializer
44
+ def initialize(s, sock_type: Socket::SOCK_DGRAM, tmpdir: Dir.tmpdir)
45
+ @s = Chan.shortcuts[s]&.call || s
40
46
  @r, @w = ::UNIXSocket.pair(sock_type)
41
47
  @bytes = Chan::Bytes.new(tmpdir)
42
48
  @counter = Chan::Counter.new(tmpdir)
43
- @lock = LockFile.new Chan.temporary_file("xchan.lock", tmpdir:)
44
- end
45
-
46
- ##
47
- # @return [<#dump, #load>]
48
- # Returns the serializer used by the channel
49
- def serializer
50
- @serializer
49
+ @lock = LockFile.new Chan.temporary_file(%w[xchan .lock], tmpdir:)
51
50
  end
52
51
 
53
52
  ##
@@ -61,7 +60,7 @@ class Chan::UNIXSocket
61
60
  # Closes the channel
62
61
  #
63
62
  # @raise [IOError]
64
- # When the channel is closed.
63
+ # When the channel is closed
65
64
  #
66
65
  # @return [void]
67
66
  def close
@@ -229,7 +228,7 @@ class Chan::UNIXSocket
229
228
 
230
229
  ##
231
230
  # @return [Integer]
232
- # Returns the number of objects waiting to be read.
231
+ # Returns the number of objects waiting to be read
233
232
  def size
234
233
  lock { @bytes.size }
235
234
  end
@@ -241,25 +240,25 @@ class Chan::UNIXSocket
241
240
  # @group Wait methods
242
241
 
243
242
  ##
244
- # Waits for the channel to become readable.
243
+ # Waits for the channel to become readable
245
244
  #
246
245
  # @param [Float, Integer, nil] s
247
- # The number of seconds to wait. Waits indefinitely when "nil".
246
+ # The number of seconds to wait. Waits indefinitely with no arguments.
248
247
  #
249
248
  # @return [Chan::UNIXSocket, nil]
250
- # Returns self when the channel is readable, otherwise returns nil.
249
+ # Returns self when the channel is readable, otherwise returns nil
251
250
  def wait_readable(s = nil)
252
251
  @r.wait_readable(s) and self
253
252
  end
254
253
 
255
254
  ##
256
- # Waits for the channel to become writable.
255
+ # Waits for the channel to become writable
257
256
  #
258
257
  # @param [Float, Integer, nil] s
259
- # The number of seconds to wait. Waits indefinitely when "nil".
258
+ # The number of seconds to wait. Waits indefinitely with no arguments.
260
259
  #
261
260
  # @return [Chan::UNIXSocket, nil]
262
- # Returns self when the channel is writable, otherwise returns nil.
261
+ # Returns self when the channel is writable, otherwise returns nil
263
262
  def wait_writable(s = nil)
264
263
  @w.wait_writable(s) and self
265
264
  end
@@ -277,10 +276,10 @@ class Chan::UNIXSocket
277
276
  end
278
277
 
279
278
  def serialize(obj)
280
- @serializer.dump(obj)
279
+ @s.dump(obj)
281
280
  end
282
281
 
283
282
  def deserialize(str)
284
- @serializer.load(str)
283
+ @s.load(str)
285
284
  end
286
285
  end
data/lib/xchan/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chan
4
- VERSION = "0.17.1"
4
+ VERSION = "0.17.2"
5
5
  end
data/lib/xchan.rb CHANGED
@@ -47,8 +47,8 @@ module Chan
47
47
 
48
48
  ##
49
49
  # @return [Hash<Symbol, Proc>]
50
- # A mapping of serializers
51
- def self.serializers
50
+ # Maps a short name to a serializer
51
+ def self.shortcuts
52
52
  {
53
53
  pure: lambda { Pure },
54
54
  marshal: lambda { Marshal },
@@ -72,11 +72,11 @@ module Kernel
72
72
  # ch.recv.pop # => 3
73
73
  # ch.close
74
74
  #
75
- # @param serializer (see Chan::UNIXSocket#initialize)
75
+ # @param s (see Chan::UNIXSocket#initialize)
76
76
  # @param sock_type (see Chan::UNIXSocket#initialize)
77
77
  # @param tmpdir (see Chan::UNIXSocket#initialize)
78
78
  # @return (see Chan::UNIXSocket#initialize)
79
- def xchan(serializer, **kw_args)
80
- Chan::UNIXSocket.new(serializer, **kw_args)
79
+ def xchan(s, **kw_args)
80
+ Chan::UNIXSocket.new(s, **kw_args)
81
81
  end
82
82
  end
@@ -5,13 +5,14 @@ require "xchan"
5
5
 
6
6
  $stdout.sync = true
7
7
  ch = xchan(:marshal)
8
- Process.detach fork {
8
+ fork do
9
9
  print "Received random number (child process): ", ch.recv, "\n"
10
- }
10
+ end
11
11
  sleep(1)
12
12
  print "Send a random number (from parent process)", "\n"
13
13
  ch.send(rand(21))
14
14
  ch.close
15
+ Process.wait
15
16
 
16
17
  ##
17
18
  # Send a random number (from parent process)
data/test/xchan_test.rb CHANGED
@@ -194,3 +194,32 @@ class Chan::BytesReadTest < Chan::Test
194
194
  assert_equal object_size * 2, ch.bytes_read
195
195
  end
196
196
  end
197
+
198
+ ##
199
+ # Chan.temporary_file
200
+ class Chan::TemporaryFileTest < Chan::Test
201
+ def test_temporary_file_mode
202
+ assert_equal 0, file.stat.mode & 0o777
203
+ ensure
204
+ file.close
205
+ end
206
+
207
+ def test_temporary_file_path
208
+ assert_match %r|#{Regexp.escape(Dir.tmpdir)}/foobar[a-zA-Z0-9-]+\.txt|,
209
+ file.to_path
210
+ ensure
211
+ file.close
212
+ end
213
+
214
+ def test_temporary_file_unlinked
215
+ refute File.exist?(file.to_path)
216
+ ensure
217
+ file.close
218
+ end
219
+
220
+ private
221
+
222
+ def file
223
+ @file ||= Chan.temporary_file %w[foobar .txt]
224
+ end
225
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: xchan.rb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.17.1
4
+ version: 0.17.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - '0x1eef'
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-05-10 00:00:00.000000000 Z
11
+ date: 2024-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: lockf.rb