package com.sourcefellows.bookingfellow.client;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.client.ResourceAccessException;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

@Component
public class TestRunner {

    private static final Logger logger = LoggerFactory.getLogger(TestRunner.class);

    private final Client client;

    @Autowired
    TestRunner(Client client) {
        this.client = client;
    }

    public void callSimple() {
        logger.info(client.callHTTPService());
    }

    public void callWithHardTimeout() {

        try {
            logger.info(client.callHTTPServiceWithHardTimeout());
        } catch (TimeoutException | ExecutionException | InterruptedException e) {
            logger.error(e.getMessage());
        }

    }

    public void callWithBulkhead() {

        for (int i = 0; i < 100; i++) {
            try {
                logger.info("result {}", client.callWithBulkhead());
            } catch (ResourceAccessException | ExecutionException | InterruptedException | TimeoutException e) {
                logger.info("exception {}", e.getClass().getSimpleName());
            }
        }

    }

}
