package com.atlassian.stash.internal.hazelcast;

import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.Member;
import com.hazelcast.core.PartitionService;
import com.hazelcast.instance.Capability;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.EnumSet;
import java.util.concurrent.TimeUnit;

import static org.mockito.Answers.RETURNS_DEEP_STUBS;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;

@RunWith(MockitoJUnitRunner.class)
public class HazelcastLifecycleTest {

    @Mock(answer = RETURNS_DEEP_STUBS)
    private HazelcastInstance instance;

    private HazelcastLifecycle lifecycle;

    @Before
    public void setup() {
        lifecycle = new HazelcastLifecycle(instance, 30);
    }

    @Test
    public void shouldMakeMemberPartitionHostAndRaiseEvent() {
        lifecycle.start();

        Member localMember = instance.getCluster().getLocalMember();
        verify(localMember).updateCapabilities(eq(EnumSet.allOf(Capability.class)));
    }

    @Test
    public void shouldDrainPartitions() {
        lifecycle.stop();

        PartitionService partitionService = instance.getPartitionService();
        verify(partitionService).drain(30, TimeUnit.SECONDS);
    }
}
