|
3 | 3 | # Released under the MIT License. |
4 | 4 | # Copyright, 2021-2025, by Samuel Williams. |
5 | 5 |
|
6 | | -require "async/bus/server" |
7 | | -require "async/bus/client" |
8 | | - |
9 | | -require "sus/fixtures/async" |
10 | | -require "tmpdir" |
11 | | -require "io/endpoint/bound_endpoint" |
12 | | - |
13 | | -class Counter |
14 | | - def initialize(count = 0) |
15 | | - @count = count |
16 | | - end |
17 | | - |
18 | | - attr :count |
19 | | - |
20 | | - def increment |
21 | | - @count += 1 |
22 | | - end |
23 | | - |
24 | | - def each |
25 | | - @count.times do |
26 | | - yield Object.new |
27 | | - end |
28 | | - end |
29 | | - |
30 | | - def make |
31 | | - Object.new |
32 | | - end |
33 | | - |
34 | | - def itself(object) |
35 | | - return object |
36 | | - end |
37 | | - |
38 | | - def error(message) |
39 | | - raise message |
40 | | - end |
41 | | -end |
| 6 | +require "async/bus/a_server" |
42 | 7 |
|
43 | 8 | describe Async::Bus::Server do |
44 | | - include Sus::Fixtures::Async::ReactorContext |
45 | | - |
46 | | - let(:ipc_path) {File.join(@root, "bus.ipc")} |
47 | | - let(:endpoint) {Async::Bus::Protocol.local_endpoint(ipc_path)} |
48 | | - |
49 | | - def around(&block) |
50 | | - Dir.mktmpdir do |directory| |
51 | | - @root = directory |
52 | | - super(&block) |
| 9 | + include Async::Bus::AServer |
| 10 | + |
| 11 | + with "#bind" do |
| 12 | + it "can receive incoming clients and expose a bound object" do |
| 13 | + server_task = Async do |
| 14 | + server.accept do |connection| |
| 15 | + connection.bind(:object, Object.new) |
| 16 | + end |
| 17 | + end |
| 18 | + |
| 19 | + client.connect do |connection| |
| 20 | + expect(connection[:object]).to be_a(Object) |
| 21 | + end |
53 | 22 | end |
54 | 23 | end |
55 | 24 |
|
56 | | - def before |
57 | | - @bound_endpoint = endpoint.bound |
58 | | - end |
59 | | - |
60 | | - def after(error = nil) |
61 | | - @bound_endpoint&.close |
62 | | - end |
63 | | - |
64 | | - let(:server) {Async::Bus::Server.new(@bound_endpoint)} |
65 | | - let(:client) {Async::Bus::Client.new(endpoint)} |
66 | | - |
67 | | - it "can receive incoming clients" do |
68 | | - server_task = Async do |
69 | | - server.accept do |connection| |
70 | | - connection.bind(:counter, Counter.new) |
| 25 | + with "a bound Array instance" do |
| 26 | + let(:array) {Array.new} |
| 27 | + |
| 28 | + def before |
| 29 | + super |
| 30 | + |
| 31 | + @server_task = Async do |
| 32 | + server.accept do |connection| |
| 33 | + connection.bind(:array, array) |
| 34 | + end |
71 | 35 | end |
72 | 36 | end |
73 | 37 |
|
74 | | - client.connect do |connection| |
75 | | - 3.times do |
76 | | - connection[:counter].increment |
| 38 | + def after(error = nil) |
| 39 | + @server_task.stop |
| 40 | + |
| 41 | + super |
| 42 | + end |
| 43 | + |
| 44 | + it "can add items to the array" do |
| 45 | + client.connect do |connection| |
| 46 | + connection[:array] << 1 |
77 | 47 | end |
78 | 48 |
|
79 | | - expect(connection[:counter].count).to be == 3 |
| 49 | + expect(array).to be == [1] |
80 | 50 | end |
81 | | - end |
82 | | - |
83 | | - it "can return proxy objects" do |
84 | | - server_task = Async do |
85 | | - server.accept do |connection| |
86 | | - connection.bind(:counter, Counter) |
| 51 | + |
| 52 | + it "can use equality operators" do |
| 53 | + array << 1 |
| 54 | + |
| 55 | + client.connect do |connection| |
| 56 | + expect(connection[:array] == [1]).to be_truthy |
| 57 | + expect(connection[:array] != [2]).to be_truthy |
87 | 58 | end |
88 | 59 | end |
89 | 60 |
|
90 | | - client.connect do |connection| |
91 | | - counter = connection[:counter].new |
| 61 | + it "can enumerate items in the array" do |
| 62 | + array << 1 << 2 << 3 |
| 63 | + enumerated = [] |
92 | 64 |
|
93 | | - 3.times do |
94 | | - counter.increment |
| 65 | + client.connect do |connection| |
| 66 | + connection[:array].each do |item| |
| 67 | + enumerated << item |
| 68 | + end |
95 | 69 | end |
96 | 70 |
|
97 | | - expect(counter.count).to be == 3 |
| 71 | + expect(enumerated).to be == [1, 2, 3] |
98 | 72 | end |
99 | | - end |
100 | | - |
101 | | - it "can return the original object" do |
102 | | - server_task = Async do |
103 | | - server.accept do |connection| |
104 | | - connection.bind(:counter, Counter) |
| 73 | + |
| 74 | + it "can raise an exception" do |
| 75 | + client.connect do |connection| |
| 76 | + expect do |
| 77 | + connection[:array]["one"] |
| 78 | + end.to raise_exception(TypeError, message: be =~ /no implicit conversion of String into Integer/) |
105 | 79 | end |
106 | 80 | end |
107 | 81 |
|
108 | | - client.connect do |connection| |
109 | | - counter = connection[:counter].new |
110 | | - object = Object.new |
111 | | - |
112 | | - object2 = counter.itself(object) |
113 | | - |
114 | | - expect(object).to be_equal(object2) |
| 82 | + it "can get all methods" do |
| 83 | + client.connect do |connection| |
| 84 | + expect(connection[:array].methods).to be == array.methods |
| 85 | + expect(connection[:array].public_methods).to be == array.public_methods |
| 86 | + expect(connection[:array].protected_methods).to be == array.protected_methods |
| 87 | + expect(connection[:array].private_methods).to be == array.private_methods |
| 88 | + end |
115 | 89 | end |
116 | | - end |
117 | | - |
118 | | - it "can raise error" do |
119 | | - server_task = Async do |
120 | | - server.accept do |connection| |
121 | | - connection.bind(:counter, Counter) |
| 90 | + |
| 91 | + it "can check if it responds to methods" do |
| 92 | + client.connect do |connection| |
| 93 | + expect(connection[:array].respond_to?(:each)).to be_truthy |
| 94 | + expect(connection[:array].respond_to?(:no_such_method)).to be_falsey |
122 | 95 | end |
123 | 96 | end |
124 | 97 |
|
125 | | - client.connect do |connection| |
126 | | - counter = connection[:counter].new |
| 98 | + it "can get a method instance and call it" do |
| 99 | + array << 1 << 2 << 3 |
| 100 | + enumerated = [] |
127 | 101 |
|
128 | | - expect do |
129 | | - counter.error("Hello") |
130 | | - end.to raise_exception(RuntimeError, message: be =~ /Hello/) |
131 | | - end |
132 | | - end |
133 | | - |
134 | | - it "can release proxy objects" do |
135 | | - server_task = Async do |
136 | | - server.accept do |connection| |
137 | | - connection.bind(:counter, Counter) |
| 102 | + client.connect do |connection| |
| 103 | + method = connection[:array].method(:each) |
138 | 104 |
|
139 | | - connection.bind(:objects, connection.objects) |
| 105 | + method.call do |item| |
| 106 | + enumerated << item |
| 107 | + end |
140 | 108 | end |
| 109 | + |
| 110 | + expect(enumerated).to be == [1, 2, 3] |
141 | 111 | end |
142 | 112 |
|
143 | | - client.connect do |connection| |
144 | | - counter = connection[:counter].new(10) |
145 | | - |
146 | | - 10.times do |
147 | | - counter.make |
148 | | - GC.start |
| 113 | + it "has a __name__" do |
| 114 | + client.connect do |connection| |
| 115 | + expect(connection[:array].__name__).to be == :array |
149 | 116 | end |
150 | | - |
151 | | - expect(connection[:objects].size).to be < 10 |
152 | 117 | end |
153 | 118 | end |
154 | 119 | end |
0 commit comments