|
| 1 | +/* |
| 2 | + * Copyright 2015 Red Hat Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as assert from "assert"; |
| 18 | +import { AddressInfo, Server } from "net"; |
| 19 | +import * as rhea from "../"; |
| 20 | + |
| 21 | +describe('idle', function () { |
| 22 | + var server: rhea.Container; |
| 23 | + var listener: Server; |
| 24 | + |
| 25 | + beforeEach(function() { |
| 26 | + }) |
| 27 | + |
| 28 | + afterEach(function () { |
| 29 | + listener.close(); |
| 30 | + }); |
| 31 | + |
| 32 | + describe('server has idle_time_out', function () { |
| 33 | + beforeEach(function (done: Function) { |
| 34 | + server = rhea.create_container({ non_fatal_errors: [] }); |
| 35 | + listener = server.listen({ port: 0, idle_time_out: 300 }); |
| 36 | + listener.on('listening', function () { |
| 37 | + done(); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + // There are setups where server detects idle due to suspend while |
| 42 | + // networking and all is well. |
| 43 | + it('client does not send within idle_time_out', function (done: Function) { |
| 44 | + var state = 0; |
| 45 | + server.on('connection_open', function (context: rhea.EventContext) { |
| 46 | + assert.strictEqual(++state, 1); |
| 47 | + }); |
| 48 | + server.on('connection_error', function (context: rhea.EventContext) { |
| 49 | + assert.fail('server must not receive error'); |
| 50 | + }); |
| 51 | + server.on('connection_close', function (context: rhea.EventContext) { |
| 52 | + assert.strictEqual(++state, 4); |
| 53 | + }); |
| 54 | + server.on('disconnected', function (context: rhea.EventContext) { |
| 55 | + assert.strictEqual(context.reconnecting, undefined); |
| 56 | + assert.strictEqual(++state, 5); |
| 57 | + setTimeout(() => { done() }, 100); |
| 58 | + }); |
| 59 | + |
| 60 | + var client: rhea.Container = rhea.create_container(); |
| 61 | + var conn = client.connect({ |
| 62 | + port: (listener.address() as AddressInfo).port |
| 63 | + }); |
| 64 | + conn.on('connection_open', function (context: rhea.EventContext) { |
| 65 | + const connection = context.connection; |
| 66 | + assert.strictEqual(++state, 2); |
| 67 | + assert.strictEqual(connection.remote.open.idle_time_out, 300); |
| 68 | + // Disable idle timer |
| 69 | + connection.remote.open.idle_time_out = 0; |
| 70 | + if (connection.heartbeat_out) clearTimeout(connection.heartbeat_out); |
| 71 | + }); |
| 72 | + conn.on('connection_error', function (context: rhea.EventContext) { |
| 73 | + assert.strictEqual(++state, 3); |
| 74 | + var error = context.connection.error; |
| 75 | + assert.strictEqual((error as any).condition, 'amqp:resource-limit-exceeded'); |
| 76 | + assert.strictEqual((error as any).description, 'max idle time exceeded'); |
| 77 | + }); |
| 78 | + conn.on('disconnected', function (context: rhea.EventContext) { |
| 79 | + assert.fail('disconnected shouldnt have been called'); |
| 80 | + }); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + describe('server has no idle_time_out', function () { |
| 85 | + beforeEach(function (done: Function) { |
| 86 | + server = rhea.create_container({ non_fatal_errors: [] }); |
| 87 | + listener = server.listen({ port: 0 }); |
| 88 | + listener.on('listening', function () { |
| 89 | + done(); |
| 90 | + }); |
| 91 | + }); |
| 92 | + |
| 93 | + it('server does not send within idle_time_out', function (done: Function) { |
| 94 | + var state = 0; |
| 95 | + var server_opens = 0; |
| 96 | + var server_closes = 0; |
| 97 | + server.on('connection_open', function (context: rhea.EventContext) { |
| 98 | + ++server_opens; |
| 99 | + const connection = context.connection; |
| 100 | + assert.strictEqual(connection.remote.open.idle_time_out, 500); |
| 101 | + if (server_opens == 1) { |
| 102 | + // Disable idle timer |
| 103 | + assert.strictEqual(++state, 1); |
| 104 | + connection.remote.open.idle_time_out = 0; |
| 105 | + if (connection.heartbeat_out) clearTimeout(connection.heartbeat_out); |
| 106 | + } else { |
| 107 | + // Reconnect after failure. Just close. |
| 108 | + assert.strictEqual(++state, 7); |
| 109 | + connection.close({ condition: 'time to end this test', description: 'well done' }) |
| 110 | + } |
| 111 | + }); |
| 112 | + server.on('connection_error', function (context: rhea.EventContext) { |
| 113 | + var error = context.connection.error; |
| 114 | + assert.strictEqual((error as any).condition, 'amqp:resource-limit-exceeded'); |
| 115 | + assert.strictEqual((error as any).description, 'max idle time exceeded'); |
| 116 | + assert.strictEqual(++state, 3); |
| 117 | + }); |
| 118 | + server.on('connection_close', function (context: rhea.EventContext) { |
| 119 | + ++server_closes; |
| 120 | + if (server_closes == 1) { |
| 121 | + assert.strictEqual(++state, 4); |
| 122 | + } else { |
| 123 | + done(); |
| 124 | + } |
| 125 | + }); |
| 126 | + server.on('disconnected', function (context: rhea.EventContext) { |
| 127 | + assert.fail('disconnected shouldnt have been called'); |
| 128 | + }); |
| 129 | + |
| 130 | + var client: rhea.Container = rhea.create_container(); |
| 131 | + var conn = client.connect({ |
| 132 | + port: (listener.address() as AddressInfo).port, |
| 133 | + idle_time_out: 500 |
| 134 | + }); |
| 135 | + var client_opens = 0; |
| 136 | + var client_closes = 0; |
| 137 | + var client_disconnects = 0; |
| 138 | + conn.on('connection_open', function (context: rhea.EventContext) { |
| 139 | + client_opens++; |
| 140 | + assert.strictEqual(client_opens, server_opens); |
| 141 | + if (client_opens === 2) { |
| 142 | + assert.strictEqual(++state, 8); |
| 143 | + conn.close(); |
| 144 | + } else { |
| 145 | + assert.strictEqual(++state, 2); |
| 146 | + } |
| 147 | + }); |
| 148 | + conn.on('connection_error', function (context: rhea.EventContext) { |
| 149 | + var error = context.connection.error; |
| 150 | + assert.strictEqual((error as any).condition, 'time to end this test'); |
| 151 | + assert.strictEqual(++state, 9); |
| 152 | + }); |
| 153 | + conn.on('connection_close', function (context: rhea.EventContext) { |
| 154 | + client_closes++; |
| 155 | + if (client_closes == 1) { |
| 156 | + assert.strictEqual(client_closes, 1); |
| 157 | + assert.strictEqual(server_opens, 1); |
| 158 | + assert.strictEqual(client_opens, 1); |
| 159 | + assert.strictEqual(++state, 5); |
| 160 | + } else { |
| 161 | + assert.strictEqual(client_closes, 2); |
| 162 | + assert.strictEqual(server_opens, 2); |
| 163 | + assert.strictEqual(client_opens, 2); |
| 164 | + assert.strictEqual(++state, 10); |
| 165 | + } |
| 166 | + }); |
| 167 | + conn.on('disconnected', function (context: rhea.EventContext) { |
| 168 | + client_disconnects++; |
| 169 | + assert.strictEqual(client_disconnects, 1); |
| 170 | + assert.strictEqual(server_opens, 1); |
| 171 | + assert.strictEqual(context.reconnecting, true); |
| 172 | + assert.strictEqual(++state, 6); |
| 173 | + }); |
| 174 | + }); |
| 175 | + }); |
| 176 | +}); |
0 commit comments