|
97 | 97 | end |
98 | 98 | end.to raise_exception(EOFError, message: be =~ /Connection closed/) |
99 | 99 | end |
| 100 | + |
| 101 | + with "#close" do |
| 102 | + it "closes the connection without active streams" do |
| 103 | + # No active streams |
| 104 | + expect(connection.streams).to be(:empty?) |
| 105 | + |
| 106 | + # Close the connection - should not raise: |
| 107 | + connection.close(nil) |
| 108 | + |
| 109 | + expect(connection).to be(:closed?) |
| 110 | + end |
| 111 | + |
| 112 | + it "generates an error when closing with active streams" do |
| 113 | + # Create an active stream: |
| 114 | + stream = Protocol::HTTP2::Stream.new(connection, 1) |
| 115 | + connection.streams[1] = stream |
| 116 | + |
| 117 | + expect(stream).to receive(:close) do |error| |
| 118 | + expect(error).to be_a(EOFError) |
| 119 | + expect(error.message).to be =~ /Connection closed with 1 active stream/ |
| 120 | + end |
| 121 | + |
| 122 | + # Close the connection without an error: |
| 123 | + connection.close(nil) |
| 124 | + end |
| 125 | + |
| 126 | + it "passes through the error when closing with active streams and an explicit error" do |
| 127 | + # Create an active stream: |
| 128 | + stream = Protocol::HTTP2::Stream.new(connection, 1) |
| 129 | + connection.streams[1] = stream |
| 130 | + |
| 131 | + custom_error = RuntimeError.new("Connection failed!") |
| 132 | + |
| 133 | + expect(stream).to receive(:close) do |error| |
| 134 | + expect(error).to be === custom_error |
| 135 | + end |
| 136 | + |
| 137 | + # Close the connection with an explicit error: |
| 138 | + connection.close(custom_error) |
| 139 | + end |
| 140 | + |
| 141 | + it "generates an error for multiple active streams" do |
| 142 | + # Create multiple active streams: |
| 143 | + stream1 = Protocol::HTTP2::Stream.new(connection, 1) |
| 144 | + stream2 = Protocol::HTTP2::Stream.new(connection, 3) |
| 145 | + connection.streams[1] = stream1 |
| 146 | + connection.streams[3] = stream2 |
| 147 | + |
| 148 | + expect(stream1).to receive(:close) do |error| |
| 149 | + expect(error).to be_a(EOFError) |
| 150 | + expect(error.message).to be =~ /Connection closed with 2 active stream/ |
| 151 | + end |
| 152 | + |
| 153 | + expect(stream2).to receive(:close) do |error| |
| 154 | + expect(error).to be_a(EOFError) |
| 155 | + expect(error.message).to be =~ /Connection closed with 2 active stream/ |
| 156 | + end |
| 157 | + |
| 158 | + # Close the connection without an error: |
| 159 | + connection.close(nil) |
| 160 | + end |
| 161 | + end |
100 | 162 | end |
101 | 163 |
|
102 | 164 | with "client and server" do |
|
0 commit comments