uringmachine 1.0.8 → 1.0.9
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/TODO.md +5 -0
- data/lib/uringmachine/broadcaster.rb +44 -0
- data/lib/uringmachine/version.rb +1 -1
- data/lib/uringmachine.rb +6 -0
- data/test/test_broadcaster.rb +97 -0
- metadata +4 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ebeda6a23e02e6ea2325264cf90d9fe2ac3977086995936328dea34f201f5fda
|
|
4
|
+
data.tar.gz: b3540e9219257a54f770b97a1ec15c2d9102f09312a4416db98b9d6b38cbfde7
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bf61e20da3015b218f0298a431578747b1eadef6269c77a2765daec5e77379231e0cd9e58e18f9d6276b5872f134662805b2900f8ffcdec4666856d36b92b21e
|
|
7
|
+
data.tar.gz: c167e10850043e7c80bcafa1eb768a484b2a353672e5965da69c81c6c6a4d709654658e59b483d83e8de0c03e56f7134bb6a31b824fdcf89130d9ca5c885e76d
|
data/CHANGELOG.md
CHANGED
data/TODO.md
CHANGED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class UringMachine
|
|
4
|
+
# A Broadcaster implements a broadcast pattern where multiple fibers may be
|
|
5
|
+
# notified of an event or some arbitrary value. Listeners listen for events
|
|
6
|
+
# using the #each method.
|
|
7
|
+
class Broadcaster
|
|
8
|
+
# Initializes a broadcaster instance.
|
|
9
|
+
#
|
|
10
|
+
# @param machine [UringMachine]
|
|
11
|
+
# @return [void]
|
|
12
|
+
def initialize(machine)
|
|
13
|
+
@machine = machine
|
|
14
|
+
@listeners = Set.new
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
# Returns the number of currently listening fibers.
|
|
18
|
+
#
|
|
19
|
+
# @return [Integer]
|
|
20
|
+
def listener_count
|
|
21
|
+
@listeners.size
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Broadcasts the given value to all listeners.
|
|
25
|
+
#
|
|
26
|
+
# @param value [any]
|
|
27
|
+
# @return [void]
|
|
28
|
+
def broadcast(value)
|
|
29
|
+
@listeners.each { @machine.push(it, value) }
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Listens for broadcast values in an infinite loop, yielding each broadcast
|
|
33
|
+
# value to the given block.
|
|
34
|
+
#
|
|
35
|
+
# @return [void]
|
|
36
|
+
def each
|
|
37
|
+
mailbox = Fiber.current.mailbox
|
|
38
|
+
@listeners << mailbox
|
|
39
|
+
loop { yield @machine.shift(mailbox) }
|
|
40
|
+
ensure
|
|
41
|
+
@listeners.delete(mailbox)
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
end
|
data/lib/uringmachine/version.rb
CHANGED
data/lib/uringmachine.rb
CHANGED
|
@@ -3,6 +3,7 @@
|
|
|
3
3
|
require 'um_ext'
|
|
4
4
|
require 'uringmachine/version'
|
|
5
5
|
require 'uringmachine/dns_resolver'
|
|
6
|
+
require 'uringmachine/broadcaster'
|
|
6
7
|
|
|
7
8
|
UM = UringMachine
|
|
8
9
|
|
|
@@ -221,6 +222,11 @@ class UringMachine
|
|
|
221
222
|
res
|
|
222
223
|
end
|
|
223
224
|
|
|
225
|
+
# @return [UringMachine::Broadcaster]
|
|
226
|
+
def broadcaster
|
|
227
|
+
Broadcaster.new(self)
|
|
228
|
+
end
|
|
229
|
+
|
|
224
230
|
# call-seq:
|
|
225
231
|
# machine.file_read(fn) -> data
|
|
226
232
|
#
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative 'helper'
|
|
4
|
+
require 'uringmachine/broadcaster'
|
|
5
|
+
|
|
6
|
+
class BroadcasterTest < UMBaseTest
|
|
7
|
+
def test_broadcaster_basic_usage
|
|
8
|
+
buf = []
|
|
9
|
+
broadcaster = UM::Broadcaster.new(machine)
|
|
10
|
+
f1 = machine.spin do
|
|
11
|
+
broadcaster.each { |v| buf << [:f1, v] }
|
|
12
|
+
end
|
|
13
|
+
f2 = machine.spin do
|
|
14
|
+
broadcaster.each { |v| buf << [:f2, v] }
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
assert_equal 0, broadcaster.listener_count
|
|
18
|
+
3.times { machine.snooze }
|
|
19
|
+
assert_equal 2, broadcaster.listener_count
|
|
20
|
+
broadcaster.broadcast(:foo)
|
|
21
|
+
3.times { machine.snooze }
|
|
22
|
+
assert_equal [[:f1, :foo], [:f2, :foo]], buf
|
|
23
|
+
|
|
24
|
+
broadcaster.broadcast(:bar)
|
|
25
|
+
3.times { machine.snooze }
|
|
26
|
+
assert_equal [[:f1, :foo], [:f2, :foo], [:f1, :bar], [:f2, :bar]], buf
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def test_broadcaster_listener_break
|
|
30
|
+
buf = []
|
|
31
|
+
broadcaster = UM::Broadcaster.new(machine)
|
|
32
|
+
f1 = machine.spin do
|
|
33
|
+
broadcaster.each { |v| buf << [:f1, v]; break }
|
|
34
|
+
end
|
|
35
|
+
f2 = machine.spin do
|
|
36
|
+
broadcaster.each { |v| buf << [:f2, v] }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
3.times { machine.snooze }
|
|
40
|
+
assert_equal 2, broadcaster.listener_count
|
|
41
|
+
broadcaster.broadcast(:foo)
|
|
42
|
+
3.times { machine.snooze }
|
|
43
|
+
assert_equal [[:f1, :foo], [:f2, :foo]], buf
|
|
44
|
+
|
|
45
|
+
assert_equal 1, broadcaster.listener_count
|
|
46
|
+
broadcaster.broadcast(:bar)
|
|
47
|
+
3.times { machine.snooze }
|
|
48
|
+
assert_equal [[:f1, :foo], [:f2, :foo], [:f2, :bar]], buf
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def test_broadcaster_listener_exception
|
|
52
|
+
buf = []
|
|
53
|
+
broadcaster = UM::Broadcaster.new(machine)
|
|
54
|
+
f1 = machine.spin do
|
|
55
|
+
broadcaster.each { |v| buf << [:f1, v]; raise }
|
|
56
|
+
rescue => e
|
|
57
|
+
buf << [:f1, :exception]
|
|
58
|
+
end
|
|
59
|
+
f2 = machine.spin do
|
|
60
|
+
broadcaster.each { |v| buf << [:f2, v] }
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
3.times { machine.snooze }
|
|
64
|
+
assert_equal 2, broadcaster.listener_count
|
|
65
|
+
broadcaster.broadcast(:foo)
|
|
66
|
+
3.times { machine.snooze }
|
|
67
|
+
assert_equal [[:f1, :foo], [:f1, :exception], [:f2, :foo]], buf
|
|
68
|
+
|
|
69
|
+
assert_equal 1, broadcaster.listener_count
|
|
70
|
+
broadcaster.broadcast(:bar)
|
|
71
|
+
3.times { machine.snooze }
|
|
72
|
+
assert_equal [[:f1, :foo], [:f1, :exception], [:f2, :foo], [:f2, :bar]], buf
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def test_broadcaster_helper_method
|
|
76
|
+
buf = []
|
|
77
|
+
broadcaster = machine.broadcaster
|
|
78
|
+
assert_kind_of UM::Broadcaster, broadcaster
|
|
79
|
+
f1 = machine.spin do
|
|
80
|
+
broadcaster.each { |v| buf << [:f1, v] }
|
|
81
|
+
end
|
|
82
|
+
f2 = machine.spin do
|
|
83
|
+
broadcaster.each { |v| buf << [:f2, v] }
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
assert_equal 0, broadcaster.listener_count
|
|
87
|
+
3.times { machine.snooze }
|
|
88
|
+
assert_equal 2, broadcaster.listener_count
|
|
89
|
+
broadcaster.broadcast(:foo)
|
|
90
|
+
3.times { machine.snooze }
|
|
91
|
+
assert_equal [[:f1, :foo], [:f2, :foo]], buf
|
|
92
|
+
|
|
93
|
+
broadcaster.broadcast(:bar)
|
|
94
|
+
3.times { machine.snooze }
|
|
95
|
+
assert_equal [[:f1, :foo], [:f2, :foo], [:f1, :bar], [:f2, :bar]], buf
|
|
96
|
+
end
|
|
97
|
+
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: uringmachine
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.0.
|
|
4
|
+
version: 1.0.9
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Sharon Rosner
|
|
@@ -39,6 +39,7 @@ extra_rdoc_files:
|
|
|
39
39
|
- ext/um/um_utils.c
|
|
40
40
|
- lib/uringmachine.rb
|
|
41
41
|
- lib/uringmachine/actor.rb
|
|
42
|
+
- lib/uringmachine/broadcaster.rb
|
|
42
43
|
- lib/uringmachine/dns_resolver.rb
|
|
43
44
|
- lib/uringmachine/fiber_scheduler.rb
|
|
44
45
|
- lib/uringmachine/version.rb
|
|
@@ -141,6 +142,7 @@ files:
|
|
|
141
142
|
- grant-2025/tasks.md
|
|
142
143
|
- lib/uringmachine.rb
|
|
143
144
|
- lib/uringmachine/actor.rb
|
|
145
|
+
- lib/uringmachine/broadcaster.rb
|
|
144
146
|
- lib/uringmachine/dns_resolver.rb
|
|
145
147
|
- lib/uringmachine/fiber_scheduler.rb
|
|
146
148
|
- lib/uringmachine/version.rb
|
|
@@ -150,6 +152,7 @@ files:
|
|
|
150
152
|
- test/run.rb
|
|
151
153
|
- test/test_actor.rb
|
|
152
154
|
- test/test_async_op.rb
|
|
155
|
+
- test/test_broadcaster.rb
|
|
153
156
|
- test/test_fiber.rb
|
|
154
157
|
- test/test_fiber_scheduler.rb
|
|
155
158
|
- test/test_io.rb
|