<commit_before>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn 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.
 *
 * Grakn 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 Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.test.engine;

import ai.grakn.client.TaskClient;
import ai.grakn.engine.TaskId;
import ai.grakn.engine.TaskStatus;
import ai.grakn.engine.tasks.TaskState;
import ai.grakn.engine.tasks.TaskStateStorage;
import ai.grakn.engine.tasks.manager.ZookeeperConnection;
import ai.grakn.engine.tasks.mock.FailingMockTask;
import ai.grakn.engine.tasks.storage.TaskStateZookeeperStore;
import ai.grakn.exception.EngineStorageException;
import ai.grakn.test.DistributionContext;
import ai.grakn.test.engine.tasks.BackgroundTaskTestUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.Size;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static ai.grakn.engine.TaskStatus.COMPLETED;
import static ai.grakn.engine.TaskStatus.FAILED;
import static ai.grakn.engine.TaskStatus.STOPPED;
import static java.util.stream.Collectors.toSet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(JUnitQuickcheck.class)
public class GraknEngineFailoverIT {

    private static ZookeeperConnection connection;
    private static TaskStateStorage storage;

    @ClassRule
    public static DistributionContext engine1 = DistributionContext.startSingleQueueEngineProcess().port(4567);

    @ClassRule
    public static DistributionContext engine2 = DistributionContext.startSingleQueueEngineProcess().port(5678);

    @ClassRule
    public static DistributionContext engine3 = DistributionContext.startSingleQueueEngineProcess().port(6789);

    @BeforeClass
    public static void getStorage() {
        connection = new ZookeeperConnection();
        storage = new TaskStateZookeeperStore(connection);
    }

    @AfterClass
    public static void closeStorage() {
        connection.close();
    }

    @Property(trials=10)
    public void whenSubmittingTasksToOneEngine_TheyComplete(List<TaskState> tasks1) throws Exception {
        // Create & Send tasks to rest api
        Set<TaskId> tasks = sendTasks(engine1.port(), tasks1);

        // Wait for those tasks to complete
        waitForStatus(tasks, COMPLETED, FAILED);

        // Assert the tasks have finished with the correct status depending on type
        assertTasksCompletedWithCorrectStatus(tasks);
    }


    @Property(trials=10)
    public void whenSubmittingTasksToTwoEngines_TheyComplete(
            List<TaskState> tasks1, List<TaskState> tasks2) throws Exception {
        // Create & Send tasks to rest api
        Set<TaskId> taskIds1 = sendTasks(engine1.port(), tasks1);
        Set<TaskId> taskIds2 = sendTasks(engine2.port(), tasks2);

        Set<TaskId> allTasks = new HashSet<>();
        allTasks.addAll(taskIds1);
        allTasks.addAll(taskIds2);

        // Wait for those tasks to complete
        waitForStatus(allTasks, COMPLETED, FAILED);

        // Assert the tasks have finished with the correct status depending on type
        assertTasksCompletedWithCorrectStatus(allTasks);
    }

    @Property(trials=1)
    public void whenSubmittingTasksToOneEngineAndRandomlyKillingTheOthers_TheyComplete(
            @Size(min=1000, max=5000) List<TaskState> tasks) throws Exception {

        Set<TaskId> taskIds = sendTasks(engine1.port(), tasks);

        // Randomly restart one of the other engines until all of the tasks are done
        int lowerBoundMs = 3000;
        Random random = new Random();<commit_msg>Ignore test (#1455)<commit_after>/*
 * Grakn - A Distributed Semantic Database
 * Copyright (C) 2016  Grakn Labs Limited
 *
 * Grakn 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.
 *
 * Grakn 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 Grakn. If not, see <http://www.gnu.org/licenses/gpl.txt>.
 */

package ai.grakn.test.engine;

import ai.grakn.client.TaskClient;
import ai.grakn.engine.TaskId;
import ai.grakn.engine.TaskStatus;
import ai.grakn.engine.tasks.TaskState;
import ai.grakn.engine.tasks.TaskStateStorage;
import ai.grakn.engine.tasks.manager.ZookeeperConnection;
import ai.grakn.engine.tasks.mock.FailingMockTask;
import ai.grakn.engine.tasks.storage.TaskStateZookeeperStore;
import ai.grakn.exception.EngineStorageException;
import ai.grakn.test.DistributionContext;
import ai.grakn.test.engine.tasks.BackgroundTaskTestUtils;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.Size;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Ignore;
import org.junit.runner.RunWith;

import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

import static ai.grakn.engine.TaskStatus.COMPLETED;
import static ai.grakn.engine.TaskStatus.FAILED;
import static ai.grakn.engine.TaskStatus.STOPPED;
import static java.util.stream.Collectors.toSet;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;

@RunWith(JUnitQuickcheck.class)
public class GraknEngineFailoverIT {

    private static ZookeeperConnection connection;
    private static TaskStateStorage storage;

    @ClassRule
    public static DistributionContext engine1 = DistributionContext.startSingleQueueEngineProcess().port(4567);

    @ClassRule
    public static DistributionContext engine2 = DistributionContext.startSingleQueueEngineProcess().port(5678);

    @ClassRule
    public static DistributionContext engine3 = DistributionContext.startSingleQueueEngineProcess().port(6789);

    @BeforeClass
    public static void getStorage() {
        connection = new ZookeeperConnection();
        storage = new TaskStateZookeeperStore(connection);
    }

    @AfterClass
    public static void closeStorage() {
        connection.close();
    }

    @Property(trials=10)
    public void whenSubmittingTasksToOneEngine_TheyComplete(List<TaskState> tasks1) throws Exception {
        // Create & Send tasks to rest api
        Set<TaskId> tasks = sendTasks(engine1.port(), tasks1);

        // Wait for those tasks to complete
        waitForStatus(tasks, COMPLETED, FAILED);

        // Assert the tasks have finished with the correct status depending on type
        assertTasksCompletedWithCorrectStatus(tasks);
    }


    @Property(trials=10)
    public void whenSubmittingTasksToTwoEngines_TheyComplete(
            List<TaskState> tasks1, List<TaskState> tasks2) throws Exception {
        // Create & Send tasks to rest api
        Set<TaskId> taskIds1 = sendTasks(engine1.port(), tasks1);
        Set<TaskId> taskIds2 = sendTasks(engine2.port(), tasks2);

        Set<TaskId> allTasks = new HashSet<>();
        allTasks.addAll(taskIds1);
        allTasks.addAll(taskIds2);

        // Wait for those tasks to complete
        waitForStatus(allTasks, COMPLETED, FAILED);

        // Assert the tasks have finished with the correct status depending on type
        assertTasksCompletedWithCorrectStatus(allTasks);
    }

    @Ignore
    @Property(trials=1)
    public void whenSubmittingTasksToOneEngineAndRandomlyKillingTheOthers_TheyComplete(
            @Size(min=1000, max=5000) List<TaskState> tasks) throws Exception {

        Set<TaskId> taskIds = sendTasks(engine1.port(), tasks);

        // Randomly restart one of the other engines until all of the tasks are done
        int lowerBoundMs = 3000;
        Random random = new Random();
{'fraction_non_alphanumeric': 0.08456824512534819, 'fraction_numerical': 0.01181058495821727, 'mean_word_length': 4.042696629213483, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3229886', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2476, 'n_words': 702}