com.tera.gapi.itest.service.CAccountPoolService.java Source code

Java tutorial

Introduction

Here is the source code for com.tera.gapi.itest.service.CAccountPoolService.java

Source

/**
 * This file is part of tera-api.
 *
 * tera-api is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
    
 * tera-api is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
    
 * You should have received a copy of the GNU General Public License
 * along with tera-api.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.tera.gapi.itest.service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import javax.inject.Inject;

import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.tera.common.account.model.Account;
import com.tera.gapi.account.service.AccountChangeService;
import com.tera.gapi.account.service.GAccountService;

/**
 * @author ATracer
 * @since 0.1.0
 */
public class CAccountPoolService implements AccountPoolService {

    private static final Logger log = LoggerFactory.getLogger(CAccountPoolService.class);

    private List<String> requestedAccountNames = new ArrayList<String>();

    @Inject
    private AccountChangeService accountChangeService;
    @Inject
    private GAccountService accountService;

    private AtomicInteger atomicInteger = new AtomicInteger(1);

    /**
     * Equals to number of requested accounts
     */
    private int capacity;

    /**
     * Extra accounts to be requested
     */
    private final int EXTRA_CAPACITY = 10;

    @Override
    public void ensureCapacity(int capacity) {
        for (int i = 0; i < capacity + EXTRA_CAPACITY; i++) {
            String accountName = "INT_TEST_" + RandomStringUtils.randomAlphanumeric(10);
            requestedAccountNames.add(accountName);
            accountChangeService.newAccount(accountName);
        }
        validateCapacity(capacity);
        log.info("Set account capacity to {}", this.capacity);
    }

    @Override
    public int getNewAccountId() {
        final int nextAccountId = atomicInteger.incrementAndGet();
        if (nextAccountId > capacity) {
            throw new IllegalStateException("Capacity of account pool was reached");
        }
        Account account = accountService.getAccount(requestedAccountNames.get(nextAccountId));
        // recursive get in case there is no account with such name
        if (account == null) {
            return getNewAccountId();
        }
        return account.getObjectId();
    }

    @Override
    public void cleanup() {
        for (String accountName : requestedAccountNames) {
            accountChangeService.deleteAccount(accountName);
        }
    }

    private void validateCapacity(int requiredCapacity) {
        int available = countAvailable();
        int checkCounter = 0;
        while (available < requiredCapacity) {
            checkCounter++;
            available = countAvailable();
            if (checkCounter > 5 && available < requiredCapacity) {
                throw new IllegalStateException(
                        "Can't encsure requred capacity: " + capacity + ", available: " + available);
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        this.capacity = available;
    }

    private int countAvailable() {
        int available = 0;
        for (String name : requestedAccountNames) {
            if (accountService.getAccount(name) != null) {
                available++;
            }
        }
        return available;
    }
}