/*
 * Copyright (C) 2015-2018 SoftIndex LLC.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.datakernel.rpc.client.sender;

import io.datakernel.rpc.client.sender.helper.RpcClientConnectionPoolStub;
import io.datakernel.rpc.client.sender.helper.RpcMessageDataStub;
import io.datakernel.rpc.client.sender.helper.RpcSenderStub;
import org.junit.Test;

import java.net.InetSocketAddress;

import static io.datakernel.rpc.client.sender.Callbacks.assertNoCalls;
import static io.datakernel.test.TestUtils.getFreePort;
import static org.junit.Assert.*;

public class RpcStrategySingleServerTest {

	private static final String HOST = "localhost";

	private static final InetSocketAddress ADDRESS = new InetSocketAddress(HOST, getFreePort());

	@Test
	public void itShouldBeCreatedWhenThereIsConnectionInPool() {
		RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub();
		RpcSenderStub connection = new RpcSenderStub();
		pool.put(ADDRESS, connection);
		RpcStrategySingleServer strategySingleServer = RpcStrategySingleServer.create(ADDRESS);

		RpcSender sender = strategySingleServer.createSender(pool);

		assertNotNull(sender);
	}

	@Test
	public void itShouldNotBeCreatedWhenThereIsNoConnectionInPool() {
		RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub();
		// no connections were added to pool
		RpcStrategySingleServer strategySingleServer = RpcStrategySingleServer.create(ADDRESS);

		RpcSender sender = strategySingleServer.createSender(pool);

		assertNull(sender);
	}

	@Test
	public void itShouldProcessAllCalls() {
		RpcClientConnectionPoolStub pool = new RpcClientConnectionPoolStub();
		RpcSenderStub connection = new RpcSenderStub();
		pool.put(ADDRESS, connection);
		RpcStrategySingleServer strategySingleServer = RpcStrategySingleServer.create(ADDRESS);
		RpcSender sender = strategySingleServer.createSender(pool);
		int calls = 100;
		int timeout = 50;
		RpcMessageDataStub data = new RpcMessageDataStub();

		assert sender != null;
		for (int i = 0; i < calls; i++) {
			sender.sendRequest(data, timeout, assertNoCalls());
		}

		assertEquals(calls, connection.getRequests());
	}
}
