def test_many_subchannels(self): k_channels = 4 self._pairs = _generate_channel_server_pairs(k_channels) k_success = 17 k_failed = 19 for i in range(k_success): self._send_successful_unary_unary(0) self._send_successful_unary_unary(2) for i in range(k_failed): self._send_failed_unary_unary(1) self._send_failed_unary_unary(2) gtc_resp = self._channelz_stub.GetTopChannels( channelz_pb2.GetTopChannelsRequest(start_channel_id=0)) self.assertEqual(len(gtc_resp.channel), k_channels) for i in range(k_channels): # If no call performed in the channel, there shouldn't be any subchannel if gtc_resp.channel[i].data.calls_started == 0: self.assertEqual(len(gtc_resp.channel[i].subchannel_ref), 0) continue # Otherwise, the subchannel should exist self.assertGreater(len(gtc_resp.channel[i].subchannel_ref), 0) gsc_resp = self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gtc_resp.channel[i].subchannel_ref[0]. subchannel_id)) self.assertEqual(gtc_resp.channel[i].data.calls_started, gsc_resp.subchannel.data.calls_started) self.assertEqual(gtc_resp.channel[i].data.calls_succeeded, gsc_resp.subchannel.data.calls_succeeded) self.assertEqual(gtc_resp.channel[i].data.calls_failed, gsc_resp.subchannel.data.calls_failed)
async def test_invalid_query_get_subchannel(self): with self.assertRaises(aio.AioRpcError) as exception_context: await self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=_LARGE_UNASSIGNED_ID)) self.assertEqual(grpc.StatusCode.NOT_FOUND, exception_context.exception.code())
async def test_streaming_rpc(self): pairs = await _create_channel_server_pairs(1, self._channelz_stub) # In C++, the argument for _send_successful_stream_stream is message length. # Here the argument is still channel idx, to be consistent with the other two. await self._send_successful_stream_stream(pairs[0]) gc_resp = await self._channelz_stub.GetChannel( channelz_pb2.GetChannelRequest(channel_id=pairs[0].channel_ref_id)) self.assertEqual(gc_resp.channel.data.calls_started, 1) self.assertEqual(gc_resp.channel.data.calls_succeeded, 1) self.assertEqual(gc_resp.channel.data.calls_failed, 0) # Subchannel exists self.assertGreater(len(gc_resp.channel.subchannel_ref), 0) gsc_resp = await self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gc_resp.channel.subchannel_ref[0].subchannel_id)) self.assertEqual(gsc_resp.subchannel.data.calls_started, 1) self.assertEqual(gsc_resp.subchannel.data.calls_succeeded, 1) self.assertEqual(gsc_resp.subchannel.data.calls_failed, 0) # Socket exists self.assertEqual(len(gsc_resp.subchannel.socket_ref), 1) gs_resp = await self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest( socket_id=gsc_resp.subchannel.socket_ref[0].socket_id)) self.assertEqual(gs_resp.socket.data.streams_started, 1) self.assertEqual(gs_resp.socket.data.streams_succeeded, 1) self.assertEqual(gs_resp.socket.data.streams_failed, 0) self.assertEqual(gs_resp.socket.data.messages_sent, test_constants.STREAM_LENGTH) self.assertEqual(gs_resp.socket.data.messages_received, test_constants.STREAM_LENGTH) await _destroy_channel_server_pairs(pairs)
def test_invalid_query_get_subchannel(self): try: self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest(subchannel_id=10000)) except BaseException as e: self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected')
def test_many_subchannels_and_sockets(self): k_channels = 4 self._pairs = _generate_channel_server_pairs(k_channels) k_success = 3 k_failed = 5 for i in range(k_success): self._send_successful_unary_unary(0) self._send_successful_unary_unary(2) for i in range(k_failed): self._send_failed_unary_unary(1) self._send_failed_unary_unary(2) gtc_resp = self._channelz_stub.GetTopChannels( channelz_pb2.GetTopChannelsRequest(start_channel_id=0)) self.assertEqual(len(gtc_resp.channel), k_channels) for i in range(k_channels): # If no call performed in the channel, there shouldn't be any subchannel if gtc_resp.channel[i].data.calls_started == 0: self.assertEqual(len(gtc_resp.channel[i].subchannel_ref), 0) continue # Otherwise, the subchannel should exist self.assertGreater(len(gtc_resp.channel[i].subchannel_ref), 0) gsc_resp = self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gtc_resp.channel[i].subchannel_ref[0]. subchannel_id)) self.assertEqual(len(gsc_resp.subchannel.socket_ref), 1) gs_resp = self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest( socket_id=gsc_resp.subchannel.socket_ref[0].socket_id)) self.assertEqual(gsc_resp.subchannel.data.calls_started, gs_resp.socket.data.streams_started) self.assertEqual(gsc_resp.subchannel.data.calls_started, gs_resp.socket.data.streams_succeeded) # Calls started == messages sent, only valid for unary calls self.assertEqual(gsc_resp.subchannel.data.calls_started, gs_resp.socket.data.messages_sent) # Only receive responses when the RPC was successful self.assertEqual(gsc_resp.subchannel.data.calls_succeeded, gs_resp.socket.data.messages_received) if gs_resp.socket.remote.HasField("tcpip_address"): address = gs_resp.socket.remote.tcpip_address.ip_address self.assertTrue( len(address) == 4 or len(address) == 16, address) if gs_resp.socket.local.HasField("tcpip_address"): address = gs_resp.socket.local.tcpip_address.ip_address self.assertTrue( len(address) == 4 or len(address) == 16, address)
async def test_many_subchannels_and_sockets(self): k_channels = 4 pairs = await _create_channel_server_pairs(k_channels, self._channelz_stub) k_success = 3 k_failed = 5 for i in range(k_success): await self._send_successful_unary_unary(pairs[0]) await self._send_successful_unary_unary(pairs[2]) for i in range(k_failed): await self._send_failed_unary_unary(pairs[1]) await self._send_failed_unary_unary(pairs[2]) for i in range(k_channels): gc_resp = await self._channelz_stub.GetChannel( channelz_pb2.GetChannelRequest( channel_id=pairs[i].channel_ref_id)) # If no call performed in the channel, there shouldn't be any subchannel if gc_resp.channel.data.calls_started == 0: self.assertEqual(len(gc_resp.channel.subchannel_ref), 0) continue # Otherwise, the subchannel should exist self.assertGreater(len(gc_resp.channel.subchannel_ref), 0) gsc_resp = await self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gc_resp.channel.subchannel_ref[0]. subchannel_id)) self.assertEqual(len(gsc_resp.subchannel.socket_ref), 1) gs_resp = await self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest( socket_id=gsc_resp.subchannel.socket_ref[0].socket_id)) self.assertEqual(gsc_resp.subchannel.data.calls_started, gs_resp.socket.data.streams_started) self.assertEqual(0, gs_resp.socket.data.streams_failed) # Calls started == messages sent, only valid for unary calls self.assertEqual(gsc_resp.subchannel.data.calls_started, gs_resp.socket.data.messages_sent) await _destroy_channel_server_pairs(pairs)
def test_streaming_rpc(self): self._pairs = _generate_channel_server_pairs(1) # In C++, the argument for _send_successful_stream_stream is message length. # Here the argument is still channel idx, to be consistent with the other two. self._send_successful_stream_stream(0) gc_resp = self._channelz_stub.GetChannel( channelz_pb2.GetChannelRequest(channel_id=self._get_channel_id(0))) self.assertEqual(gc_resp.channel.data.calls_started, 1) self.assertEqual(gc_resp.channel.data.calls_succeeded, 1) self.assertEqual(gc_resp.channel.data.calls_failed, 0) # Subchannel exists self.assertGreater(len(gc_resp.channel.subchannel_ref), 0) while True: gsc_resp = self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gc_resp.channel.subchannel_ref[0]. subchannel_id)) if gsc_resp.subchannel.data.calls_started == gsc_resp.subchannel.data.calls_succeeded + gsc_resp.subchannel.data.calls_failed: break self.assertEqual(gsc_resp.subchannel.data.calls_started, 1) self.assertEqual(gsc_resp.subchannel.data.calls_failed, 0) self.assertEqual(gsc_resp.subchannel.data.calls_succeeded, 1) # Socket exists self.assertEqual(len(gsc_resp.subchannel.socket_ref), 1) while True: gs_resp = self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest( socket_id=gsc_resp.subchannel.socket_ref[0].socket_id)) if gs_resp.socket.data.streams_started == gs_resp.socket.data.streams_succeeded + gs_resp.socket.data.streams_failed: break self.assertEqual(gs_resp.socket.data.streams_started, 1) self.assertEqual(gs_resp.socket.data.streams_succeeded, 1) self.assertEqual(gs_resp.socket.data.streams_failed, 0) self.assertEqual(gs_resp.socket.data.messages_sent, test_constants.STREAM_LENGTH) self.assertEqual(gs_resp.socket.data.messages_received, test_constants.STREAM_LENGTH)
async def test_many_subchannels(self): k_channels = 4 pairs = await _create_channel_server_pairs(k_channels, self._channelz_stub) k_success = 17 k_failed = 19 for i in range(k_success): await self._send_successful_unary_unary(pairs[0]) await self._send_successful_unary_unary(pairs[2]) for i in range(k_failed): await self._send_failed_unary_unary(pairs[1]) await self._send_failed_unary_unary(pairs[2]) for i in range(k_channels): gc_resp = await self._channelz_stub.GetChannel( channelz_pb2.GetChannelRequest( channel_id=pairs[i].channel_ref_id)) # If no call performed in the channel, there shouldn't be any subchannel if gc_resp.channel.data.calls_started == 0: self.assertEqual(len(gc_resp.channel.subchannel_ref), 0) continue # Otherwise, the subchannel should exist self.assertGreater(len(gc_resp.channel.subchannel_ref), 0) gsc_resp = await self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest( subchannel_id=gc_resp.channel.subchannel_ref[0]. subchannel_id)) self.assertEqual(gc_resp.channel.data.calls_started, gsc_resp.subchannel.data.calls_started) self.assertEqual(gc_resp.channel.data.calls_succeeded, gsc_resp.subchannel.data.calls_succeeded) self.assertEqual(gc_resp.channel.data.calls_failed, gsc_resp.subchannel.data.calls_failed) await _destroy_channel_server_pairs(pairs)