@Override
  public synchronized void onResponseComplete(Exception exception) {
    if (requestComplete.compareAndSet(false, true)) {
      this.exception = exception;
      try {
        if (!responseMetadataFinalized.get() && exception != null) {
          // clear headers except for the value of Allow
          String allow = getHeader(RestUtils.Headers.ALLOW);
          responseMetadata.put(RESPONSE_HEADERS_KEY, new JSONObject());
          if (!Utils.isNullOrEmpty(allow)) {
            setHeader(RestUtils.Headers.ALLOW, allow);
          }
          setHeader(RestUtils.Headers.CONTENT_TYPE, "text/plain; charset=UTF-8");
          ResponseStatus status = ResponseStatus.InternalServerError;
          if (exception instanceof RestServiceException) {
            status = ResponseStatus.getResponseStatus(((RestServiceException) exception).getErrorCode());
          }
          responseMetadata.put(RESPONSE_STATUS_KEY, status.name());
          bodyBytes.write(exception.toString().getBytes());
          responseMetadataFinalized.set(true);
        }
        close();
        if (restRequest != null) {
          restRequest.getMetricsTracker().nioMetricsTracker.markRequestCompleted();
          restRequest.close();
        }
        onEventComplete(Event.OnRequestComplete);
      } catch (Exception e) {
        // nothing to do
      }
    }
  }
Called when an event (function call) finishes successfully in MockRestResponseChannel. Does *not* trigger if the
event (function) fails.
@param mockRestResponseChannel the {@link MockRestResponseChannel} where the event occurred.
@param event the {@link Event} that occurred.
ambry-test-utils/src/main/java/com/github/ambry/rest/MockRestResponseChannel.java