test_class.declarator: url = URL.valueOf("dubbo://localhost:20880")
test_class.modifier: private
test_class.original_string: private URL url = URL.valueOf("dubbo://localhost:20880");
test_class.type: URL
test_class.var_name: url
test_class.declarator: channel
test_class.modifier: private
test_class.original_string: private MockChannel channel;
test_class.type: MockChannel
test_class.var_name: channel
test_class.declarator: task
test_class.modifier: private
test_class.original_string: private HeartBeatTask task;
test_class.type: HeartBeatTask
test_class.var_name: task
test_class.file: dubbo-remoting/dubbo-remoting-api/src/test/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTaskTest.java
test_class.identifier: HeartBeatTaskTest
test_case.body: @Test
      public void testHeartBeat() throws Exception {
          url = url.addParameter(Constants.DUBBO_VERSION_KEY, "2.1.1");
          channel.setAttribute(
                  HeaderExchangeHandler.KEY_READ_TIMESTAMP, System.currentTimeMillis());
          channel.setAttribute(
                  HeaderExchangeHandler.KEY_WRITE_TIMESTAMP, System.currentTimeMillis());
          Thread.sleep(2000L);
          task.run();
          List<Object> objects = channel.getSentObjects();
          Assert.assertTrue(objects.size() > 0);
          Object obj = objects.get(0);
          Assert.assertTrue(obj instanceof Request);
          Request request = (Request) obj;
          Assert.assertTrue(request.isHeartbeat());
      }
test_case.class_method_signature: HeartBeatTaskTest.testHeartBeat()
test_case.full_signature: @Test public void testHeartBeat()
test_case.identifier: testHeartBeat
test_case.invocations: addParameter setAttribute currentTimeMillis setAttribute currentTimeMillis sleep run getSentObjects assertTrue size get assertTrue assertTrue isHeartbeat
test_case.modifiers: @Test public
test_case.parameters: ()
test_case.return: void
test_case.signature: void testHeartBeat()
focal_class.declarator: logger = LoggerFactory.getLogger(HeartBeatTask.class)
focal_class.modifier: private static final
focal_class.original_string: private static final Logger logger = LoggerFactory.getLogger(HeartBeatTask.class);
focal_class.type: Logger
focal_class.var_name: logger
focal_class.declarator: channelProvider
focal_class.modifier: private
focal_class.original_string: private ChannelProvider channelProvider;
focal_class.type: ChannelProvider
focal_class.var_name: channelProvider
focal_class.declarator: heartbeat
focal_class.modifier: private
focal_class.original_string: private int heartbeat;
focal_class.type: int
focal_class.var_name: heartbeat
focal_class.declarator: heartbeatTimeout
focal_class.modifier: private
focal_class.original_string: private int heartbeatTimeout;
focal_class.type: int
focal_class.var_name: heartbeatTimeout
focal_class.file: dubbo-remoting/dubbo-remoting-api/src/main/java/com/alibaba/dubbo/remoting/exchange/support/header/HeartBeatTask.java
focal_class.identifier: HeartBeatTask
focal_class.interfaces: implements Runnable
focal_class.class_method_signature: HeartBeatTask.HeartBeatTask(ChannelProvider provider, int heartbeat, int heartbeatTimeout)
focal_class.full_signature:   HeartBeatTask(ChannelProvider provider, int heartbeat, int heartbeatTimeout)
focal_class.identifier: HeartBeatTask
focal_class.parameters: (ChannelProvider provider, int heartbeat, int heartbeatTimeout)
focal_class.signature:  HeartBeatTask(ChannelProvider provider, int heartbeat, int heartbeatTimeout)
focal_class.class_method_signature: HeartBeatTask.run()
focal_class.full_signature: @Override public void run()
focal_class.identifier: run
focal_class.modifiers: @Override public
focal_class.parameters: ()
focal_class.return: void
focal_class.signature: void run()
focal_method.body: @Override
      public void run() {
          try {
              long now = System.currentTimeMillis();
              for (Channel channel : channelProvider.getChannels()) {
                  if (channel.isClosed()) {
                      continue;
                  }
                  try {
                      Long lastRead = (Long) channel.getAttribute(
                              HeaderExchangeHandler.KEY_READ_TIMESTAMP);
                      Long lastWrite = (Long) channel.getAttribute(
                              HeaderExchangeHandler.KEY_WRITE_TIMESTAMP);
                      if ((lastRead != null && now - lastRead > heartbeat)
                              || (lastWrite != null && now - lastWrite > heartbeat)) {
                          Request req = new Request();
                          req.setVersion(Version.getProtocolVersion());
                          req.setTwoWay(true);
                          req.setEvent(Request.HEARTBEAT_EVENT);
                          channel.send(req);
                          if (logger.isDebugEnabled()) {
                              logger.debug("Send heartbeat to remote channel " + channel.getRemoteAddress()
                                      + ", cause: The channel has no data-transmission exceeds a heartbeat period: " + heartbeat + "ms");
                          }
                      }
                      if (lastRead != null && now - lastRead > heartbeatTimeout) {
                          logger.warn("Close channel " + channel
                                  + ", because heartbeat read idle time out: " + heartbeatTimeout + "ms");
                          if (channel instanceof Client) {
                              try {
                                  ((Client) channel).reconnect();
                              } catch (Exception e) {
                                  //do nothing
                              }
                          } else {
                              channel.close();
                          }
                      }
                  } catch (Throwable t) {
                      logger.warn("Exception when heartbeat to remote channel " + channel.getRemoteAddress(), t);
                  }
              }
          } catch (Throwable t) {
              logger.warn("Unhandled exception when heartbeat, cause: " + t.getMessage(), t);
          }
      }
focal_method.class_method_signature: HeartBeatTask.run()
focal_method.full_signature: @Override public void run()
focal_method.identifier: run
focal_method.invocations: currentTimeMillis getChannels isClosed getAttribute getAttribute setVersion getProtocolVersion setTwoWay setEvent send isDebugEnabled debug getRemoteAddress warn reconnect close warn getRemoteAddress warn getMessage
focal_method.modifiers: @Override public
focal_method.parameters: ()
focal_method.return: void
focal_method.signature: void run()